C.W.K.
Stream
Lesson 01 of 10 · published

Context Window Math — Tokens, Order, and What Stays

~18 min · context, tokens, math

Level 0Apprentice
0 XP0/100 lessons0/14 achievements
0/120 XP to next level120 XP to go0% complete

Tokens are the unit, not characters

Every model bills, limits, and reasons in tokens. A token is roughly ¾ of an English word — but it varies by language (Korean is dense, code is dense in different ways) and by tokenizer. Anthropic, OpenAI, and Google all use different tokenizers; the same English paragraph occupies a different number of tokens in each. Pretend they're the same and your context-window math will be off by 20-30%.

Three numbers to know about your model

  • Total context window — input + output. Claude Opus 4.7 has a 1M-token mode; OpenAI GPT-5.5 and Gemini 2.5 Pro are at 200k–2M depending on tier.
  • Output cap — practical max for a single response, often much smaller than the context window.
  • Effective attention budget — past a certain length, attention degrades. The model can technically read 1M tokens, but it doesn't attend to the middle equally well. Long-context limits is a real concern, covered in lesson 7.

Order is part of the prompt

Models pay disproportionate attention to the start and end of the context — the lost-in-the-middle effect. Put your instruction at the top, the most important evidence at the top of the document block, and a short reminder of the instruction at the bottom for very long contexts.

Code

Counting tokens for a prompt·python
import anthropic

client = anthropic.Anthropic()
resp = client.messages.count_tokens(
    model="claude-opus-4-7",
    system=open("prompts/agent.md").read(),
    messages=[{"role": "user", "content": user_text}]
)
print(resp.input_tokens, "input tokens")
Sandwich the instruction in long contexts·markdown
# Top of context — full instruction
[5-clause contract]

# Documents
[hundreds of thousands of tokens]

# Bottom of context — short reminder
Return JSON matching the schema above. Cite each claim with a doc id.

External links

Exercise

Pick a prompt of yours. Count its tokens with the provider's official tokenizer. Compare to what you would have estimated by word count. Which way did you err?

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.