Lesson 13 of 40Agents
What Is an Agent?
Explain what makes an AI agent (a model, tools, a loop and an environment), how tool use works, and when an agent is the wrong choice.
You have been using agents for a whole unit: Claude Code and Copilot's agent mode both read, act and check in a loop. Now you will look inside one. "Agent" is the most stretched word in AI right now. It gets stuck on chatbots, on scripts with one model call, and on products that run for an hour on their own. This lesson gives you a precise definition you can use: what parts an agent has, how a model "uses" a tool when all it can do is write text, what makes the loop stop, and how to tell when you should build something simpler. Everything in the rest of this unit builds on these ideas.
Four parts
An agent is a language model that works toward a goal by repeatedly choosing actions, seeing what happened, and choosing again. It has four parts:
- The model (an LLM). It reads everything in its context and decides the next step. It is the only part that "thinks".
- Tools. Functions the model is allowed to ask for: read a file, run a command, search the web, query a database. Each tool has a name, a description, and a list of inputs.
- The loop. Plain code that sends the conversation to the model, runs whatever tool it asked for, adds the result to the conversation, and sends it back. Again and again.
- The environment. The world the tools touch: your repo, a terminal, a browser, an API. The environment is where the real effects happen, and where the model gets feedback (a test failed, the file does not exist).
Take away any one and you no longer have an agent. A model with no tools can only talk. Tools with no loop give you one action and then stop. A loop with no environment has nothing to learn from.
You hand the loop a goal, not a list of steps.
How tool use actually works
A model only produces text. It cannot open a file or press Enter. So how does it "use a tool"?
You describe each tool to the model in the request: its name, what it does, and what inputs it takes (as a small JSON schema). When the model decides it needs a tool, it does not write a normal answer. It writes a structured tool call instead: the tool name plus the inputs, as JSON. The API marks the response so your code knows a tool is wanted.
Then the work passes to you:
- The model asks. "Call
read_filewith{"path": "orders.csv"}." - Your code runs it. Your program reads the file, with whatever checks and permissions you choose.
- The result goes back. You add the output to the conversation as a tool result and call the model again.
That division of labour matters. The model never touches your machine directly. Every action passes through code you wrote, which is where you add limits, approvals and logging. Claude Code is exactly this pattern: the model asks for Bash or Edit, and the harness (the program around the model) decides whether to run it or ask you first.
The goal arrives.
Watch a loop run
Here is a complete agent loop in plain Python. The model is fake: a function with hard-coded rules, so it runs without an API key. Everything else is real. The tools are real functions, and the loop really runs them and feeds results back. Run it, then read the run_agent function: that loop is the heart of every agent you will use.
Press Run (⌘ Enter) to see what happens.Notice that nobody told the loop "first list files, then read, then add". The fake model chose each step by looking at the latest result. With a real LLM, those choices come from the model reading the history, which is why the same loop can handle tasks you never planned for.
The history only ever grows. Each turn adds one more result, and the model is sent the whole thing every time. That is why long agent runs get expensive and why the context fills up.
What does this print?
history = ["goal"] for tool in ["list_files", "read_file", "add"]: history.append(tool + " result") print(len(history))
Stop conditions
A loop that never ends is a bug that costs money. Every agent needs clear ways to stop:
- The model says it is done. It replies with a plain answer and no tool call. This is the normal exit.
- A turn or step limit. A hard cap, like
max_turnsabove. Claude Code subagents have amaxTurnssetting for the same reason. - A budget. Stop after a number of tokens, dollars or minutes.
- A check passes or fails for good. For coding agents: the tests pass, or the same error has repeated three times.
- A human says stop. In Claude Code, Esc interrupts at any moment.
Models get stuck in ways that look busy. This one asks for the same missing file forever:
Press Run (⌘ Enter) to see what happens.Without max_turns, that runs until something external kills it. Always decide the limit before you start the loop, not after the bill arrives.
Workflow or agent?
Not every system with an LLM and some code is an agent. There is a useful line between two designs:
- A workflow is code-driven. Your program decides the steps in a fixed order and calls the model inside some of them. "Summarise the ticket, then classify it, then draft a reply." The model fills in each step but never chooses what comes next.
- An agent is model-driven. You give a goal and tools, and the model decides which step to take, how many, and when to stop.
| Single call | Workflow | Agent | |
|---|---|---|---|
| Who picks the steps | Nobody, one step | Your code | The model |
| Predictable | Very | Mostly | Less |
| Cost and latency | Lowest | Medium | Highest, varies |
| Good for | Classify, extract, summarise | Known multi-step processes | Open-ended tasks with feedback |
The official advice is to start at the simplest tier that works: a single call, then a workflow, and only then an agent. Most features in real products are single calls or workflows. The lesson on shipping AI features goes deeper on this choice.
Match each task to the simplest design that fits.
When not to use an agent
Agents are slower, cost more, and are harder to predict than a single call. Before you build one, ask four questions:
- Complexity. Is the task genuinely open-ended, with steps you cannot list in advance? If you can write the steps down, write a workflow.
- Value. Is the result worth the extra tokens and time? Running an agent to rename one variable is waste.
- Viability. Can a model actually do it well today? Try it by hand in a chat first. If the model fails with a human guiding it, a loop will not save it.
- Cost of error. Can mistakes be caught and undone? Code has tests, review and git. Sending emails to customers or moving money does not roll back easily, so those need a human approval step or no agent at all.
A coding agent passes all four well: code tasks are open-ended, valuable, within current models' ability, and errors are caught by tests and undone with version control. That is why coding was one of the first places agents worked.
None picked yet
Put one turn of an agent loop in order.
- Tap the steps below in the right order.
- agent
- A model that works toward a goal by choosing tool calls in a loop, using feedback from its environment.
- tool
- A function described to the model by name, description and input schema, which your code runs on request.
- tool call
- A structured request from the model to run a named tool with specific inputs.
- harness
- The program around the model that runs the loop, executes tools and enforces permissions, like Claude Code.
- workflow
- A system where code decides the order of steps and calls the model inside them.
- stop condition
- A rule that ends the loop: a final answer, a turn limit, a budget, a passing check, or a human.