# Tokens, turns and context

Learn what a token, a turn and the context window are, and why every model call re-reads the whole conversation, so a short late message is not a cheap one.

> You pay in tokens, small pieces of text, for everything the model reads and writes. Each call re-reads the whole context, so cost depends on how much the conversation holds, not on how short your latest message is.

Source: https://ai-sw-factory.mellicci.dev/cost/tokens-turns-and-context

**Diagram:** Within one reply, every call to the model re-reads the whole context again, including what the previous call added.

- Your short prompt — about 40 tokens
- → added to
- Earlier conversation — tens of thousands of tokens
- → all of it
- Model reads it all — call 1, then a tool call
- → result appended
- Re-reads the whole context — call 2, read again
- → then
- Answer — one reply

When you use a coding agent you are billed in **tokens**, and the bill is driven far more by what the model *reads* than by what you *type*. The [loop from Module 1](https://ai-sw-factory.mellicci.dev/fundamentals/how-coding-agents-work) explains why: the model decides, the harness runs the tool, and the result goes back to the model for another call.

This page gives you the four words you need for everything else in this module: token, prompt, reply and context. Together they lead to one fact that explains most surprising bills.

## Why it matters

A token is not a word. It is a few characters or a word fragment, so a short sentence is a dozen or two tokens, and a thousand words of prose is on the order of 1,300-1,500 tokens. Code and unusual formatting usually tokenize a little less efficiently than plain prose, so a file costs somewhat more than its word count suggests.

The key fact is this: **every call re-reads the whole context.** The model keeps nothing between calls; the harness sends the full conversation each time. So a one-line message late in a long session is not a cheap message. You pay to read the line and everything before it.

If you do not know this, you judge cost by the size of your own messages and look in the wrong place.

## How it works

Four terms, in the order they nest:

| Term | What it is | Why it matters for cost |
|---|---|---|
| Token | A few characters or a word fragment | The unit everything is counted and billed in |
| Prompt | What you send on a turn | Usually the smallest part of what is read |
| Reply (turn) | The prompt plus the model's entire answer, however many tool calls happen inside it | The unit cost is attributed to; three tool round-trips are still one reply |
| Context window | Everything the model sees on a call | Re-read on every call, so it sets the cost floor of each one |

A reply is made of one or more calls. Each tool call ends one model call, and the tool result starts the next, with the context now a little larger.

## Example

The `ticket-service` team (a TypeScript API, four developers, each with a coding agent) saw agent spend double last month. One developer, deep into a long session of about 60,000 tokens, types a 40-token message asking to fix a failing validation test. The reply takes four calls: read the test, read the source file, run the test, then answer. The numbers are illustrative.

| Call | What it adds to the context | Tokens read on this call | Share that is new |
|---|---|---|---|
| 1 | Your message (40) | 60,040 | under 0.1% |
| 2 | Call 1's tool request and the test file (1,350) | 61,390 | about 2% |
| 3 | Call 2's request and the source file (3,200) | 64,590 | about 5% |
| 4 | Call 3's request and the test output (950) | 65,540 | about 1.5% |
| **Reply total** | | **about 251,500** | |

Your 40 tokens became roughly 250,000 tokens of reading, because the same 60,000 were read four times. The agent's own output is small by comparison. On a warm session most of that re-read is served from the cache at a much lower price ([prompt caching](https://ai-sw-factory.mellicci.dev/cost/prompt-caching)), but it is still read and still billed, on every call. A fresh session for the same task would have read a small fraction of it.

## What helps, what hurts

What helps:

- Judge cost by the size of the context, not of your message. This follows from every call re-reading everything.
- Start a fresh session for an unrelated task, so the re-read floor starts low. [Context growth](https://ai-sw-factory.mellicci.dev/cost/context-growth) covers this and the other levers.
- Do the work in fewer calls where you can, for example by asking for a whole change instead of one edit at a time. Each extra call re-reads everything again.

What hurts:

- Keeping one session open across unrelated tasks, because all earlier material is re-read on every call.
- Treating "it was only a one-line follow-up" as cheap.
- Counting turns instead of calls. A reply with ten tool calls costs far more than a reply with one, even though both are one turn.

## Trade-offs

- A fresh session is cheaper to read but has forgotten everything. You pay to rebuild the context the work needs, with notes or [instruction files](https://ai-sw-factory.mellicci.dev/fundamentals/instruction-files).
- Fewer, larger requests cut re-reads but give you fewer chances to steer, so a wrong turn costs more before you see it.
- A long context is not always waste. Earlier material the task needs is what makes the model's answer good.

## Related

- [How coding agents work](https://ai-sw-factory.mellicci.dev/fundamentals/how-coding-agents-work) - the loop that makes several calls per reply.
- [Why tokens cost differently](https://ai-sw-factory.mellicci.dev/cost/why-tokens-cost-differently) - the same token can have different prices.
- [Prompt caching](https://ai-sw-factory.mellicci.dev/cost/prompt-caching) - what makes the re-read cheaper.
- [Context growth](https://ai-sw-factory.mellicci.dev/cost/context-growth) - why each reply gets dearer as a session goes on.

## Key terms

- **Token** - the unit a model is billed in; a few characters or a word fragment.
- **Prompt** - what you send in on a given turn.
- **Reply** - also called a turn; the prompt plus the model's whole answer, however many tool calls.
- **Call** - one request to the model; a reply can contain several.
