How it works
The explanation, with examples you can run.
Tokens, not words
Before the model sees your text, a tokenizer splits it into pieces from a fixed vocabulary, usually tens of thousands to a few hundred thousand entries. Common words are often one token; rare words, names, code identifiers and long numbers split into several. The vocabulary is learned from training text (byte pair encoding and its relatives), which is why the same meaning can cost noticeably more tokens in some languages than others.
Tokens matter to you because they are the unit of everything practical: context limits, output limits, latency and price are all counted in tokens. A rough rule for English prose is a few characters per token, but treat that as a guess and use the provider's token-counting tool when a budget matters.
The toy tokenizer below uses greedy longest match against a tiny vocabulary and falls back to single characters. Real tokenizers are trained rather than hand-written, but the effect is the same: familiar text packs into few tokens, unfamiliar text into many.
Press Run to see what your code prints, in order.
Runs in this browser, in an isolated frame with no access to this page. console.log, info, warn and error are shown; code runs as a classic script, so use an async function for await.
The context window
Each request has a context window: the maximum number of tokens the model can consider at once, counting your instructions, the conversation so far, any documents you paste in, and the tokens it generates. The model has no memory between requests. If your chat feature "remembers" earlier turns, it is because your code sends them again.
Two consequences follow. First, long conversations get slower and more expensive every turn, and eventually need trimming or summarising. Second, a bigger window is not a perfect memory: research such as Lost in the Middle (Liu et al., 2023) found that models often use information at the start or end of a long input better than information buried in the middle. Put the most important instructions and evidence where they are hard to miss, and do not paste in more than the task needs.
What training does and does not give you
A base model is trained to predict the next token on a large amount of text. Assistant models are then tuned further on examples and human or automated feedback so that they follow instructions and hold a conversation. Neither step teaches the model to check facts. It produces what is likely given its training and your prompt, which is often right and sometimes a confident invention (usually called a hallucination). Its knowledge also stops at a training cutoff.
So design as if the model were a very fast, very well-read colleague who never says "I'm not sure" unless asked: give it the facts it needs (see retrieval), ask it to say when the context does not contain the answer, and verify anything your product presents as true.
Reading a response
A typical response carries more than text. Look for:
- Stop reason: did it finish naturally, hit your output limit, or stop to call a tool? An answer that stopped at the limit is incomplete.
- Usage: input and output token counts for that call. Log them; they are your cost and latency data.
- Content blocks: modern APIs return a list of typed parts (text, tool calls, sometimes reasoning), not one string.
// Placeholder client; real SDKs differ in names but return similar fields.const res = await llm.generate({ model: MODEL, maxOutputTokens: 500, messages })if (res.stopReason === "max_tokens") { // incomplete: raise the limit, ask for less, or continue in a follow-up call}log.info({ inputTokens: res.usage.inputTokens, outputTokens: res.usage.outputTokens })Check yourself
3 questions. Get them all right to finish this topic.
0 of 3 right so far. Get all three to finish this topic.