Module 27 · 45 min

What an Agent Is

You can say what a model, a token, a context window, a tool, a turn and a system prompt are, and point at each one in a transcript you produced.

You have probably used a chatbot already: you type, it types back, and that feels like the whole story. The app you started in module 26 looks exactly like that from the outside, but underneath it is a different animal. The word for that difference is agent. Every remaining page on this track is about making one behave, so let us nail down what that word means.

A model answers. Hand it text, and it produces more text. It has no hands, no memory between conversations, and no access to anything on your machine.

An agent acts. It is a model wrapped in a program that runs in a circle: send the text to the model, read back what it wants, and if what it wants is for something to be done, do that thing and send the result back. Round it goes until the model stops asking for things and answers you instead. Nothing about the model changed. The circle around it is the agent.

flowchart LR
you["your message"] --> model["the model<br/>reads everything so far"]
model -->|"asks for a tool"| tool["the tool runs<br/>on your machine"]
tool -->|"result goes back"| model
model -->|"nothing left to ask"| answer["the answer<br/>reaches you"]
One turn of the circle. Your message goes in at the left; the model either asks for a tool or answers you. The box around it is the context window, which holds everything that has been said so far.

The things it can ask for are tools

Here is the part that trips people up: the model cannot actually do anything on your machine. What it can do is name a tool and say what it wants passed to it.

A tool is one named thing the program around the model knows how to do, read a file, list a folder, run a command, search the web. Your program on the machine decides whether to carry that out. That gap between naming and doing is where every module about permissions and hooks later in this track lives, because it is the only place a real decision can be made.

One trip around the circle, the model asks, a tool runs, the result goes back, is a turn. Turns are counted, and they are capped: the Agent SDK takes a maxTurns setting, and every lab script in module 14 sets one, because a circle with no cap and a vague instruction keeps going. The agent-loop page is the reference for the circle itself.

The word session covers one run of that circle from your message to the final answer. In a chat app a session is longer-lived than that, which is the subject of module 29 and the reason the app you ran counts its turns out loud.

Everything it has seen is one pile of text

Before each reply, the model reads back over the whole conversation: your messages, its own, the contents of every file a tool opened, the output of every command a tool ran. That pile has a name and a fixed size. It is the context window, the amount of text the model can hold at once, and module 02 is the page about what happens when your instruction is competing with everything else inside it.

The pile is measured in tokens rather than in words. A token is the unit both of the model’s limits and of your bill: the window’s size is a number of tokens, and what a session costs is worked out from how many went in and came back. Claude Code even carries an environment variable, CLAUDE_CODE_MAX_CONTEXT_TOKENS, whose documented job is to override the window size the tool assumes.

One part of the pile you did not type. Before your first message, the program puts standing instructions in front of the model telling it what it is and how to behave. That is the system prompt. Module 07 is about editing it, and modules 31 and 36 give each agent in the finished app its own, which is how two agents in one app end up with different personalities and different jobs.

What happened when you typed in module 26

The app you ran was in fake mode, which leaves the model out of the circle above: none was called, so nothing was billed, and a canned script answered instead. Everything else was real. Your keystrokes went from the browser to the server over a connection that stays open. The server held a conversation and gave it an identity. The reply came back in pieces rather than all at once. The count in the text went up by one each time you sent something.

Fake mode is on, so no model was called. This is turn 1 of one session. You said: Say hello.

That sentence is the shape of a turn with the expensive part removed. When you fill it back in, three things change and nothing else does. There is a model at the far end, so the reply is written rather than canned. There is an API key in the environment, an API being the way one program asks another for something over the network, and the key being the password that says who is paying. And the last message a real session sends back carries num_turns and total_cost_usd: a count of trips round the circle and a figure in dollars, which module 14 prints on screen.

That dollar figure is the only place an agent’s appetite shows up. A model that will not stop asking for tools does not look broken while it is happening. It looks busy.

Build

Nothing new to install, and no code on this page. We will work through the ladder against what you already have running.

  1. Run it. Start the app from module 26 again in fake mode and send three messages.
  2. Read one thing. Find the turn count in each reply, and find the JSON line the server printed when it started. One name in that line, and one value beside it, are the whole reason no model was called and nothing was billed. Say which.
  3. Change one thing. Stop the server and start it again, then send a message. The count goes back to 1. Say why, in terms of the pile of text, not in terms of the app.
  4. Build. Write out the six words on this page, model, token, context window, tool, turn and system prompt, with your own one-sentence definition of each. Where a definition needs another one of the six words to make sense, note which. That is the map you will use for the rest of the track, and getting it wrong here is cheap.
The mistake most people make first

Everybody’s first mental model of an agent is that it remembers what you told it. You say “keep your answers short”, it does, and it is reasonable to conclude that the instruction is now in force.

It is not in force. It is in the pile. Your sentence went into the context window as text and sits there being read alongside everything that arrived afterwards, and forty messages later, half of them file contents, it is one voice among many. What you see when it fails is nothing at all: no error, no warning, no notice that a rule was dropped. Only long answers again. Module 02 covers the measured version of this and the two mechanisms that actually hold.

The same misreading has a cost side. If an instruction is not a rule the agent must obey, then “stop when you have finished” is not a stopping condition either. An agent given a vague task and no maxTurns keeps circling. Each turn adds to the pile and to the bill. The only place it shows is the dollar figure on the final message, which arrives after the money is spent.

Does this travel?

The circle is every agent framework there has ever been, the single most portable idea on this site. Whatever you pick up next will send text to a model, read back a request to run something, run it, and feed the result in again. What differs between them is vocabulary and packaging: some say step where this says turn, some say function call where this says tool, some hide the loop entirely and hand you a finished agent. Learn to find the circle in a new tool’s documentation and you can read the rest of it. The names maxTurns, total_cost_usd and CLAUDE_CODE_MAX_CONTEXT_TOKENS are Claude Code’s own and travel nowhere.

Check yourself

  1. A friend says their agent “read the file and then decided to delete it”. Which part of that sentence describes the model, and which part describes the program around it? Why does the distinction matter for anyone deciding what an agent is allowed to do?
  2. You send the same message twice in one conversation and get two different-length replies. Nothing in the app changed between them. Name the thing that did change, and say where it lives.
  3. Fake mode produced a reply with no model and no cost. Which of the six words on this page still applied to what you saw, and which did not?