Text becomes tokens
Before a text model processes a request, a tokenizer maps the text to token IDs. The divisions depend on its vocabulary, algorithm and preprocessing. A token may cover a word, part of a word, punctuation or whitespace. Capitalization and surrounding spaces can change the result. Different tokenizers use different methods. For example, tiktoken uses byte-pair encoding, while Hugging Face supports BPE, Unigram, WordPiece and other models. Tokenizer methods.
For a rough English estimate, one token is about four characters or three quarters of a word. That makes 500 words roughly 667 tokens. Actual counts vary, so measure the text that your application will send. English estimates and counting.
Measure the languages your users use
The same meaning can produce different counts across languages and encodings. This example counts two specific strings using the named cl100k_base encoding. Install tiktoken before running it. The encoding is an example for comparison. Select the tokenizer for your production model when estimating its inputs. tiktoken documentation.
import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
en = (
"Hello, how are you? "
"I need help with my order."
)
hi = (
"नमस्ते, आप कैसे हैं? "
"मुझे अपने ऑर्डर में मदद चाहिए।"
)
print(len(enc.encode(en)), len(enc.encode(hi)))
# 13 52The local measurement with tiktoken 0.14.0 gives 13 and 52 tokens. These are plain-text counts for those exact strings. API message formatting, tools and generated output add their own usage. This one comparison cannot establish the cost ratio for an entire Hindi support service. Measure representative messages, replies and conversation lengths in each language, then apply the chosen model’s input, output and cache rates.
Code, JSON, numbers and whitespace can also produce different token patterns. Inspect the actual input before estimating capacity or cost from word count. When an application transforms or retrieves text, count the final request it constructs.
Planning a context budget
Read the chosen model’s documented input, output and context limits. For Claude, the context includes the request and the generated output, with model-specific rules for thinking and previous turns. Gemini’s model metadata exposes separate inputTokenLimit and outputTokenLimit fields. Allow room for the response and any reasoning tokens that count toward the relevant limit. Claude context rules, Gemini model limits.
An illustrative request budget might contain a 1,000-token system prompt and tool definitions, 6,000 tokens of retrieved documents and 3,000 tokens of conversation history. That is 10,000 input tokens. Reserve another 500 output tokens for this example, and verify the complete request against the selected model’s limits.
At illustrative rates of $3 per million input tokens and $15 per million output tokens, the example costs $0.03 + $0.0075 = $0.0375. Ten thousand such requests cost $375, before other charges. These figures assume all input uses that input rate. Cache pricing, reasoning output and other features can change the bill. Use current prices and measured usage for a real forecast.
Using a large window well
If your program resends an ever-growing history, each later request contains more input. Under a simplified model that adds the same number of tokens each turn, resends all previous turns and uses a constant uncached rate, cumulative input cost grows quadratically with the number of turns. Trimming or summarizing changes that growth. Prompt caching can change the price of repeated input while those tokens still occupy context. Measure the approach you use. Prompt caching.
Evaluate retrieval with the actual model and task. The 2023 Lost in the Middle study found position-sensitive performance on its question-answering and key-value retrieval tests, often with lower accuracy when relevant information was in the middle. This is a dated empirical result. Test your own documents, distractors and answer checks before deciding how much context to include. Study and test details.
Count before you send
Use a model-compatible local tokenizer when available. A provider’s token-counting endpoint can estimate a complete request, including supported messages and tools. For example, Claude provides a token-counting API. Input counts are estimates of the request and do not predict generated output. Compare them with returned usage. Claude token counting.
Check request size and the output allowance before generation. If the request is too large, deliberately select, split or summarize the material and verify that the necessary information remains. Report the problem clearly when the task cannot fit. Avoid silently dropping the end of a document.
Check your understanding
A local measurement with cl100k_base gives 13 tokens for the exact English string and 52 for the exact Hindi string in this lesson. What does this establish?
Pick the one you would defend. Your answer stays private.
Your place and first choices stay on this browser. Classroom credit is recorded separately.
Every lesson here is open. Your progress is recorded once you pass the Python, from Zero, for AI exam.
Stuck on something?Ask Sayali, your AI tutor