TL;DR
A single tool call answers one question. An LLM agent keeps going: a language model reads a goal and everything that has happened so far, chooses the next action, a program carries it out, and the result is added to the context. The loop stops when the model writes a final answer or runs out of its step budget. The model is the decision-maker inside a loop that the program controls.
Builds on: In-Context Learning, Tool Use · Leads to: Generative Agents
Where it's used
Software repair. A coding agent is given a failing test. It reads the error, edits a file, runs the test again, and repeats until the test passes or its step budget ends. Each run's output decides what it does next, so no single prompt could have planned the whole fix in advance.
Research and reporting. An agent asked for a short report on a company's earnings searches for the filing, opens it, pulls out the revenue figures, and writes a summary with the sources it used. Retrieval steps like these are covered in retrieval-augmented generation.
Operations tasks. A helpdesk agent resets a password, checks whether the account is locked, and then sends a confirmation message. Each step depends on the answer to the step before, and the agent stops and asks a person before any action that cannot be undone.
How it works
Take a tiny goal: "Buy the cheaper of two train tickets." The tools are price(ticket), which returns a price, and
book(ticket), which buys a ticket.
The step budget, the most actions the loop may take, is 4. The context starts with the goal, the tool descriptions and an empty history. Each value a tool returns is an observation.
- Read the goal. The model sees the goal and the tools. Nothing has been done. Check: the model knows it needs two prices before it can compare them.
- Choose the first action. The model writes a short thought, "I need the price of A", and then the structured action
price("A"). The program runs it and returns 30. The context now holds the action and the observation. Check: the history has one entry. - Choose the second action. The model writes "Now the price of B" and runs
price("B"). It returns 24. Check: the history has two entries, and both prices are visible to the model. - Decide and act. The model compares the two prices. It writes "B is cheaper, 24 is less than 30" and runs
book("B"), which returns "booked, reference 77". Check: the action follows from the two observations, not from a guess. - Stop. The model writes "Booked ticket B for 24, reference 77." Because a final answer was written, the loop ends. Check: three actions were used, within the budget of 4.
flowchart TD
A["Goal, tools and empty history"] --> B["Model writes a thought and an action"]
B --> C{"Is it a final answer?"}
C -- "yes" --> Z["Return the answer"]
C -- "no" --> D["Program runs the action with a tool"]
D --> E["Append the observation to the history"]
E --> F{"Steps used up?"}
F -- "yes" --> G["Stop with the best answer so far"]
F -- "no" --> B
The loop is the whole mechanism. The model does not remember between runs unless the program stores the history, and it does not act on its own; every action passes through the program, which can check it, log it, or refuse it. The same vocabulary of agents, policies and actions is used in reinforcement learning.
The math
Write for the goal and for the observations received so far, with actions . The agent's state at step is the context. Because the state holds the whole history, the setting is a partially observable decision process, and the context is the agent's only window onto the world.
The model defines a policy : the probability that it chooses action given the context . Here are the model's fixed weights. An action is either a tool call or a final answer. The environment (the tools and the world behind them) returns an observation with probability .
Step 2 in symbols: one action
At step the model samples , which means it picks an action at random with the probabilities the policy assigns. The program computes from , and the context becomes , where means appending the new action and observation to the end of the history. The step is the same operation as a single tool call, repeated.
Steps 3 and 4 in symbols: each choice, and the probability of a whole run
A run is the sequence with a final answer. Its probability is the product of the choices and the observations:
For the train example the observations are fixed by the prices, so for each one, and only the policy terms matter.
Step 5 in symbols: the stop rule
The loop stops at the first where is a final answer, or at , the step budget. The reported answer is the final answer if there is one, and otherwise the best answer the program can extract from the history. The budget is what keeps the loop from running forever.
Worked examples
An agent takes three choices in a run: , and . Assume every observation is certain, so .
- Multiply the first two policy terms: .
- Multiply by the third term: .
- The run has probability , so it happens in about 68 of every 100 runs.
- Check: each factor is between 0 and 1, so the product is smaller than each of them. .
The interpretation: a longer run multiplies more factors, so its probability falls quickly even when each step is likely. This is one reason agents with many steps are harder to trust than short ones.
An agent has a budget of steps. Its first two actions are price("A") and price("B"), and the third action
is book("B"). The fourth step is unused.
- After step 1 the history has one action and one observation. The budget has steps left.
- After step 2 the budget has steps left.
- After step 3 the model writes the final answer, "Booked B for 24", so the loop stops at .
- The unused budget is step. It is not spent, because the stop rule fired first.
- Check: , the full budget.
The interpretation: the budget is a limit, not a target. An agent that uses it all without a final answer has run out of steps, and the program should report that rather than present a guess as the result.
A coding agent has 3 attempts to fix a failing test. Assume each attempt fixes the bug with probability 0.6, independently.
- The probability that attempt 1 fails is .
- The probability that all three attempts fail is .
- The probability that at least one attempt succeeds is .
- Check: the probability of success rises with each extra attempt, and 0.936 is larger than 0.6, the single-attempt chance.
- In practice the attempts are not independent. If the agent makes the same wrong edit twice, the second attempt adds little, so the real figure is lower than 0.936.
The interpretation: a loop of retries helps a great deal when attempts are varied, and much less when they repeat the same mistake.
Try it: an agent with 2 attempts fixes each bug with probability 0.5, independently. What is the probability that at least one attempt succeeds? Answer: both attempts fail with probability , so at least one succeeds with probability .
Common mistakes and tradeoffs
It is tempting to think an agent is the model. In fact the agent is the loop around the model: the tools, the history, the stop rule and the checks in the program. The same model can behave as a plain chatbot or as an agent depending on that loop.
It is tempting to think more steps mean a better answer. In fact each step costs time and money, and each new observation can pull the agent off course. Long runs also multiply small errors, as Example 1 shows.
It is tempting to think an agent can safely act without oversight. In fact an action such as sending a message, deleting a file or paying an invoice may be impossible to undo. Good designs require a person to confirm such actions, and they keep the tools' permissions as narrow as the task allows.
It is tempting to think an observation is always reliable. In fact a web page or document read by the agent can contain text that tries to redirect it. Observations should be treated as data, the same warning as in Tool Use.
When to use an agent: the task has several steps that depend on each other's results, and the path is not known in advance. When not to use one: a fixed sequence of steps is cheaper and more predictable as ordinary code, and a single question with one tool call needs no loop at all. Alternatives: a fixed pipeline with a model at one or two points, and chain-of-thought prompting alone when the model needs to reason but has no tools to use.
History
The idea of an agent that perceives, decides and acts has long been part of artificial intelligence. Textbooks such as Russell and Norvig's Artificial Intelligence: A Modern Approach describe agents in general terms, with the language model as just one possible decision-maker. What changed after 2022 is that a language model could read a goal and a history in plain text and choose a next action, because a model trained to follow instructions ([[instruction-tuning|instruction tuning]]) needs no special training for each task. ReAct (2022) showed the thought, action and observation loop written as text. Toolformer (2023) showed that a model could learn when to call a tool. In spring 2023, open-source projects such as AutoGPT showed the loop to many developers, and research such as the Generative Agents paper (2023) studied agents with memory in a simulated town. Reliability, cost and safety remained open problems.
Recap
- An LLM agent is a language model run in a loop with tools and a history.
- At each step the model chooses an action; the program runs it and appends the observation to the context.
- The loop ends at a final answer or at the step budget.
- A run's probability is the product of its choices, so long runs are harder to trust.
- Retries help when attempts vary, and less when they repeat the same error.
What to read next: Generative Agents, which gives agents memory and reflection, and ReAct prompting, the thought-action-observation format that many agents build on.
Check yourself
QuestionWhat does the program do in each step of an agent loop, and what does the model do?Show answer
The model chooses the next action from the goal and the history. The program runs that action with a tool, appends the observation to the history, and checks the stop rule before the next step.
QuestionAn agent's three choices have probabilities 0.5, 0.8 and 0.9, and every observation is certain. What is the probability of the run, to three decimals?Show answer
, and . The answer is 0.360.
QuestionWhy is a step budget needed even when the model is good at the task?Show answer
Without a budget, a loop can run forever if the model never writes a final answer, for example because it keeps retrying an action that fails. The budget guarantees the loop ends, and the program can then report that the goal was not reached.
QuestionA helpdesk agent is about to delete a customer's account. What should the system do before the action runs?Show answer
The system should stop and ask a person to confirm, because deletion cannot be undone and the agent may have misread the request. Narrow permissions and a confirmation step protect against a wrong action at low cost.