TOKENISATION

The prompt fits in characters, and the instruction falls off the front

L215 MINAI SYSTEMS
PREDICT

Owoleaf's support assistant runs on a local Ollama server with a 256-token window (default settings otherwise). The app keeps 64 tokens for the answer and budgets the other 192 as 768 characters, at 4 characters a token. A Yorùbá chat assembles to about 500 characters. What reaches the model?

A model never sees characters. A tokeniser turns text into tokens from a fixed vocabulary, learned by byte-pair encoding: start from the 256 byte values and merge the most frequent adjacent pairs in a training corpus. Text like the corpus becomes a few long tokens. Text it rarely saw stays near raw bytes, and in UTF-8 a Yorùbá ẹ is three bytes, a Hausa ɗ two, an emoji four.

So “a token is about 4 characters” is an English fact. The tokens, context windows and cost chapter gives the real ranges: about 4 characters per token for English prose, 1.5 to 2 for Yorùbá. The window counts tokens, so budget in tokens, keep room for the answer, and choose what to drop yourself.

▶ SIMULATION
OWOLEAF APP
customer language: —
budgets in: —
estimate (chars ÷ 4): —
real tokens sent: —
not budgeted yet
PROMPT, IN ORDER
not assembled yet
MODEL SERVER · 256-TOKEN WINDOW
received: — tokens
dropped from the start: — (no error raised)
room for the answer: — tokens
waiting
✓ the whole instruction reaches the model✓ the customer's question reaches the model✓ prompt tokens + 64 reserved for the answer ≤ 256
SCHEDULE
01[CUSTOMER]customer writes in English and pastes the transfer reference
02[APP]assemble: instruction, 2 help articles, history (oldest first), question
03[APP]budget in characters: estimate chars ÷ 4; drop oldest turns until the estimate ≤ 192
04[SERVER]model server: prompt over 256 tokens? drop the start, keep the last 128
05[MODEL]generate the answer (needs about 40 tokens)
Start of english · char budget scenario. 5 steps.
MAX_CHARS = (WINDOW - RESERVE) * 4          # "a token is about 4 characters"
parts = [instruction, *docs, *history, question]
while sum(len(p) for p in parts) > MAX_CHARS:
    parts.remove(oldest_turn(parts))
prompt = "\n".join(parts)                    # Yorùbá: 600 chars, 300+ tokens
# or: tokens = tok.encode(prompt)[-BUDGET:]   # counts right, cuts the instruction

Watch the three invariants. In YORÙBÁ · CHAR BUDGET the estimate says 130, under the 192 budget; the real count is 279, and the server keeps only the last 128 tokens, so the instruction and both help articles never arrive. TRIM THE FRONT counts correctly and cuts the instruction itself. NO OUTPUT RESERVE fits 232 tokens into 256 and the answer stops after 24. The FIX drops two old turns whole and sends 187 tokens, which leaves 69 for the answer. Read the token row: English words are single tokens; each tone-marked Yorùbá letter is two raw bytes (xc3 xad is í).

BREAK IT NOT YET

This English chat fits: the app budgets 192 tokens as 768 characters, and English runs close to 4 characters per token. Add one more customer message so the character budget still says the prompt fits, and the model server cuts the instruction anyway.

GOAL: the char estimate is ≤ 192 and the instruction did not reach the model

FIX IT NOT YET

Rewrite buildPrompt to return the parts to send, as an array of strings in prompt order: the instruction first, then the articles you keep (in rank order), then the history turns you keep (oldest to newest), then the question last. Rules the grader checks: count(part) summed over the parts you return is at most p.window − p.reserve; the instruction and the question are always sent, unchanged; every part is sent whole or not at all; you drop the oldest turns and the lowest-ranked articles first; and you drop nothing when everything fits, and never leave out a part that would still have fitted. The grader runs English, Yorùbá, a pasted bank SMS, a long history, and a reserve so tight that an article has to go.

What the sim simplifies. The tokeniser is a real byte-level BPE, but it learned its merges from one page of English support text, so it exaggerates the gap: Yorùbá comes out under 1 character per token here, where production tokenisers give 1.5 to 2. Each part is counted on its own; real chat formats add a few tokens per message for role markers. The server trims one long prompt to the last half of its window, which is what current Ollama does (it keeps a few num_keep tokens at the very start, then cuts to about half). Ollama’s chat endpoint first drops whole old messages and keeps system messages; the token trim hits whatever is still too long, such as a single prompt with the documents inside it. That matches the chapter’s measurement of a 900-word prompt arriving as 259 tokens in a 512-token window; the October 2025 log above shows an older build cutting to the full window. The Claude API does not trim: input over the window is a 400 invalid_request_error, “prompt is too long”, and on Claude 4.5 and newer models, input that fits but leaves too little room stops with stop_reason: "model_context_window_exceeded".

Count with the model you will call. For Claude that is Anthropic’s token counting endpoint with the model id; tiktoken is a different model’s tokeniser and gives a different count (the chapter measures the gap). The same count drives the input-token rate limit, which LLM rate limits budgets request by request. Normalise text to NFC before you count, because decomposed text is more bytes: à is 2 bytes composed and 3 decomposed. The API chapter applies the same budget to chat history and explains why you drop old turns in chunks when you use prompt caching: prompt cache prefix shows how one changed byte near the top makes the whole prompt miss. A message that alone is bigger than the budget, like a pasted statement, needs its own rule: cap it at input, or cut inside it and say so.

Mental model:

The window is measured in tokens of this model’s tokeniser, and string length is a guess that is only right for English. Count before you send, reserve room for the answer, and when something must go, drop whole parts from the middle: oldest turns and weakest retrieved chunks. The instruction and the question are never the part that gets cut. If you do not choose what to drop, the server chooses the start.

IN PRODUCTION

Support bots for Yorùbá, Hausa, Igbo or Pidgin speakers; RAG pipelines on a local model with Ollama's default 4,096-token window on smaller GPUs; agents whose tool results paste logs, JSON, numbers or base64; any code that does text[:8000] before an API call. Symptoms: the model ignores its system prompt for some users only, answers in the wrong language, forgets a rule it followed in testing, or a JSON answer stops halfway with stop_reason at the limit.

IN INTERVIEWS

“Your RAG answers are fine in English and wrong in another language. What do you check?” Count tokens per request by language, not characters; check whether the server truncated (Ollama's prompt_eval_count against your count, or the warning in its log). Then the fix: a token budget with the model's tokeniser, output reserved, the instruction and question pinned, and whole oldest turns and lowest-ranked chunks dropped first. Mention the Claude API's 400 for oversize input and the cost side: the same chat costs more in Yorùbá.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: LLM RATE LIMITS →