How AI Agents WorkCourse map

Chapter 0 · Start here

LLM vs agent

A language model turns text into text. An agent is the program around the model: it decides what to send, does what the model asks for, and repeats.

The model: text in, text out

Think of the model as a function. You pass it some text, and it gives you back some text.

response = model(context)

The text you pass in is called the context. In an agent, the context holds the instructions, the conversation so far, and a short description of each tool the agent may use. The text that comes back is usually a reply. It can also be a request, such as "call search_files with the query auth".

Four facts about this function explain most of agent design:

So when the model answers, it draws on two things: what it learned in training, and the context of this one call.

The agent: a loop around the model

An agent is a program that calls the model again and again. Each time, it sends the current situation, reads the reply, does what the reply asks, and records what happened.

State Build the context Call the model Answer or action Run the tool Record the result, repeat

Here is a complete agent in Python-style pseudocode:

def run_agent(task, tools, max_steps=30):
    messages = [system_prompt(), user(task)]
    for step in range(max_steps):
        reply = model(messages, tools)       # the only line that uses the model
        messages.append(reply)
        if not reply.tool_calls:
            return reply.text                # no request: the model says it is done
        for call in reply.tool_calls:
            result = execute(call)           # your code runs the tool, under your rules
            messages.append(tool_result(call.id, result))
    return "stopped: step limit reached"

Only the line with model(...) involves the model. Everything else is ordinary code that you write. The messages list is the agent's state. The if decides when to stop. The execute line is where something in the world actually changes.

The rest of this course adds features to this loop, one chapter at a time: memory, planning, subagents, approvals, and more.

Example: two calls

A user asks, "How many open bugs do we have?" The agent has one tool, search_issues. On the first call, your code sends this:

// call 1: what the model receives
messages: [
  {"role": "system", "content": "You are Patch, a coding agent..."},
  {"role": "user",   "content": "How many open bugs do we have?"}
]
tools: [search_issues(label: string, state: "open" | "closed")]

The model cannot search, so it replies with a request:

{"role": "assistant", "tool_calls": [
  {"id": "call_1", "name": "search_issues",
   "arguments": {"label": "bug", "state": "open"}}]}

Your code runs the search and finds 17 issues. Then it calls the model a second time. It sends everything again, plus the request and its result:

// call 2: what the model receives
messages: [
  {"role": "system",    "content": "You are Patch, a coding agent..."},
  {"role": "user",      "content": "How many open bugs do we have?"},
  {"role": "assistant", "tool_calls": [{"id": "call_1", "name": "search_issues", ...}]},
  {"role": "tool", "tool_call_id": "call_1", "content": "17 issues: #412, #415, ..."}
]

Now the model answers, "There are 17 open bugs." It seemed to remember its own request, but it did not. Your code sent the whole conversation back, including the model's earlier reply.

Two related features are worth knowing. Some provider APIs can store the conversation for you, so you send an id instead of the full history. The provider rebuilds the context on its side, so the model still reads the full history on every call. Providers can also cache the repeated start of a context. That makes calls cheaper and faster, but the context is just as long. Chapter 4 covers both.

Who holds what

A sentence like "the AI remembered my preference" mixes up five different parts:

The parts of an agent system
PartWhat it isKeeps information?
Model callOne run of the model on one contextNo
Provider serviceThe API in front of the model. It can store conversations and cache repeated text.Sometimes
Agent runtimeYour loop. It builds the context, calls the model, checks rules, runs tools, and records results.Yes, during a run
StoresFiles, databases, and saved checkpointsYes, across runs
ContextThe exact input for one call, built fresh each timeNo, it is rebuilt

When someone says the agent remembers something, ask which of these parts holds it. It is never the model call itself.

Where intelligence and agency come from

The intelligence comes from the model. It reads the request, picks the next step, writes the arguments for a tool, and makes sense of the results.

The agency comes from your code. Agency means the ability to act in the world over time. That needs tools connected to real systems, permission to use them, a loop that keeps going, saved state, and the authority to stop. All of that lives in the runtime.

So the model chooses, and your code decides whether each choice happens. That is why the same model can power very different agents. In a chat window it can only talk. With file and shell tools, it becomes a coding agent. With access to tickets and refunds, it becomes a support agent.

Who does what

Try this before reading the answers. For each job, pick who does it in a typical agent, where your code runs the loop.

Pick an answer in each row.

Runs a shell command

  1. Model
  2. Runtime
  3. Both
Why

Runtime. The model can only ask. Your code runs the command.

Chooses which tool to use next

  1. Model
  2. Runtime
  3. Both
Why

Model. It picks from the tools your code listed in the context.

Remembers what happened in the last call

  1. Model
  2. Runtime
  3. Both
Why

Runtime. It sends the history again. A provider might store it instead, but the model never does.

Decides to call the model again

  1. Model
  2. Runtime
  3. Both
Why

Runtime. The loop is ordinary code. The reply is only an input to that decision.

Blocks a dangerous command

  1. Model
  2. Runtime
  3. Both
Why

Runtime. The model might decide not to ask, but the actual block has to happen in your code.

Writes the arguments for a tool call

  1. Model
  2. Runtime
  3. Both
Why

Model. It writes them as part of its reply.

Checks that the arguments are valid

  1. Model
  2. Runtime
  3. Both
Why

Runtime. Treat the arguments like any input you did not write yourself.

Reads an error message and tries another way

  1. Model
  2. Runtime
  3. Both
Why

Model. Your code's job is to put the error in front of it.

Shortens old history to save space

  1. Model
  2. Runtime
  3. Both
Why

Both. Your code decides when to shorten and what must be kept. A model call usually writes the summary.

Checks that the task really succeeded

  1. Model
  2. Runtime
  3. Both
Why

Runtime. It runs the checks, such as the tests. Another model can help judge, but your code decides what counts.

Now the questions people most often get wrong, with their answers:

The model, the runtime, and the rest
QuestionAnswer
Does the model run tools?No. It writes a request. Your code decides whether to run it, then runs it.
Does the model remember earlier calls?No. Your code, or a provider that stores the conversation, sends the history again each time.
Who decides to call the model again?Your loop. The usual rule is: if the reply asks for tools, run them and call again.
Who stores state?Your runtime and its stores. Sometimes the provider stores the conversation.
Who runs shell commands?Your code, inside whatever machine or container you gave it.
Who enforces permissions?Your code and the environment around it, such as the operating system, containers, and network rules. Never the model alone.
Who decides the task is finished?Three parts share this. The model says it is done, your code stops, and a check confirms it. See below.

What "done" really means

When an agent says it is finished, three separate things happen, and different parts own them:

The model says it is done Your code stops the loop A check confirms success

A run that stopped is not necessarily a run that succeeded. Decide how you will check success before you build. For a coding agent, the check might be: the failing test now passes, the other tests still pass, and no files outside src/ changed. Chapter 16 builds on this.

From one call to an agent

To tell a workflow from an agent, ask who decides what happens next.

One call

One model call does one job, such as summarizing a document. There is no loop.

A workflow

Your code decides the order of the steps, and the model fills in some of them. For example, an invoice workflow might go like this:

Read the invoice Check the fields Fix bad fields Save

An agent

The model decides the next step, inside limits your code enforces. Fixing a failing test is a good example, because the right steps depend on what the agent finds along the way.

Choose a step Run it Choose the next step Stop when a rule says so

Many real systems mix the two. Use the simplest design that works. A workflow is easier to predict, test, and pay for. Choose an agent when the right steps depend on what it discovers, as in debugging.

Running example: Patch, version 0

Patch is the coding agent we build up across the course. It works on a small team's web service: a Python API with a PostgreSQL database, deployed on AWS. In version 0, Patch is not an agent at all. You paste in a failing test and the file, one model call suggests a fix, and you run the tests yourself. You are the loop. Chapter 1 replaces you with code.

What goes wrong

Believing the model remembers

Why it happens
Your code sends the history back so smoothly that the model seems to remember. Then a new session or a shortened history breaks something you thought it had learned.
How to spot it
Log the exact context of every call. If a fact is not in the context, the model did not use it.
What to do
Decide where each needed fact is stored and how it gets into the context. Chapters 4 and 5 cover this.

Trusting "done"

Why it happens
A reply without a tool request only means the model thinks it is finished. Models sometimes report success they did not achieve.
How to spot it
Compare what the model claims with evidence, such as test results.
What to do
Write the success check into your code and run it before reporting success.

Rules that live only in the prompt

Why it happens
"Never delete files" in the instructions is a request, not a lock. The model can misread it, and text it reads elsewhere can argue against it.
How to spot it
Try to talk the agent out of the rule. If wording can change the outcome, nothing enforces the rule.
What to do
Enforce rules in your code and in the environment. Chapter 10 shows how.

You decide, AI builds

Decide yourself

  • Whether you need an agent at all, or a workflow is enough.
  • Which actions the agent may take, and on which systems.
  • How you will check that a task succeeded.

Delegate to AI

  • The loop code and the calls to the provider's API.
  • Formatting messages for a specific provider.
  • Retries and logging.

Check yourself

What makes something an agent rather than a single model call? (Q1)

Answer

A loop in which the model chooses the next action. Your code carries out that action, sends the result back to the model, and repeats until a stop rule fires. A single call has no loop and takes no actions.

Where does agency live? (Q3)

  1. In the model's training
  2. In your code: the loop, the tools, the permissions, and the saved state
  3. In the system prompt
Answer

In your code. The model chooses actions, but the ability to carry them out, keep going, and stop lives in the runtime.

What does the model know when you call it? (Q4)

  1. Everything the agent has seen so far
  2. What it learned in training, plus the context of this call
  3. Its training, plus everything the provider has stored
Answer

Training plus this call's context. Stored conversations and memory matter only once something puts them into the context.

Takeaways

Going deeper