Lab 02 · How an agent actually worksTime: 8 minutes

How an agent actually works.

An agent is a model in a loop that can ask for tools. Your code runs the loop.

Objective

Step through one exchange with the Claude API and say, at each step, what moves, who sends it, and what is in the list.

You will need

This page. Nothing to install, and no key.

Part 1: Step through a run

Part 1 · Step through a run, three ways

Recorded from the Claude API on November 26, 2026 (UTC), model claude-opus-5-5.

Start with one tool call. Then try two, where the list grows twice, and none, where the loop ends on the first trip.

A diagram of four parts: the person who asks, your code, the model, and a tool you define, which hangs off your code. Beside it is the list of messages your code keeps, with a numbered place for every message the run will produce. The question: Has Andrew taught Claude Code to a live room? Step 1. A person asks a question. Your code starts a list with it as messages[0], role "user". On a plain Messages API request like this one, that list is the only memory the conversation has. Step 2. Your code sends the model the list. In these requests the system prompt and the tool definitions go in their own fields, system and tools, beside the list. Neither one is a message. Step 3. The model replies with a request, not a result. It has run nothing: the reply asks for search_portfolio and ends with stop_reason "tool_use", and your code adds it to the list as messages[1]. Step 4. Your code runs search_portfolio. It adds the result as messages[2], a message with role "user" that carries a tool_result block matched to the request by id. Step 5. Your code sends the whole list again. The model kept nothing from trip 1, so trip 2 carries all 3 messages. It costs 1,794 input tokens, against 894 on trip 1. Prompt caching can make the repeated part cheaper. Step 6. The reply ends with stop_reason "end_turn", so the loop stops. Your code hands the answer to the person. That is the whole agent: a model in a loop that can ask for tools, and code that keeps the list and runs them.

Step 1 of 6

A person asks a question.

Your code starts a list with it as messages[0], role "user". On a plain Messages API request like this one, that list is the only memory the conversation has.

Or use ← →

Trips to the modelTrips to the model: 0
Message in the listMessage in the list: 1

Sent beside the list on every trip

systemsearch_portfolioget_case_studylist_work

The list your code keeps

  1. messages[0]role "user"

    Asks the question.

    content "Has Andrew taught Claude Code to a live room?"

Part 2 Check yourself

  1. 1On trip 2 of the one-tool run, how many messages does your code send?

    Right. All three: the question, the reply that asked for the tool, and the tool result. The model keeps nothing between trips. On a plain request like these, your code sends the whole list every time.

    Not quite. All three: the question, the reply that asked for the tool, and the tool result. The model keeps nothing between trips. On a plain request like these, your code sends the whole list every time.

  2. 2A reply ends with stop_reason "tool_use". What happens next?

    Right. Your code runs it, because it is a tool you defined. It adds the result to the list as a tool_result block in a "user" message and sends the list again. The reply was only a request.

    Not quite. Your code runs it, because it is a tool you defined. It adds the result to the list as a tool_result block in a "user" message and sends the list again. The reply was only a request.

  3. 3Where do the tool definitions go?

    Right. Beside it. In these requests they go in the tools field on every trip, next to the system prompt in the system field. Neither one is a message.

    Not quite. Beside it. In these requests they go in the tools field on every trip, next to the system prompt in the system field. Neither one is a message.

If it fails

“The model ran the search.”

It did not, and it cannot. Look at step 3 of the one-tool run: the reply holds a tool_use block, a tool name and an input, and no result. The search runs in step 4, in your code. If your code never runs it, the model never gets an answer: the next request must carry a tool_result for that id, or the API rejects it.

That holds for tools you define, like search_portfolio. Anthropic’s server tools, such as web search and code execution, run on Anthropic’s side and come back already done. The model still runs nothing itself.

“The model remembers what we said.”

It keeps nothing between trips. On a plain request like these, your code sends the whole list every time: watch the message count on each send step. Drop a message from the list and the model has never seen it.

Some platform features, such as message threads or Managed Agents, keep the list on Anthropic’s side instead. Something still has to keep it.

Run it yourself

The loop that recorded these runs is in scripts/record-traces.mjs, and it needs an API key. You do not have to write this loop yourself: the Anthropic SDK’s tool runner, a beta helper, loops over tools you define, and with Managed Agents, Anthropic runs the loop for you.

How these runs were made

Next

This lesson’s runs used three tools that search this site. You can call the same tools from your own client.

Lab 03: Ask this site a question

How these runs were made

Recorded from the Claude API, request by request.

Each run is a real exchange with the Claude API, saved exactly as sent and received. The loop that made them was built with Claude Code, with no agent framework, and is written out in full in scripts/record-traces.mjs, so it is the same loop this lesson teaches.

RunRecorded (UTC)ModelTrips
One tool callNovember 26, 2026claude-opus-5-52
Two tool callsNovember 26, 2026claude-opus-5-53
No tool callNovember 26, 2026claude-opus-5-51

Every reply in these runs ends with stop_reason "tool_use" or "end_turn". A loop for real use also checks for max_tokens (the reply was cut off), refusal (do not run that turn’s tools) and pause_turn (a server tool paused, so send the list again). The recorder stops if one of them appears. When a tool fails, it sends the result back with is_error set to true.

To record them again, from the repository root:

node --env-file=<file with ANTHROPIC_API_KEY> scripts/record-traces.mjs