Riven

Context Window Reference: Where the Tokens Actually Go

A context window is the maximum number of tokens a model can attend to in one request. It is shared by the system instructions, the conversation history, any tool or retrieval output, and the reply you want back. Remaining budget equals the context limit minus the prompt tokens minus the reply reserve. Requests that exceed the limit are truncated or rejected, so a budget is a correctness control, not just a cost control.

The formula

Remaining tokens equal the context limit, minus everything already in the prompt, minus the reserve you leave for the reply. Utilisation is prompt tokens divided by the context limit.

The reserve is the part most often forgotten. If you fill the window with retrieved documents and leave nothing for the answer, the model truncates mid-sentence or the request fails. Reserve at least the expected answer length plus headroom for reasoning.

What competes for the window

Four things share the budget: the system prompt, the conversation so far, any tool results or retrieved passages, and the reply. The system prompt is fixed and usually small; conversation grows monotonically; retrieval is the most variable and the most common cause of overflow.

Because conversation history only grows, a long session eventually fails no matter how large the window. The fix is summarisation or eviction of old turns, decided before the limit is reached rather than after a failed request.

Sizing a request before sending it

Estimate tokens from characters rather than words when the input is code or structured data, because whitespace and punctuation tokenise densely. English prose runs near four characters per token; JSON and identifiers run closer to two to three.

Measure once per document type and keep the ratio. Sampling a real payload is worth more than any general rule: tokenise one representative document, divide characters by tokens, and use that factor for everything of the same shape.

Token estimation factors by input shape (measure your own to confirm)

Input shapeApprox chars per tokenApprox tokens per 1,000 chars
English prose4.0250
Technical prose with numbers3.5286
JSON / structured data2.5400
Source code3.0333
Non-Latin script1.5667

Worked budget at a 128,000 token window

ComponentTokensRunning remaining
Context limit128,000128,000
System prompt1,200126,800
Conversation history18,000108,800
Retrieved documents60,00048,800
Tool results8,00040,800
Reply reserve8,00032,800 spare

When the window becomes the constraint

SymptomLikely causeControl
Answers cut mid-sentenceNo reply reserveReserve output length before filling
Late-session failuresHistory grows unboundedSummarise or evict old turns
Retrieval overflowToo many passages injectedRank and cap passages, not top-k blindly
Cost rising faster than usagePrompt re-sent every turnPrompt caching, or trim history

Frequently asked questions

Does a larger context window cost more?

Per request, yes: most providers bill input tokens, so a fuller window costs more. A larger window does not by itself raise the per-token rate; what raises the bill is sending more tokens per call.

Can I just truncate old messages?

Truncation works but loses information abruptly. Summarising evicted turns preserves the decisions and drops the verbatim text, which is usually the better trade for long sessions.

More in tools

Sources and read dates

Related reading