Slavik Shynkarenko

notebook № 076 plates · 15 min to read, longer to play

Anatomy of an AI agent

On a loop that uses tools

A language model on its own can only write text. Give it tools, a loop and somewhere to keep its notes, and it starts doing things. This notebook takes an agent apart: the loop, the tools, the context window, the common shapes, and the brakes.

I

plate I · the brain

A mind without hands

A large language model does one thing: it reads text and writes the text most likely to come next. It has no clock, no internet, no files. It can’t check anything. If you ask it something it doesn’t know, it still answers, because writing an answer is all it can do.

An agent is that same model with hands. The program around it, often called the harness, offers a list of tools: read a file, search the web, run a calculation. The model can’t run them itself, but it can write “please run this tool with these inputs”, and the harness does it and hands back the result.

That is the whole difference, and it is a big one. The model stops guessing and starts checking.

Ask about the weather with tools off, then on. Then try the multiplication, which a model can get almost right, which is worse than wrong.

fig. 7.1~/model-vs-agentpick a question
A scripted illustration, not a live model. The answers show the typical shape of each case.
II

plate II · the heartbeat

The loop

One tool call is rarely enough. Real tasks need several steps, and each step depends on what the last one found. So an agent runs in a loop: the model thinks about what to do next, acts by asking for a tool, then observes the result, which is added to the conversation. Then it goes round again, until the model decides it is done and writes a final answer instead of a tool call.

Step through the run below. Watch the transcript grow: every tool result becomes part of what the model reads on the next turn.

fig. 7.2~/agent-loopstep · run · switch task
A scripted transcript, not a live model: the messages are written by hand to show the loop clearly. Token counts are rough, about four characters per token.

Two things to notice. First, the model never touches the world directly: it only ever reads and writes text, and the harness does the rest. Second, the model chooses when to stop. That is powerful, and it is also how agents get stuck: a loop with no exit condition can run forever. Plate VI is about the brakes.

III

plate III · the hands

Tools are a contract

A tool has two halves. The half the model sees is just text: a name, a description of when to use it, and a schema, the shape of the inputs it takes. The other half is ordinary code in the harness, which the model never sees.

The model asks for a tool by writing a small block of JSON (a plain text format for structured data). The harness checks it against the schema, runs the code, and sends back the result, or an error the model can read and fix. The description is the only manual the model gets, so a vague one leads to the wrong tool at the wrong time.

Delete the "city" line and run it. Then pick send_email: some tools should never run without a human saying yes.

fig. 7.3~/tool-calledit the call, then run it
The checking is real: your edited call is parsed and validated in the page. The tool results are made up.
IV

plate IV · the desk

A desk that fills up

Everything the model knows about the task lives in its context window: the text it reads before writing each reply. Think of it as a desk. The instructions sit on it, so do the tool descriptions, the conversation so far and every tool result. The desk has a fixed size, measured in tokens, chunks of text about three quarters of a word long.

Each turn, the model rereads the whole desk. So every big tool result makes every later step slower and more expensive, and a desk that is too full can push the important notes out of sight.

Agents cope by compacting: summarising the old conversation into a few lines and clearing the rest. Anything worth keeping for longer goes into memory outside the window, and is fetched back when needed. Memory is just text you put back on the desk. The memory notebook goes deep on how.

fig. 7.4~/context-windowadd things, then compact
Illustrative sizes. Real windows run from tens of thousands to a million tokens or more; the shape of the problem is the same.
V

plate V · the anatomy

Shapes of an agent

One model in one loop goes a long way. When it stops being enough, people wire several model calls together in a few common shapes. A chain runs fixed steps in order. A router sorts the request and sends it to a specialist. An orchestrator splits a big task, hands the parts to workers (often called subagents) with their own clean context, and stitches the results together. An evaluator pair has one model write and another check, round after round.

fig. 7.5~/patternspick a shape
Each dot is one message. Call counts are for one typical task, to compare the shapes, not to measure any product.

Every extra box buys something and costs something. Workers can run in parallel and each keeps a small, focused desk, but every hand-off loses detail, and more model calls cost more. The usual advice holds: start with one loop, and add a box only when you can name the problem it solves.

VI

plate VI · the brakes

Knowing when to stop

An agent acts on the world, so its mistakes are real. It can retry a failing command forever, or tidy up a disk a little too well. The harness, not the model, is where the brakes live.

Three brakes cover most of it. A step limit ends runaway loops. An allow list says which tools may run on their own. And for anything that can’t be undone, the agent stops and asks a human. The model can be talked into things; the harness can’t.

Run with every brake on, then switch off ask before deleting. Then lower the step limit until the job can’t finish.

fig. 7.6~/guardrailsset the brakes, then run
A scripted run of a cleanup task, not a live model. The brakes are real checks in the page.

Questions people ask

What is the difference between an LLM and an AI agent?

An LLM takes text in and gives text out, once. An agent wraps the model in a loop, gives it tools to act with and a context to keep notes in, and lets it choose its next step from what the last one returned.

How do AI agents use tools?

Each tool is described to the model with a name, a purpose and a JSON schema for its inputs. The model replies with a structured call; the program around it checks the arguments, runs the tool and passes the result back. The Model Context Protocol standardises this contract.

What are the common AI agent architectures?

A single agent in a loop, a fixed chain of steps, a router that sends each request to a specialist, an orchestrator that hands subtasks to workers, and a writer paired with a critic. Start with the simplest one that works and add structure only when it fails.

Related material

elsewhere in the notebooks

further reading

← all notebooks