운영자 직접 작성 · 2026년 9월 업데이트 · 약 10분 분량
에이전트 프레임워크를 쓰기 전에 루프를 직접 써보세요. 저는 LangChain을 먼저 썼다가 디버깅에서 막혔고, 120줄짜리 루프를 손으로 쓴 뒤에야 "도구 호출이 실패하면 어디서 멈추는지"가 보였습니다.
MAX_STEPS = 10
def run(query, tools, llm):
history = [{"role": "user", "content": query}]
for step in range(MAX_STEPS):
msg = llm(history)
if msg.get("tool_calls"):
for call in msg["tool_calls"]:
try:
result = tools[call["name"]](**call["args"])
except Exception as e:
result = f"ERROR: {e}"
history.append({"role": "tool", "id": call["id"], "content": str(result)})
continue
return msg["content"]
raise RuntimeError("Step limit exceeded")