Under the Hood: Tokens, Context & Cost
How tokenisation works, what a context window actually costs you in money and accuracy, and the arithmetic that makes AI spending predictable.
What you'll be able to do
- Estimate the token count and cost of a request before you send it
- Explain why output tokens dominate the bill on most workloads
- Position content inside a long prompt so the model actually uses it
Assumes: Beginner tier, or equivalent familiarity with chatbots
Tokens are the unit of everything
Cost, speed, context limits and rate limits are all denominated in tokens. Getting an intuition for them makes the rest of applied AI legible.
A tokeniser splits text into sub-word pieces chosen so that common sequences get one token and rare ones get several.
| Input | Tokens | Why |
|---|---|---|
the | 1 | Extremely common |
tokenisation | 3β4 | Split into fragments |
def get_queryset(self): | 7β8 | Punctuation costs separately |
Ω
Ψ±ΨΨ¨Ψ§ Ψ¨Ψ§ΩΨΉΨ§ΩΩ
| 8β12 | Scripts under-represented in training fragment heavily |
Working figures: ~0.75 words per token in English; ~4 characters per token. Code runs denser than prose because of punctuation and indentation.
The language asymmetry is not a footnote β the same meaning can cost two or three times more in some languages than in English. If you are building for a non-English audience, measure it rather than assuming.
Do not estimate when it matters. Every provider ships a token counter, and there is a counting endpoint on the major APIs. Use it before you ship anything with a budget attached.
The cost equation
Almost all pricing follows one shape:
Two structural facts hold across current model families, and they matter more than any specific price:
- Output costs roughly 5Γ input. Across current tiers the ratio sits near five to one. Generated text is the expensive half.
- Tiers are separated by roughly an order of magnitude. A small fast model against a frontier model is typically a 5β10Γ difference per token.
Specific prices move constantly, so build the habit of reading them from the providerβs pricing page rather than memorising a table. As a reference point at the time of writing, a frontier model sits around $5 per million input tokens and $25 per million output; a small model is a few times cheaper again.
Where the money actually goes
Two consequences follow directly, and they surprise people:
Long outputs cost more than long inputs. A 500-token reply can outweigh a 2,000-token prompt. βAnswer in three sentencesβ is a cost control, not just a style note.
Repeated prefixes are the biggest avoidable waste. A chat app that resends a 4,000-token system prompt on every turn pays for it every turn. Which brings us to caching.
Prompt caching
If a large prefix of your request is byte-identical to the last one, providers can serve it from cache. The economics are dramatic: cached reads cost roughly a tenth of normal input tokens, against a one-off write premium of about 1.25Γ to put it there.
The catch is that caching is a prefix match. Any byte that changes invalidates everything after it. So the architectural rule is:
Stable content first. Volatile content last.
Put the frozen system prompt and fixed tool definitions at the front; put timestamps, request IDs and the userβs actual question at the end. A datetime.now() at the top of a system prompt silently destroys the cache on every single request, and nothing in the response will tell you β you have to look at the cache-hit figures in the usage data.
The context window, and its two limits
Frontier models now reach around a million tokens; cheaper tiers commonly sit at 200K. That is a hard ceiling on everything visible at once β instructions, history, uploads, and the reply.
But the hard limit is rarely the real one. Two softer limits bite first:
Attention is uneven. Recall on a specific fact is strongest near the beginning and end of a long context and measurably weaker in the middle. Fill 800K tokens and you are not getting 800K tokens of reliable recall.
Latency and cost scale with what you send. Every token is processed on every request. A million-token prompt is slow and expensive whether or not the model needed all of it.
The practical consequence: a big window is not a substitute for retrieval. Selecting the right 5,000 tokens beats dumping 500,000 β it is faster, cheaper and more accurate. That is the entire argument for RAG, which is the next lesson but one.
Managing long conversations
Three approaches, in increasing sophistication:
- Truncate β drop the oldest turns. Trivial, and it loses things you needed.
- Summarise β periodically compress old history into a short digest. Cheap and effective; most production chat does this.
- Retrieve β store history externally and pull back only the relevant parts. Best quality, most machinery.
Practical checklist
- Measure tokens with a real counter before shipping anything with a budget.
- Constrain output length explicitly β it is the expensive half.
- Order prompts stable-first so caching can work, then verify cache hits in the usage data.
- Put critical instructions at the very start or very end of a long prompt.
- Route simple tasks to a small model; reserve the frontier tier for work that needs it.
Try this: Take a prompt you use often, count its tokens, and work out what it would cost at 1,000 requests a day. Then split it into a frozen prefix and a variable suffix and recompute with cached reads at a tenth of the price. The gap is usually large enough to change how you write prompts.
Go deeper
Quick Quiz
Test what you just learned. Pick the best answer for each question.
Q1 On a typical chat workload, which usually dominates the bill?
Q2 You send the same 8,000-token system prompt on every request. What helps most?
Q3 Why can burying a key fact mid-way through a long prompt hurt?
Q4 Which change reduces cost without touching output quality?