Agent Loop

Agents

The control loop an LLM agent runs — call the model, execute any tool calls it returns, feed the results back, repeat until it stops asking for tools.


In one line

The control loop an LLM agent runs — call the model, execute any tool calls it returns, feed the results back, repeat until it stops asking for tools.

What it actually means

An agent loop is just a while loop around an LLM call. The model is given a system prompt, the conversation so far, and a list of tool schemas. It returns either a final assistant message or one or more tool calls. If it’s tool calls, you execute them, append the results as tool messages, and call the model again. You stop when the model returns a final answer or you hit a step limit. Production loops add timeouts, retries, max-step caps, cost ceilings, and human-in-the-loop checkpoints for sensitive actions.

Why it matters

The agent loop is the runtime of every modern AI agent. Frameworks dress it up — graphs, state machines, supervisors — but at the bottom they’re all running this loop. If you understand it directly, you can debug any framework, build your own when frameworks get in the way, and reason clearly about cost, latency, and failure modes.

Example

messages = [system_prompt, user_message]
for _ in range(MAX_STEPS):
    resp = llm.chat(messages, tools=TOOLS)
    if not resp.tool_calls:
        return resp.content
    for call in resp.tool_calls:
        result = TOOLS[call.name](**call.args)
        messages.append({"role": "tool", "tool_call_id": call.id, "content": result})
    messages.append(resp)
raise StepLimitExceeded()

You’ll hear it when

  • Implementing or reviewing any agent.
  • Setting cost or step ceilings on a production agent.
  • Adding human approval to a sensitive action.
  • Debating LangGraph vs writing your own loop.
  • Diagnosing agents that loop forever or get stuck.

Related terms

See also