MSO is the name of this module: Models, Sampling, and Operations/Optimization. It covers how Claude reads text (tokens and the context window), how it chooses each next token (sampling and non-determinism), which model, reasoning mode and prompting mode to use, and how to call it (SDK, REST or batch).
1.1 Tokens
Everything Claude processes is tokens, not characters or words. Understanding tokens is the foundation for reasoning about cost, speed, and context limits.
What a token is. Claude uses a BPE (Byte Pair Encoding) tokeniser. Common English words are usually one token. Uncommon words, code symbols, and non-Latin scripts are split across multiple tokens.
| Content type | Approximate token count |
|---|---|
| 1 average English word | ~1.3 tokens |
| 1 line of Python | ~5–12 tokens |
| 1 character in Chinese/Japanese/Korean | ~2–3 tokens |
| 1 JSON object with 5 fields | ~20–40 tokens |
| 1 image (any size) | 1,000–3,000 tokens (detail-dependent) |
Structured output costs more than prose. A JSON envelope around a response uses tokens for {, ", :, } at every level of nesting. This matters at scale.
Counting tokens programmatically. Use the SDK's token counter before sending expensive requests:
import anthropic
client = anthropic.Anthropic()
response = client.messages.count_tokens(
model="claude-sonnet-5",
system="You are a helpful assistant.",
messages=[{"role": "user", "content": "Explain quantum entanglement."}]
)
print(response.input_tokens) # e.g. 21
1.2 Context Window
The context window is the total number of tokens Claude can hold in one conversation — input and output combined. It is a shared resource.
The key constraint. When you call the API, you specify max_tokens for the output. The model will generate at most that many tokens. The input (system prompt + conversation history) plus the output must fit in the context window:
input_tokens + max_tokens ≤ context_window_size
If your input is 180K tokens and you request max_tokens=32768, you need a 212K+ context window.
KV cache and fill order. The model maintains a Key-Value (KV) cache of previously computed token representations. This is what makes multi-turn conversations efficient — tokens that were already processed don't need to be recomputed. However, the cache is bounded by the context window. When the conversation grows past the limit, you must decide what to drop.
Fill order strategy. Place content in this order for maximum cache efficiency:
- System prompt (stable — cache this first)
- Static documents or reference material
- Conversation history (older turns first)
- Current user message (dynamic — at the end)
Stable content at the front gets cached and reused. Dynamic content at the end avoids invalidating the cache.
Do not prepend dynamic content (timestamps, user IDs) to your system prompt. It invalidates the cache on every request and eliminates the cost savings from prompt caching.
1.3 Sampling and Non-Determinism
Claude generates tokens probabilistically. The same prompt will produce different outputs on different runs — this is non-determinism, and it is fundamental to how the model works.