# 29.1 Tokens, context windows and why output varies (/building-ai-workers/model-selection-and-cost/tokens-and-why-output-varies)

---
type: Document
title: "29.1 Tokens, context windows and why output varies"
description: "What a token is, why the same text counts differently on another model, what fills the context window, and why acceptance is a rate over several runs."
status: stable
order: 329.01
ksor:
  owner: team:panaversity
  audience:
    - public
  approval:
    by: process:panaversity
    at: 2026-11-30T10:58:07Z
chapter: '29'
part: III
expert_status: manager
concepts: [ "29.1" ]
last_verified: '2026-11-30'

sources:
  - id: anthropic-cost-and-intelligence
    title: Optimizing for cost and intelligence, Anthropic (verified 2026-11-30)
    resource: https://platform.claude.com/docs/en/about-claude/models/optimizing-for-cost-and-intelligence
  - id: anthropic-pricing
    title: Pricing, Anthropic (verified 2026-11-30)
    resource: https://platform.claude.com/docs/en/about-claude/pricing
  - id: anthropic-models
    title: Models overview, Anthropic (verified 2026-11-30)
    resource: https://platform.claude.com/docs/en/about-claude/models/overview
  - id: anthropic-token-counting
    title: Token counting, Anthropic (verified 2026-11-30)
    resource: https://platform.claude.com/docs/en/build-with-claude/token-counting
  - id: anthropic-migration-guide
    title: Migrating to Claude Opus 5.5, Anthropic (verified 2026-11-30)
    resource: https://platform.claude.com/docs/en/models/opus-5-5/migration-guide
  - id: anthropic-context-windows
    title: Context windows, Anthropic (verified 2026-11-30)
    resource: https://platform.claude.com/docs/en/build-with-claude/context-windows
  - id: anthropic-thinking
    title: Building with extended thinking, Anthropic (verified 2026-11-30)
    resource: https://platform.claude.com/docs/en/build-with-claude/thinking
  - id: anthropic-api-2026-10-21
    title: Migrating to API version 2026-10-21, Anthropic (verified 2026-11-30)
    resource: https://platform.claude.com/docs/en/api/migration-2026-10-21
  - id: anthropic-glossary
    title: Glossary, Anthropic (verified 2026-11-30)
    resource: https://platform.claude.com/docs/en/about-claude/glossary
generated:
  at: 2026-11-30T10:58:07Z
  by: esl-rewrite/1.2.0+ksor.1
trust_tier: unverified
build_id: sha256:d6ce3f28a2cf8e492a60bae1b426b7cc6466e35e83d70c3b9763db2c55009f4b
dirty: true
ksor_version: 0.0.60
---

**In everyday life.** Two market stalls sell rice by the cup. One stall's cup is smaller, so the same sack is more cups there. A lower price per cup tells you little until you count how many of that stall's cups the sack fills. At home, a cook makes the same rice dish from the same recipe every day, and no two pots come out exactly alike. To know whether the cook is good, you do not taste one pot. You count how many pots in a week you would serve to guests. In this section, the cup is a token, the stall is a model, and a pot you would serve is an accepted result.

**What you will learn.** You practice this in Lab 29, Task 1, where you price the week from its token counts. You practice it again in Task 4, where each case runs three times.

- What is a token, and why does the same text count differently on another model?
- How do you count tokens before you send a request?
- What counts toward the context window?
- Why can the same request pass the gates one run and fail the next?
- Why is acceptance a rate, and not one run?

**Why it matters.** You pay for every run, and only some runs are accepted. If you do not know why output varies, a lucky run looks like a saving, and an unlucky one looks like a broken model.

**At work.** Tuesday, May 11, 9:30 a.m. Before you divide anything, you read two lines of `usage/week.jsonl`. Both are extraction calls, for two invoices of about the same length, on the same model and the same prompt. Their output token counts differ. In the week's suite results, one extraction case did not pass all three of its trials.

<!-- OPEN: recorded run. The two lines' output tokens, and the suite case that did not pass all three trials, come from the recorded week. -->

### What is a token, and why does the same text count differently on another model?

**A token is a piece of text, and each model cuts text into tokens in its own way.** You met tokens in Chapter 0.1. A model reads and writes tokens, not letters, and everything is counted in them ([0.1.2](../../how-ai-works/what-ai-actually-is/why-it-behaves.md)). The price is per million tokens, with one price for input and another for output. On Sonnet 5.5, input costs $2 and output $10 per million tokens.[^anthropic-pricing] Output costs five times input on Claude's current models, so a long answer costs more than a long question.[^anthropic-cost-and-intelligence]

The part that cuts text into tokens is the **tokenizer**. Claude's models from 4.7 on use a newer tokenizer that "produces approximately 30% more tokens for the same text".[^anthropic-token-counting] The Opus 5.5 migration guide says "up to about 35 percent more, varying by content".[^anthropic-migration-guide] Sonnet 4.6 and earlier use the older one.[^anthropic-pricing]

So a price per token is not the price of a page. Here is the trap, with example numbers, not from a recorded run. Sonnet 4.6 charges $3 per million input tokens, and Sonnet 5.5 charges $2.[^anthropic-pricing] That looks like a third off. But the same text that was 10,000 tokens on Sonnet 4.6 may be about 13,000 on Sonnet 5.5, 30% more. Price both:

- Sonnet 4.6: 10,000 × $3 ÷ 1,000,000 = $0.030.
- Sonnet 5.5: 13,000 × $2 ÷ 1,000,000 = $0.026.

The saving is $0.004 of $0.030, about 13%, not 33%. And it could be smaller still, because the extra tokens vary by content.

### How do you count tokens before you send a request?

**With the token counting endpoint, on the model you plan to use.** Anthropic's advice is plain: "Recount prompts against the model you plan to use rather than reusing counts measured against earlier models."[^anthropic-token-counting]

Counting is free, and it has its own rate limits, apart from the limits on sending requests. The count is an estimate, and it does not apply caching.[^anthropic-token-counting] So it tells you how big a request is, not what it will cost after the cache. The usage on the response is the true count.

### What counts toward the context window?

**Everything in the request, and the answer too.** The context window is everything one request holds, and the most it can hold ([0.1.2](../../how-ai-works/what-ai-actually-is/why-it-behaves.md)). On the Messages API it holds the system prompt, every message and the tool definitions. The messages include tool results, images and documents. It also holds the turn's own output, thinking included.[^anthropic-context-windows]

The extraction shows why this matters for cost. Its second request carries everything in the first: the tools, the rules, the task, the `get_invoice` call, and the invoice as a tool result. So the second request's input is larger than the first, for every invoice. Cache breakpoints save the cost of that repeated start ([29.7](caching-by-how-often-content-changes.md)).

Claude's current models hold 1 million tokens, "roughly 555k words" on the current tokenizer.[^anthropic-models] A request can write at most 128,000 tokens of output.[^anthropic-context-windows] On most current models, a long request costs the same per token as a short one. Haiku 5.5 is the exception: over 100,000 input tokens, the whole request pays higher prices.[^anthropic-pricing]

### Why can the same request pass the gates one run and fail the next?

**Because the model samples each token with some chance, and no setting turns that off.** You saw this in Chapter 0.1. For each token, the model scores the possible next tokens and picks one, with some chance in each pick. That pick is called **sampling** ([0.1.1](../../how-ai-works/what-ai-actually-is/the-machine.md)). So the same request can come back in different words. For the extraction, a different word can be a different value, and one of the three gates can refuse it.

Older code used **sampling settings**, such as `temperature`, to make answers less varied. Today they are gone. On API version `2026-10-21`, sending `temperature`, `top_p` or `top_k` returns an error with status 400 on any model, "even at the parameter's default value".[^anthropic-api-2026-10-21] You met that 400 in [21.9](../agent-loop-by-hand/when-the-loop-goes-wrong.md). On older API versions, Claude's current models also return a 400 for any value but the default, and for `temperature` and `top_p` sent together.[^anthropic-thinking]

Even the old setting never made answers repeat. Anthropic's glossary says: "Even with temperature set to 0, the results will not be fully deterministic."[^anthropic-glossary] Deterministic means that the same input always gives the same output. So variation is not a fault to tune away. You live with it in three ways. A schema narrows what the answer may be (Chapter 25). Gates catch answers that vary too far. And a suite measures how often that happens.

### Why is acceptance a rate, and not one run?

**Because one run cannot show how often a request passes.** On the suite, you measure it with trials. In the live week, you measure it over many invoices: how many were accepted, out of how many were sent.

That is why cost per accepted result is a rate too. Every run is paid for, and only the accepted ones count. Here is the method, with example numbers, not from a recorded run. Suppose one extraction, both requests together, reads 5,930 input tokens and writes 1,000 output tokens on Sonnet 5.5. This book uses that same example invoice again in 29.3 and [29.9](token-budgets-and-a-saving.md). It costs 5,930 × $2 ÷ 1,000,000 + 1,000 × $10 ÷ 1,000,000 = $0.01186 + $0.01000 = $0.02186. If 100 invoices cost $2.19 and 95 were accepted, the cost per accepted result is $2.19 ÷ 95, about $0.023. The five invoices stopped at a gate were paid for, and appear in the 7:10 report for Ana.

**The operating rule:** count tokens on the model you will use. Judge any change by the share of runs accepted over many runs, never by one run.

**Do this now.** Ask your coding agent to show you the variation in your own worker. Copy this line:

> Find the usage of the last 20 model calls this project made for one step. For each call, show the input tokens and the output tokens. Then tell me the smallest and the largest output count, and whether the requests were the same apart from their data. Change no file.

[^anthropic-pricing]: Pricing, Anthropic, read November 30, 2026.
[^anthropic-cost-and-intelligence]: Optimizing for cost and intelligence, Anthropic, read November 30, 2026.
[^anthropic-token-counting]: Token counting, Anthropic, read November 30, 2026.
[^anthropic-migration-guide]: Migrating to Claude Opus 5.5, Anthropic, read November 30, 2026.
[^anthropic-context-windows]: Context windows, Anthropic, read November 30, 2026.
[^anthropic-models]: Models overview, Anthropic, read November 30, 2026.
[^anthropic-api-2026-10-21]: Migrating to API version 2026-10-21, Anthropic, read November 30, 2026.
[^anthropic-thinking]: Building with extended thinking, Anthropic, read November 30, 2026.
[^anthropic-glossary]: Glossary, Anthropic, read November 30, 2026.
