Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 53 ยท Tools and Agents

Building an Agent Loop

Build a think-act-observe agent loop with the ReAct pattern, stop conditions, step limits, loop detection and tracing. Live Python with a scripted model.

A single function call answers one question. An agent repeats the cycle: the model looks at the situation, decides what to do next, a tool does it, and the model looks at the result and decides again, until the task is finished. The agent loop is the small piece of ordinary code that keeps that cycle going.

Despite the hype, there is no magic in it. An agent is a while loop around a model call, plus a list of tools, a history, and limits that stop it. This lesson builds one from scratch, with a scripted stand-in for the model so that every run is deterministic, and shows the safeguards that keep a real agent from running forever or burning money.

The agent loop

Think The model reads the history and decides.
Act Your code runs the chosen tool.
Observe The result is added to the history.
Stop A final answer, or a limit is reached.

Repeat until done. Always have a way out.

1 / 2

The think-act-observe cycle

flowchart LR
    A["History<br/>(goal + steps so far)"] --> B["Model decides"]
    B -->|"tool call"| C["Run tool"]
    C -->|"observation"| A
    B -->|"final answer"| D["Stop"]
    B -->|"step limit hit"| E["Stop with an error"]

Each pass appends the model's decision and the tool's result to the history.

Each iteration has three parts:

  1. Think: send the goal and everything that has happened so far to the model. It answers with either a tool call or a final answer.
  2. Act: if it asked for a tool, validate the arguments and run it.
  3. Observe: append the result (or error) to the history and go around again.

The model has no memory between calls. The history is its memory, which means every step makes the prompt longer, and cost grows with each iteration (see Counting Tokens and Estimating Cost).

The ReAct pattern

ReAct (reasoning and acting) is the classic way to structure the loop. At each step the model writes a short thought about what it knows and what to do next, then an action (a tool and its input). The tool's output is fed back as an observation, and the cycle repeats until the model writes a final answer:

TEXT
Thought: I need the population of Norway, then divide it by the area.
Action: search("population of Norway")
Observation: About 5.5 million people.
Thought: Now I need the area.
...
Final Answer: About 17 people per square kilometer.

Writing the thought before the action gives the model room to plan, and gives you something readable in the logs. Today most APIs use native tool calling (as in the previous lesson) instead of parsing "Action:" lines from plain text, and reasoning models do their thinking internally, but the cycle is the same. We use the text format here because it makes every step visible.

A complete agent in 60 lines

In the example the model is a scripted function that replays a plausible run. The tools are a calculator and a tiny search. The calculator uses Python's ast module to evaluate arithmetic safely: never pass model output to eval.

Python A ReAct agent with a scripted model and two tools

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The printed trace is the most valuable debugging tool you have. When an agent does something strange, read its trace: which tool did it pick, what did it pass, what did it see? Production systems store these traces (with ids and timings) for every run.

Stop conditions and limits

A loop that can only stop when the model decides to stop will eventually run forever. Always build in more than one exit:

  • Final answer: the normal exit.
  • Step limit: a hard cap, commonly 5 to 20 iterations depending on the task.
  • Token or cost budget: stop when the total tokens or spend passes a threshold.
  • Time limit: stop after a number of seconds.
  • Loop detection: stop (or nudge the model) when it repeats the exact same action with the same input. That is the signature of a stuck agent.
  • Error budget: stop after several failed tool calls in a row.
  • Human exit: for risky actions, pause and ask the user for approval, and let the user cancel.

When a limit is hit, return a clear partial result: what was done and what remains. Do not pretend the task succeeded.

Python Step limits and loop detection with a stuck agent

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Without loop detection, this agent would use all ten steps for nothing. With it, the run stops at step 3 and returns a reason you can show to the user or log. The second call shows the plain step limit cutting in first.

Designing a good agent

  • Start without an agent. If a fixed sequence of steps solves the problem, write a plain workflow. Use an agent only when the next step genuinely depends on what was found (see MCP, Workflows and Multi-Agent Systems).
  • Give few, sharp tools with clear descriptions, and make results small.
  • Make errors informative, so the model can recover.
  • Trim the history when it grows long (see Memory, Planning and Context Management).
  • Test with scripted models, as in this lesson: you can replay a tricky sequence (an error, a loop, an empty search result) and check that your loop handles it.
  • Limit permissions. An agent that can run shell commands or spend money needs approvals and a sandbox, because anything a prompt injection can reach, the agent can do.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the agent loop, what plays the role of the model's memory between steps?

    Models are stateless between calls. The whole history is sent again each step, which also explains why long runs get expensive.
  2. 2. In ReAct, what is an 'observation'?

    Observations are tool outputs (or errors) that go back into the history for the next decision.
  3. 3. Why should an agent loop have a step limit even when the model usually finishes quickly?

    Safety limits protect against rare failures, which at scale will happen.
  4. 4. Why is passing model-written text to Python's eval() in a calculator tool a bad idea?

    Model output is untrusted input. Parse and evaluate only the operations you allow.

Summary

  • An agent is a loop: the model decides, your code runs a tool, the result goes back into the history, repeat.
  • ReAct writes a thought, an action and an observation at each step; native tool calling does the same job in structured form.
  • The history is the only memory, so context and cost grow every step.
  • Always have several exits: final answer, step limit, budget, time, loop detection, error budget, human cancel.
  • Log a trace for every run, use scripted models to test the loop, and prefer a plain workflow when the steps are known.

Next: manage a growing history in Memory, Planning and Context Management.