跪拜 Guibai
← All articles
Frontend · Backend · Agent

The Agent Loop Is Just a While Loop with a Memory

By 不一样的少年_ ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

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.

Summary

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.

Takeaways
A single tool call creates a script; an agent loop creates an autonomous process that decides its own next step.
Every loop iteration must pass the full message history to the model so it knows what tools were already called and what they returned.
The loop exits when `tool_calls` is empty, signaling the model has enough information to answer.
A hard `MAX_STEPS` limit is mandatory to prevent infinite loops from models that re-read files or chase dead ends.
Tool failures should return error messages to the model rather than crashing the program, giving the model a chance to recover.
Batched tool calls in a single round are handled by iterating all `tool_calls` before the next model request.
Separating `ChatClient` (single request) from `Agent` (loop and history) keeps the architecture clean when more tools are added.
Conclusions

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.

Concepts & terms
Agent Loop
A control flow where the model repeatedly decides the next action, the program executes it, and the result is fed back into the conversation history until the model produces a final answer instead of requesting more tools.
Function Calling / Tool Calling
A model capability where, instead of returning plain text, the model returns a structured request specifying which function to call and with what parameters. The program executes the function and returns the result to the model.
Message History / Context
The full array of messages (system, user, assistant, tool) passed to the model on each request, serving as the agent's short-term memory of what has happened so far in the conversation.
MAX_STEPS
A hard upper limit on loop iterations that prevents runaway agent behavior, infinite token consumption, and stuck loops when the model fails to reach a conclusion.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗