What Is the Agent Loop?
How an AI agent repeats model, tool and result until the task is done, with its limits and failure points, explained for your AI interview.
In short
The agent loop is the cycle an AI agent repeats: the model asks for a tool, your code runs it, the result goes back to the model, and this repeats until the model writes a final answer or a limit stops it.
12 min readBeginnerVery common · estimateFacts checked 26 November 2026
Say it out loud
Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.
The agent loop is the cycle an AI agent repeats to finish a job. The model looks at what it has and asks for one tool, like looking up a policy. My code runs that tool and sends the result back. The model then decides the next step, and the loop ends when it writes a final answer. Each pass costs time and money, so I always set a step cap. A stuck run stops and goes to a person.
An AI agent is a model that uses tools to finish a job. The agent loop is the cycle that makes that happen. The model reads everything so far, then asks for one tool, such as looking up a customer's policy. Your code runs that tool and sends the result back as text. The model reads it and decides again.
The loop repeats until the model writes a final answer with no tool request. The basic idea is simple. The hard part is the edges. A tool can time out. A model can ask for the same lookup again and again. Each pass is another model call, so a run that never ends also never stops costing money.
In my designs the loop has three guards. First, a step cap, so the run stops after a set number of passes and goes to a person. Second, a timeout on every tool, so a slow system fails fast with a clear error the model can read. Third, a check in code before each tool runs, so the model can ask for a tool but never decides the outcome alone.
Anthropic's guide from December 2024 describes agents as just LLMs using tools based on environmental feedback in a loop. It also says stopping conditions help keep control, and that much of its tooling has changed since. So I use its ideas, not its tool details. When I explain an agent, I start with the loop, then add the tools and the limits.
What is the agent loop, in plain words?
An AI agent is a model that uses tools to finish a job, such as reading a file or looking up a record. The agent loop is the cycle it repeats to get there. The model looks at what it has and asks for one tool. Your code runs it, and the result goes back to the model.
The loop ends when the model writes a final answer and asks for no more tools. Each pass is one step, and each step is another call to the model, so every extra pass adds time and cost.
Picture a new claims clerk on a motor insurance file. They read the claim, ask the records team for the policy, read the reply, then decide what to check next. They stop when the file is ready for a senior person. The clerk does not run the records system themselves. They ask, then read the answer. The model in an agent works the same way. The claims agent in this answer does the same job in software.
Key words
- Agent
- An AI model that uses tools in a loop to reach a goal.
- Tool call
- The model's request to run a named tool, with its inputs.
- Tool result
- What your code sends back after running the tool, so the model can read it.
- Step cap
- A limit on how many passes the loop may make.
- Timeout
- The longest time your code waits for a tool before it gives up and returns an error.
How does one pass through the loop work?
Each pass has the same five beats. The chart shows them, with the numbers in the order they happen.
One pass of the agent loop
Claim and rules
what the model is asked to do
Model
reads all text, picks one step
Asks for a tool
tool name plus JSON inputs
Final answer
no tool request, loop ends
Your code runs it
checks inputs, sets a timeout
- 1Your app gives the model the claim and the rules.
- 2The model asks for one tool, with its inputs.
- 3If it is done, it writes the final answer and the loop ends.
- 4Your code runs the tool. The model never does.
- 5The result goes back, and the model decides again.
- 1
Send the task and the tool list
Your app sends the claim, the rules and a description of each tool to the model.
- 2
The model asks for one tool
It returns a tool name and the inputs as JSON, a plain text format for structured data, such as the claim number.
- 3
Your code runs the tool
It checks the inputs, calls the real policy system and waits for a reply.
- 4
The result goes back
Your code adds the reply to the conversation as a tool result.
- 5
The model decides again
It reads everything so far, then asks for another tool or writes the final answer.
The Claude API docs put the contract in plain terms:
“The model never runs anything on its own.”
Claude API docs
“When Claude calls one of your tools, the API response contains a tool_use block with the tool name and a JSON object of arguments.”
Claude API docs
Your code runs the operation and sends the output back on the next request. The same docs say the loop exits on any other stop reason than a tool request. A stop reason is the API's label for why the model stopped, so your code can tell a finished answer from an early stop.
What does a real run look like in a claims agent?
A mid-size motor insurer runs a triage agent on new claims. Its tools are a policy lookup, a photo reader and a garage quote service. The job arrives as a short request: "Triage claim M-2291 and send the quote." Here is the run, start to end.
Pass 1: policy lookup
The model asks for the policy. Result: active, cover 8,000 (made-up numbers).
Pass 2: photo reader
Result: six photos, rear bumper damage, no airbag sign.
Pass 3: garage quote, first try
The service times out. The timeout comes back as a tool result, so the model sees it.
Pass 4: garage quote, retry
The model asks once more. The second try times out too.
Pass 5: final answer
No quote, so the model writes a summary and sends the claim to a person.
The hard part is the timeout. The model cannot tell a slow service from a wrong answer, so your code has to. It sets a timeout on each tool, returns a clear error, and caps the run at eight passes. The cost is that a person reads some claims a faster run could have cleared. For a claim that needs a quote before anyone decides, that trade is fair.
Tip
In the interview, say the loop is simple. The failures live at the edges: timeouts, repeated calls, caps and the hand-off to a person.
Why does the loop need a stop?
A model can keep asking for tools, and nothing inside the model makes it stop. So the stop rule belongs in your code. Anthropic says so in its guide to building agents, and the Agent SDK docs describe the same risk:
“it's also common to include stopping conditions (such as a maximum number of iterations) to maintain control.”
Anthropic, building agents
“Without limits, the loop runs until Claude finishes on its own”
Claude Agent SDK docs
The SDK has settings for this. You can cap the number of turns, or cap the spend, and a run that hits a limit ends with an error result rather than an answer. Your code can then route the claim to a person.
A cap stops the run. The answer can still be wrong. After a cap, send the case to a person with the log of every step, so they can see what the agent tried and where it got stuck.
Here is the loop with its cap in plain Python. The model is a script, so the code runs free and gives the same output every time. The shape is the one a real agent uses.
# A scripted fake model: no real AI here, just the loop and its limit.
TOOLS = {
"lookup_policy": lambda claim: "policy active, cover 8000",
"read_photos": lambda claim: "6 photos, rear bumper damage",
}
def fake_model(results):
# Looks at the results so far and picks the next step.
if not results:
return ("lookup_policy", "M-2291")
if len(results) == 1:
return ("read_photos", "M-2291")
return ("final", "Rear bumper claim, policy active. Send to a person for the quote.")
def run_agent(max_steps):
results = []
for _ in range(max_steps): # the step cap
name, arg = fake_model(results) # the model only picks
if name == "final":
return "DONE: " + arg
results.append(TOOLS[name](arg)) # your code runs the tool
print(name, "->", results[-1])
return "STOPPED: step cap reached, send to a person"
print(run_agent(max_steps=3))
print(run_agent(max_steps=2))Output
lookup_policy -> policy active, cover 8000 read_photos -> 6 photos, rear bumper damage DONE: Rear bumper claim, policy active. Send to a person for the quote. lookup_policy -> policy active, cover 8000 read_photos -> 6 photos, rear bumper damage STOPPED: step cap reached, send to a person
Tip
Asked how to stop endless loops? Name three guards: a step cap, a spend cap, and a check that blocks the same tool call with the same inputs repeated.
How would you debug an agent that keeps calling the same tool?
Log every pass: step number, tool name, inputs and result. If the same tool gets the same inputs three times in a row, that is the clue. Then read the result. Often the tool returns an empty reply or a vague error, and the model keeps retrying to make sense of it.
Fix the tool's reply first, so it says what went wrong and what to do next. Then add a code rule that stops an identical call after a set number of repeats, and keep the step cap as the last guard.
What does the interviewer want to hear?
A strong answer covers
- A plain definition: the model asks for a tool, your code runs it, the result goes back, and it repeats.
- Who runs the tool: your code, never the model.
- What ends the loop: a final answer, or a limit such as a step cap.
- Why it needs guards: timeouts, a step cap and checks before each call.
- Cost and risk: every pass is another model call, and errors can compound.
- Logs: every step recorded, so a bad run can be explained.
Common traps
Trap"The model runs the tool itself."
BetterIt only asks for the tool. Your code runs it, so that is where you check inputs and set timeouts.
Trap"An agent runs until it is done, so it is safe."
BetterWithout a step cap and a stop rule, a stuck run keeps calling tools and costing money. Set the limits first.
Trap"The agent loop is the framework."
BetterThe loop is a simple cycle. Frameworks wrap it, so know what runs underneath when something breaks.
Can you spot the problem in a loop?
Spot the risk
Which loop could run for a very long time and keep costing money on a claims agent?
Quick check
A claims agent asks for the same policy lookup with the same claim number six times. What should you add first?
Can you answer these from memory?
Flash cards · tap a card to flip it
What will they ask next?
- Q156What is tool (function) calling?11 min
- Q157What is an AI agent?11 min
- Q158Workflow vs agent?10 min
- Q161What is ReAct for agents?12 min
- Q173How do you stop endless agent loops?11 min
- Q482Write a minimal agent loop with tool dispatch.Coming soon
Learn it in depth
Last updated on 26 November 2026
Building an agent that loops over tools?
I design and build AI agents, including the loop, the tool checks, the step limits and the hand-off to a person. Book a short call and tell me what your agent needs to do.