Tokenization & Context Windows
Understand tokens and context windows: how BPE tokenization works, why code costs more tokens, managing the context budget, and the token math behind LLM cost — for Java developers.
On this page
Tokens are the currency of LLMs — the unit models read, generate and bill for. Understanding tokenization and the context window explains cost, informs how you budget a prompt, and prevents a class of production failures. This tutorial covers both for a working engineer.
Key Takeaways
- A token is ~¾ of an English word; models process and bill per token.
- Code and non-English text tokenize less efficiently — more tokens per character.
- The context window is shared by system prompt, history, retrieval and response — budget it.
- Cost is per token, input and output priced separately; input often dominates.
What a token is
Tokenizers split text into sub-word units using an algorithm like Byte-Pair Encoding (BPE), which learns common character sequences from training data. Frequent words become single tokens; rare words split into pieces.
"tokenization" → ["token", "ization"] (2 tokens)
"the" → ["the"] (1 token)
"antidisestablishmentarianism" → many tokens (rare, fragments heavily)A rough rule for English prose: 1 token ≈ 0.75 words, or about 4 characters. But it is only a rule of thumb — the real count depends on the specific text and tokenizer.
Why code costs more tokens
Tokenizers are trained predominantly on natural language, so code fragments more:
// This innocuous line is more tokens than its length suggests:
Map<String, List<Integer>> resultsByCategory = new HashMap<>();
// The generics, camelCase, and punctuation each split into multiple tokens.Counting tokens accurately
Do not guess when it matters — count with the model's tokenizer:
Tokenizer tokenizer = new OpenAiTokenizer("gpt-4o-mini");
int promptTokens = tokenizer.estimateTokenCountInText(prompt);
// Use this to keep prompts within budget and to predict cost before the call.
if (promptTokens > MAX_PROMPT_TOKENS) {
prompt = truncateToTokens(prompt, MAX_PROMPT_TOKENS, tokenizer);
}The context window is a shared budget
The context window is the total tokens the model can consider at once, and everything shares it:
If their sum exceeds the window, you get truncation or an error. Budget each component:
- System prompt — fixed; keep it focused.
- History — bound the chat memory.
- Retrieved documents — limit top-k and chunk size.
- Response — reserve space with a max-tokens cap.
Long context is not a dumping ground
Models now offer very large context windows, which tempts you to stuff everything in. Resist it, for three reasons: cost and latency both rise with context length, and models attend less reliably to information buried in the middle of a very long prompt (the "lost in the middle" effect). Retrieving the relevant few thousand tokens beats sending a hundred thousand — see transformer architecture for why.
The cost math
Cost is per token, with input and output priced separately:
// Prices per 1M tokens, input and output differ. Keep them in config.
double cost = (promptTokens / 1_000_000.0) * inputPricePerM
+ (completionTokens / 1_000_000.0) * outputPricePerM;A key insight: input tokens often dominate. A long system prompt, accumulated history, and retrieved context are sent on every call, while the response is usually shorter. This is why unbounded chat memory is a cost problem, not just a context one — you pay for the whole history every turn. Track token cost per feature to see where the money goes; see Spring AI observability.
Practical token discipline
- Count tokens with the real tokenizer when budgets are tight.
- Bound history, retrieval and response length so their sum fits comfortably.
- Prefer focused retrieval over long context — cheaper, faster, more reliable.
- Track cost per feature; input tokens are usually the larger line.
- Remember code and non-English text cost more than their length suggests.
Next
Frequently Asked Questions
What is a token in an LLM?
Why does code use more tokens than prose?
How do I manage the context window?
How do I calculate LLM cost?
Related tutorials
- Prompt Engineering MasterclassAdvanced prompt engineering techniques: prompt chaining, meta-prompting, self-consistency, structured reasoning and prompt optimization — beyond the basics, for reliable production prompts.
- LLM Evaluation & BenchmarksHow to evaluate LLMs and LLM applications: what public benchmarks like MMLU and HumanEval measure, their limits, and building a custom evaluation suite that reflects your real task.
- Embedding Models & Semantic SearchHow embedding models power semantic search: bi-encoders vs cross-encoders, re-ranking, hybrid search combining keywords and vectors, and choosing embeddings for retrieval quality.
- Model Distillation & QuantizationMake models smaller and faster: quantization (GGUF, GPTQ, AWQ), knowledge distillation, the accuracy-vs-efficiency trade-off, and when self-hosting a compressed model makes sense.