Skip to content
The AI Agent Factory

26.2 Reading context use in a trace

Approved by
Panaversity ·
16 min read

In everyday life. Your phone says its storage is almost full. You do not delete things at random. You open the storage screen, which shows one bar split by kind: photos, videos, apps, messages. Videos fill most of it. Now you know what to remove. You also know what you can get back. An app can be downloaded again. A photo you took exists nowhere else, so it is saved somewhere before anything is deleted.

What you will learn. You practice this in Lab 26, Tasks 2 to 5, where you read tokens by part before and after each change.

  • What are the parts of the window, and which grow?
  • Where do the numbers come from?
  • How does a trace show what was cut, and when?
  • Who knows the budget?
  • What can the trace not tell me, and what does reading it cost?

Why it matters. Each cut in this chapter removes a different part. If you cut a part that was never the large one, you pay for the cut and gain nothing.

At work. Monday, 8:40. The check has failed, and you open traces/month-end-drift.jsonl, the record of Friday's run. You read the tokens by part at four moments (Figure 26.2). The instructions and the tool definitions are the same size at all four. The tool results grow from nothing to most of the window. One line of the trace marks the summary. After it the window is small again: the instructions, the tools, one summary and the latest turns. The first invoice with no policy line is the next one the worker decides. Then you read the summary's own text. It has the task, and it does not have "with the policy line for each". The cause is removal (26.1).

Figure 26.2. Tool results fill the window, and the summary empties it. The first invoice without a policy line comes straight after.

What are the parts of the window, and which grow?

Five parts, and only two of them grow. Anthropic's documentation says what counts: "the system prompt, every message in messages (including tool results, images, and documents), and your tool definitions". What the model writes counts too.1 The five are the instructions, the tool definitions, the request, the tool results and the worker's turns.

The instructions and the tool definitions stay the same size. So does the request, until a summary replaces it. The tool results grow with every tool call, and the worker's turns grow with every decision. Sorted into the three kinds, the instructions are rules, the tool results are evidence, and the worker's turns hold the state of the work.

So in a run like Friday's, the part to look at first is the tool results.

Where do the numbers come from?

From the usage field of every response, and from counting tokens for one part. "Every response reports what the request consumed in its usage field."1 Two cases need care.

  • With caching. The cached part of the request (21.12) is the start of the request that the API has already processed. The API charges less to read it again. When it is in use, the input is split across three fields: input_tokens, cache_read_input_tokens and cache_creation_input_tokens. All three count toward the window, so add them. The cache changes what you pay, not how full the window is.1
  • With compaction on. The model call that writes a summary is not in the top-level counts. It is listed under usage.iterations, so add those as well.2

usage gives one total for the request. To split it by part, something must count each part. The API has a way to count the tokens of a request, which the vendor's pages name beside usage.3 In this book's lab, your loop writes the tokens by part into the trace at every turn.

How does a trace show what was cut, and when?

As an event of its own, at the turn where it happened. When the API clears old tool results, its response says which cuts were applied and how many tokens they removed.4 When it summarizes, the summary comes back in the response as a block of its own.2 In this book's lab, your loop writes each of these as its own line in the trace.

Then you read two things. The first is the turn straight after the event. If the check's failures begin there, the cut is the likely cause. The second is what is left. After a summary, the model has the summary's text and the turns after it, so read that text for the rule.

What was cut is not gone from your side. The builder's own copy of the conversation, the array of 21.5, still holds every turn after a clearing and after a summary (26.3). So you can always compare what the model was sent at turn 1 with what it was sent at the failing turn.

Who knows the budget?

You do, and the model does not. Only two models, Claude Sonnet 4.5 and Claude Haiku 4.5, are told how much of their window is left. "No other model receives these tags."1 The lab's model is not one of them. So the worker cannot slow down or tidy up as its window fills, because nothing tells it that it is filling.

The context budget is therefore your number: the most tokens you let one request hold. The vendor's advice is to compare the count "with a limit you choose, below the model's context window."3 It is a second budget, beside the $1.00 that limits what the run may spend.

If nothing acts at your number, the window's own limit does. A request whose input is already too large is refused with a 400 error. If the model reaches the limit while it writes, it stops, and the stop reason, the field that says why the model stopped, is model_context_window_exceeded.1 Your loop ends on that stop (21.3). The budget exists so that a cut you chose comes first.

What can the trace not tell me, and what does reading it cost?

It cannot tell you whether the rule held. A trace shows what ran and what the window held, not whether the answer was right (21.10). On Monday the check by code found the invoices with no policy line. The trace only said where to look for the reason.

Reading by part has a price: something to count each part at every turn, and a longer trace. For the morning triage, a run of a few turns, the total in usage is enough. Tokens by part are worth their cost in a run long enough to be cut.

The operating rule: before you cut anything, read which part is large. After any cut, read the turn that follows it.

Do this now. Ask your coding agent to read one long run. Copy this line:

Read the newest trace in this project. For the first model call and the last one, show the input tokens, and say how much of each is instructions, tool definitions, tool results and the model's own turns. Say what you could not split, and why. Change no file.

Check yourself

Question 1 / 8

0 answered

Which two parts of a worker's window grow over a long run?

  1. Context windows, Anthropic.↑↑2↑3↑4↑5
  2. Compaction at a token threshold, Anthropic.↑↑2
  3. Compaction on demand, Anthropic.↑↑2
  4. Context editing, Anthropic.↑
Owner
Panaversity