The agent loop (think → act → observe)
Every agent runs the same cycle. It thinks, takes one action, looks at the result, and repeats until a stop rule says it is done.
In 3 lines
- What it is: The agent loop is the cycle every agent repeats: think about the next step, act by using a tool, then observe the result.
- Why it matters: The loop is what lets an agent fix its own course. It is also where agents waste money or get stuck, so you need to know when it stops.
- Example: An agent asked "Is the shop open on Sunday?" searches the website, reads the hours page, sees the answer, and stops.
Think of it like…
Real-world example
A support team at an online shoe store uses an agent to answer refund questions. A customer writes: "Can I still return the boots I bought?"
Here is how the loop runs:
- Think: "I need the order date. I should look up the order."
- Act: The agent calls the "look up order" tool with the customer's email.
- Observe: The tool returns an order from 12 days ago.
- Think: "Now I need the return window. I should check the policy."
- Act: It calls the "read policy" tool.
- Observe: The policy says returns are open for 30 days.
- Think: "12 is less than 30, so yes. I have enough to answer."
- Answer: "Yes, you can still return them. You have 18 days left."
The agent asks for no more tools, so the loop stops. Notice that it never guessed the order date. Each step used a real result to decide the next one.
How it works
Quick reminder: the model is the large language model, the AI that reads and writes text. A tool is an action the model may ask for, like "search the web." A tool call is the model's request to use one. Each pass around the loop is one trip, also called a step.
Each trip has three moves, then a check:
- Think. The model reads the goal plus the whole conversation so far. It picks the next action, usually one tool call (it can ask for several at once).
- Act. The model asks for a tool call, such as "look up order." For most tools, your program runs it, not the model.
- Observe. Your program adds the tool's result to the conversation. This new text is called an observation.
- Check: should we stop? If not, go back to step 1. The model now sees a longer conversation.
Goal
│
▼
THINK ──► ACT (tool call) ──► OBSERVE (tool result)
▲ │
└────────── not done ◄── STOP? ◄────┘
│
yes ──► Final answerThe diagram shows think, act, observe, then a stop check on every trip.
Where the pattern came from
A 2022 research paper called ReAct (short for "Reasoning and Acting") described this pattern. The authors had a language model write a short reasoning note before each action. Many agents today skip the written note, but the think-act-observe cycle is the same. In some tests, the actions were searches on a simple Wikipedia tool. The paper reports that mixing the two cut down on made-up facts, often called hallucinations, compared with reasoning alone.
Anthropic describes agents the same way: a model using tools in a loop, based on feedback from its environment. The environment is everything outside the model that its tools touch, such as files, websites, or a database. It stresses that the agent should get ground truth at each step. Ground truth means real results from the world, like a tool's output or code that actually ran.
How the loop stops
A loop with no exit is a bug. Real agents use several stop rules at once:
| Stop rule | What it means |
|---|---|
| The model says it is done | It answers in plain text and asks for no tool. |
| Step limit | The program stops after a set number of trips. |
| Hand off to a human | The agent pauses to ask for approval or missing facts. |
| Reply cut off | The model hit its length limit mid-reply, so the program must handle it. |
Most tool errors do not stop the loop. The program sends the error back as an observation, and the model tries another way.
Two real examples show the first two rules. Anthropic's Claude API is the interface your code uses to talk to Claude. Each reply has a label called stop_reason. The value "tool_use" means "run my tools and send back the results," so you loop again. The value "end_turn" means the model has finished its answer, so you exit. Other values, such as "max_tokens" for a cut-off reply, also need handling.
The AI SDK, a code library for building AI apps, gives its tool-loop agent a default cap of 20 steps. That cap guards against runaway loops, meaning loops that keep going and keep costing money.
Common mistakes
- Trusting "done" with no check — the model may say the job is finished when it is not. Add a real check, like opening the saved file or re-running the code.
- No step limit — a stuck agent keeps calling tools and spending money. Always set a maximum number of trips.
- Hiding tool errors — if a failed tool returns nothing, the model guesses. Send the error message back as the observation so it can try another way.
- Letting the history grow forever — every trip adds text the model must re-read. Long runs get slow and confused. Keep tool results short and to the point.
Remember this
Quiz
Go deeper
- Read first: What is an AI agent?
- Read next: Tools and function calling, coming soon.
- Sources are listed below with the date they were last checked.
Done reading?
Mark it learned and the site will remind you to review it before you forget. Review page
Sources
Facts last checked against these on 20 Oct 2026.
What is an AI agent?
An AI agent is a language model that uses tools in a loop until a task is done. Here is what that means and how it differs from a chatbot.
Tools and function calling
A tool is an action your code offers a model. The model asks for it by name with inputs, your code runs it, and the result goes back.