What Is an Agent
What an agent is, how it differs from a single model call and from a fixed workflow, and how to decide whether a task needs one.
On this page
An agent is a language model that is given a goal and a set of tools, and that works toward the goal in a loop. It chooses a tool, the application runs it, and the model reads the result and chooses the next step, until it decides the goal is met. Agents are used for tasks where the right next step depends on what the previous step found, which a fixed script cannot handle.
Without this, a model that is asked why last night's backup failed can only list common causes such as a full disk or expired credentials. An agent can read the backup log, see that the disk is full, check how full it is, and look for what is using the space, because each step is decided from the result of the one before.
How an agent works
In Tools and MCP, a model asks the application to run a tool and then reads the result. An agent extends this by letting the model decide what to request next, repeatedly. It has four parts:
- The goal says what you want and, ideally, when to stop, for example "Find out why the backup failed. Do not delete anything."
- The tools are the actions the model can request, such as reading a log, checking disk usage or listing a folder.
- The loop is the application calling the model, running the tool the model asks for, adding the result to the history, and calling the model again.
- The stop condition ends the loop when the model gives a final answer, or when a limit set by the application is reached, such as a maximum number of steps or a time budget.
The trace below shows the backup investigation with three read-only tools: readLog, diskUsage and listDir. The tool results are made up for the example.
history = [goal]loop: step = model(history) if step is an answer: return step result = run(step.tool, step.input) history += [step, result]- history
who text goal Why did the backup fail? Delete nothing. - model calls
- 0
- tool runs
- 0
1The history starts with the goal, and the model is also given a short description of each of the three tools.
- history
who text goal Why did the backup fail? Delete nothing. model readLog("backup", last 20 lines) - model calls
- 1
- tool runs
- 0
2The model has no facts yet, so it asks for
readLoginstead of answering. - history
who text goal Why did the backup fail? Delete nothing. model readLog("backup", last 20 lines) tool ERROR: no space left on /mnt/backup - model calls
- 1
- tool runs
- 1
3The app runs the tool and appends the result to the history, which shows that the backup disk is full.
- history
who text goal Why did the backup fail? Delete nothing. model readLog("backup", last 20 lines) tool ERROR: no space left on /mnt/backup model diskUsage("/mnt/backup") - model calls
- 2
- tool runs
- 1
4The model now wants to know how full the disk is, which it only thought to ask because of the last result, so it requests
diskUsage. - history
who text goal Why did the backup fail? Delete nothing. model readLog("backup", last 20 lines) tool ERROR: no space left on /mnt/backup model diskUsage("/mnt/backup") tool 490 of 500 GB used (98%) - model calls
- 2
- tool runs
- 2
5The app runs the tool and appends the answer, which is that the disk is 98% full.
- history
who text goal Why did the backup fail? Delete nothing. model readLog("backup", last 20 lines) tool ERROR: no space left on /mnt/backup model diskUsage("/mnt/backup") tool 490 of 500 GB used (98%) model listDir("/mnt/backup", by size) - model calls
- 3
- tool runs
- 2
6The next question is what is using the space, so the model requests
listDirsorted by size. - history
who text goal Why did the backup fail? Delete nothing. model readLog("backup", last 20 lines) tool ERROR: no space left on /mnt/backup model diskUsage("/mnt/backup") tool 490 of 500 GB used (98%) model listDir("/mnt/backup", by size) tool snapshots-old 310 GB, 14 months old; daily 170 GB; logs 10 GB - model calls
- 3
- tool runs
- 3
7One folder holds 310 of the 490 GB and has not been touched for 14 months. The three folders add up to 490 GB, so nothing else is using space.
- history
who text goal Why did the backup fail? Delete nothing. model readLog("backup", last 20 lines) tool ERROR: no space left on /mnt/backup model diskUsage("/mnt/backup") tool 490 of 500 GB used (98%) model listDir("/mnt/backup", by size) tool snapshots-old 310 GB, 14 months old; daily 170 GB; logs 10 GB model Disk full. snapshots-old (310 GB) looks unused. Archive it? - model calls
- 4
- tool runs
- 3
8The model now has enough to explain the failure, so it gives a final answer and the loop ends. It suggests a fix but deletes nothing, as the goal required.
- history
who text goal Why did the backup fail? Delete nothing. model readLog("backup", last 20 lines) tool ERROR: no space left on /mnt/backup model diskUsage("/mnt/backup") tool 490 of 500 GB used (98%) model listDir("/mnt/backup", by size) tool snapshots-old 310 GB, 14 months old; daily 170 GB; logs 10 GB model Disk full. snapshots-old (310 GB) looks unused. Archive it? - model calls
- 4
- tool runs
- 3
9The run took four model calls and three tool runs. None of the tool calls was planned at the start, because each one was chosen from the result before it.
The application only ran the loop, executed the tools and kept the history. The decision to check the disk after seeing the error came from the model. The Agent Loop turns this pseudocode into working code.
Single call, workflow and agent
The word "agent" is applied to almost anything that uses AI, so it helps to separate three designs by who decides the next step.
Single call
A single call sends one prompt and uses one answer, such as "Rewrite this paragraph in simpler language". It is the cheapest and most predictable design, and it is enough whenever the whole input fits in the prompt and no outside information is needed. If a task can be done well this way, nothing more is needed.
Workflow
A workflow makes several model calls in an order that your code fixes. For example, code can fetch new support tickets, ask the model to classify each one, ask it to draft a reply, and then send the replies. The model never chooses what happens next, so every path is known in advance. This makes workflows faster, cheaper and easier to test than agents, and a task whose steps you can write down should be built as a workflow. It fails when the steps cannot be known in advance, because the code has no way to react to an unexpected finding. Common workflow variants are routing, where one model call picks which of several fixed paths to follow, and running independent calls in parallel and combining the results. Both keep the order of steps under your control.
Agent
In an agent, the model chooses the next step and decides when to stop, within the goal, tools and limits you set. This is the only one of the three that can handle a task whose path depends on what it finds, such as debugging a failure or researching a question across several sources. The costs are that each step is another model call, so the run is slower and more expensive, and that the model can take a different path on each run, which makes it harder to test.
When an agent is the wrong choice
Errors add up over a long run. If each step is right 95% of the time and a single wrong step spoils the result, a run of ten steps is correct throughout only about 60% of the time, because 0.9510 is about 0.60. An agent can sometimes notice and repair a bad step, so real results can be better, but a longer path still gives more chances to go wrong.
Systems that use several agents, each with its own role, multiply this problem, because each handoff is another place for information to be lost and every agent adds model calls. Start with one agent and split it only when one set of tools and instructions has become too large for the model to use well.
Choose the simplest design that does the job. Use a single call if one good prompt is enough. Use a workflow if you can write the steps down in advance. Use an agent only when the steps depend on what is found along the way, and when a wrong step can be noticed and undone. Debugging and research fit this description, and a weekly report does not. Coding assistants that edit code and run tests until they pass are agents, and in that kind of tool a person reviews the final result.
Problems to avoid when building an agent
Building an agent for a fixed pipeline
If you can draw the steps as a flowchart before the work starts, write them as code. An agent has to rediscover the same steps on every run, which costs more, takes longer, and sometimes skips a step. The model should be used inside a workflow for the parts that need language understanding, such as classifying or summarising.
A goal with no finish line
A goal such as "improve our test coverage" has no end, so the agent either stops early or runs until a limit stops it. A goal such as "add tests for parseDate until every branch is covered, then stop" tells the agent what done means. State the finish line in the goal, and also set a maximum number of steps in the application, so that a confused agent cannot loop forever.
Tools that can do damage
An agent will use any tool it is given, including one that deletes files or sends email, and it will sometimes use it wrongly. Give the agent the narrowest set of tools the task needs, make them read-only where possible, and require a person to approve any action that cannot be undone. The backup example is safe to run unattended because all three tools only read.
Trusting the final report
The last message of an agent describes what the model believes it did, and it can claim that a test passes when it never ran the test. Check the real outcome yourself, by looking at the changed file, the test result or the database record. Evals make this kind of checking systematic.
A history that keeps growing
Every tool result is added to the history that the model reads on the next step, so a long run with large outputs, such as a whole log file, fills the context and makes later steps slower and less accurate. Have tools return only what is needed, for example the last 20 lines of a log, and see Context Rot for what happens as the context fills.
What this changes in practice
Before building an agent, write down the steps you would take by hand. If they are always the same, build a workflow. If each step depends on the last result, an agent is reasonable, and the next things to decide are the goal with its finish line, the smallest useful set of tools, a limit on steps, and which actions need a person's approval. Test the agent by checking the outcomes of its runs, not by reading its final message.
Pick the simplest design for each task: single call, workflow or agent. Give one sentence of reasoning for each.
- Translate a product description into Spanish.
- Every morning, tag new customer reviews by topic and post a count per topic to the team chat.
- Find out why the checkout page got slower this week, using logs, code changes and metrics.
- Turn a meeting transcript into a list of action items with owners.
Show a sample answer
- Use a single call. There is one input and one output, and no lookup is needed.
- Use a workflow. The steps are the same every day (fetch, tag, count, post), so code runs them and the model only does the tagging.
- Use an agent. The cause could be in the logs, in a code change or in the metrics, and each finding decides where to look next.
- Use a single call. The task looks large, but the transcript fits in one prompt and no tools are needed.
An agent is worth its extra cost only when the path cannot be known until the work has started.
What makes a system an agent rather than a workflow?
Each step of an agent is right 90% of the time. About how often is a 5-step run right at every step?
Every Friday, last week's sales should be pulled from a database, summarised and emailed to the team. What should you build?
Related lessons
An agent is only as good as its tools, so continue with Tool Use to see how to design them, and then The Agent Loop to write the loop yourself.