You should use an agent framework when your application has complex workflows, and a hand-written loop when the agent logic is simple and you need more control.
Use a hand-written loop when:
You have a simple tool-calling workflow.
There are only a few tools.
You need full control over execution.
while True:
response = llm.invoke(messages)
if not response.tool_calls:
break
for tool_call in response.tool_calls:
result = execute_tool(tool_call)
messages.append(result)Use an agent framework when:
Your workflow has multiple steps or agents.
You need conditional routing and parallel execution.
You need memory and state management.
You need checkpointing and recovery.
You need human approval or monitoring.