The agent loop, by hand with the raw SDK
Before any framework, build the loop yourself so you are never mystified by one. An agent is a model in a loop with tools: it thinks, picks a tool, calls it, observes the result, and decides again, until the task is done or it needs a human.
Why it matters
Every agent framework (LangGraph, Google ADK, Claude tool use, OpenAI assistants) is ergonomics over the same heartbeat. If you only meet agents inside a framework, debugging feels like magic. If you have written the raw loop once, you can read any framework's execution log and see exactly where Think, Act, or Observe went wrong.
The four steps, named
Think
ModelGiven the goal, history, and tool schemas, the model proposes the next action: answer the user, or call a named tool with arguments.
Act
Your codeYour runtime executes the tool (HTTP, DB, calendar, search). The model does not run code. You do.
Observe
ResultTool output (or error) is appended to the conversation as a tool result message the model can read.
Decide again
LoopThe model sees the real result and either calls another tool or returns a final answer. That responsiveness is what separates an agent from a fixed workflow.
What you will build (raw SDK)
With the plain OpenAI or Anthropic SDK (no LangGraph yet): register one or two tools with JSON schemas, run a while-loop that sends messages, executes tool_calls, and feeds results back. Stop when the model returns text with no tool calls, or when you hit a max-iteration guard.
Critical
Always bound the loop
Unbounded agents can burn tokens and money. Cap iterations (for example 8 to 12), log every tool call, and fail closed when the cap is hit.
Workflow vs agent (the line)
A workflow follows steps you defined in advance. An agent decides its own steps toward a goal. Use a workflow when the path is known. Use an agent when judgment or branching depends on tool results you cannot script ahead of time. Cost, latency, and failure modes are higher for agents. That tradeoff is the professional skill.
Watch out
Common mistakes
- Skipping the raw loop and jumping straight into a framework.
- Letting the agent loop with no max-iteration or cost guard.
- Treating tool errors as silent failures instead of observations the model should see.
- Calling something an agent when it is a fixed three-step pipeline.