The Agent Loop Is Just a While Loop with a Memory
Most agent tutorials skip the loop and show only a single tool call, which breaks the moment a task requires multiple steps. This walkthrough builds the exact control loop that every production agent framework wraps, making the pattern transferable to LangChain, CrewAI, or a custom stack.
A single-shot tool call turns a chatbot into a script, not an agent. The fix is an agent loop: a `for` loop that feeds the model the full conversation history each round, lets it request tools, executes them, and appends the results back into the history. The loop exits only when the model stops requesting tools and returns a plain-text answer. This turns a rigid two-step flow into an open-ended investigation where the model can read one file, realize it needs another, and continue until it has enough context.
A hard `MAX_STEPS` guard prevents runaway loops from models that get stuck re-reading the same file or chasing irrelevant paths. Tool failures are not fatal; the error message is fed back to the model so it can try a different path or report the problem. The implementation also handles models that batch multiple tool calls in a single round, executing them sequentially before asking the model to re-evaluate.
The resulting architecture cleanly separates responsibilities: `ChatClient` handles a single model request, while `Agent` owns the loop, the message history, and the decision to continue or stop. This skeleton stays unchanged when more tools like `write_file` or `bash` are added later; only the tool-execution dispatch needs to grow.
The agent loop is conceptually trivial — a `for` loop with an exit condition — yet it is the single architectural decision that separates a demo from a working agent.
Hardcoding a two-request flow is the default in most quickstart examples; breaking out of that pattern is the real threshold to building autonomous agents.
Feeding tool errors back into the conversation is an underappreciated pattern that prevents agents from silently failing and lets them self-correct.
The `MAX_STEPS` guard is not a hack but a fundamental safety and cost-control mechanism that every agent runtime needs, regardless of framework.