AI Agents

How AI Agents Work

Follow one agent run from goal to finish so you can read a run log, spot the usual failures and set the limits that contain them.

Goal Reason Act Observe Repeat Done

The short answer

An AI agent works in a loop. The model reads the goal and the text gathered so far, picks one tool and fills in its inputs. Software runs the tool and returns the result. The model reads it and chooses again, until it judges the goal met or the software reaches a step limit.

What is the agent loop?

The agent loop is a cycle with four moves: the model reads the goal and the text gathered so far, the model chooses one action, the surrounding software carries out that action, and the model reads the result before choosing again. The cycle ends once the model judges the goal met or the software reaches a limit.

Model reads goal and history so far Model chooses one tool, or stop Software acts runs the tool Result returns added to the history One pass one call to the model goal met Final answer the run ends cap reached Run stopped reported unfinished
The loop has two exits: the model declares the goal met, or the software counts to its cap and stops the run.

Researchers at Princeton and Google described this pattern in the ReAct paper, short for "reasoning and acting". They had a model alternate between a written thought and an action, and reported that the combination handled multi-step tasks better than reasoning or acting alone. Agent platforms now run versions of that cycle.

The model itself runs nothing. It writes a request, such as "call the spreadsheet tool with these values", and the software around it makes the call. Tool calling explains that hand-off in detail.

What happens at each step of a run?

A run has six stages, and stages two to five repeat until the work is done.

  1. Goal in. A trigger starts the run: a form submission, a chat message or a schedule. The software assembles the system prompt (the standing instructions), the tool descriptions and the new input into one block of text.
  2. The model reasons. It reads the whole block and works out what the goal needs next.
  3. The model picks a tool. It names one tool from its list and fills in the inputs.
  4. The software runs the tool. It makes the call and returns the result to the model as text.
  5. The model reads the result. It compares what came back with the goal.
  6. Repeat or stop. If work remains, the model picks another tool. If the goal is met, the model writes a final answer and the run ends.

Each pass through stages two to five is one call to the model. The model keeps nothing between calls, so the software sends the full history each time. Agent memory explains how the software manages that history as it grows.

What does one run look like in practice?

My intake agent in n8n, a no-code automation platform, takes one form submission through eight steps. It checks the submission against five business rules, writes a record and prepares a welcome email for a person to approve.

  1. A person submits the form. The form trigger starts the workflow.
  2. A clean-up step tidies the submitted fields into a consistent format. This step is fixed logic with no model in it, because tidying a field has one right answer.
  3. The agent node receives the clean fields. Its system prompt holds the five rules in plain language.
  4. Google Gemini, the model, reads the rules and the submission.
  5. Gemini calls the lookup tool to fetch a detail the rules depend on, and reads what comes back.
  6. Gemini applies the rules and calls the Google Sheets tool to write the record as a new row.
  7. Gemini drafts the welcome email. The workflow pauses and waits for a person.
  8. I read the draft and approve it. The Gmail tool sends the email and the run ends.

Two kinds of step sit side by side in that run. I fixed the trigger, the clean-up and the approval in advance. The model chose the tool calls in between by reading the rules, and I drew no arrow from the lookup to the spreadsheet.

Fixed steps The model chooses the calls Fixed 1 Form trigger 2 Clean-up fields 3 Agent node 4 Gemini reads 5 Lookup tool 6 Sheets new row 7 Draft email 8 Approve then send
I fixed the steps at both ends in advance; the filled circles in the middle are the calls the model chose by reading the rules.

That split is deliberate. Fixed steps handle the parts with one right answer. The agent handles the part where rules written as sentences meet text typed by a person.

Why do agent runs fail?

Runs fail in three common ways: the model picks the wrong tool, the model repeats itself, or the model lacks a piece of context it needs.

Wrong tool

The model chooses a tool by reading its name and description. Two tools with similar descriptions invite a wrong pick, and a vague description invites wrong inputs. Rewrite the description before you blame the model: say what the tool does, what each input means and which case it serves.

Loops

A tool returns an error or an empty result, and the model sends the same call again with the same inputs. The error text gave it no reason to try something different. Write error messages the model can act on, such as "no row matches this email address; ask the user to confirm it".

Missing context

The model works from the text in front of it and has no other source. If a rule lives in a colleague's head or in a document the agent cannot open, the model fills the gap with a guess that reads like fact. Put the rule in the system prompt, or give the agent a tool that fetches it.

What limits a run?

Two controls limit a run: a step cap and an approval gate. You set both in the software around the model, so they hold even on a run where the model ignores its instructions.

A step cap is a maximum number of loop passes. The n8n agent node calls this setting "max iterations". Once the count reaches the cap, the software stops the run and reports that it did not finish. With a cap in place, an endless loop ends as a visible failure with a bounded bill.

An approval gate pauses the run before a named action and waits for a person. Place it before actions you cannot take back: an email to a customer, a payment, a deletion. Leave reversible steps such as drafting without a gate, or the approver will start clicking through without reading.

A run that repeats itself Step cap 1 2 3 4 5 6 7 8 the same call, six times Stopped as unfinished A run with a wrong detail Approval gate 1 2 3 4 Draft email a wrong name in it Held until approved
Both guards sit in the software: the looping run ends at the cap, and the wrong draft waits at the gate for a person to read it.

Keep the log of each run as well: the input, each tool call with its inputs, each result and the final answer. Reading one failed run in order tells you more than ten reruns, because you see the exact step where the model went wrong. If you plan to build an agent yourself, building AI agents sets out the order of work.

Common questions

Does the AI model run the tools itself?

No. The model writes a request that names a tool and its inputs. The software around the model makes the call and passes the result back as text.

How does an agent know it has finished?

The model reads the latest result, compares it with the goal and writes a final answer in place of another tool request. A step cap in the software ends the run if the model does not stop by itself.

Why does the same agent give different results on the same input?

A language model samples its output, so two runs can take different steps or choose different wording. Use fixed steps for the parts of a task that need an identical result each time.

Sources and references

  1. arXiv, ReAct: Synergizing Reasoning and Acting in Language Models
  2. Anthropic, Building effective agents
  3. Anthropic, Tool use with Claude
  4. n8n, Advanced AI documentation

Concepts covered: agent loop, ReAct, tool calling, large language model, n8n, step cap, human approval.

Weekly newsletter

The AI Brief by Clarence Cheang

A weekly exploration of the developments, technologies and ideas shaping artificial intelligence.

One email a week. Unsubscribe in one click. What is in each issue