1:1 mentoring with Big Tech AI engineers
LLM & Agentic

Tokens, Context Windows & Sampling

What a language model actually does on every call: split text into tokens, predict one next token at a time, fit everything into a fixed context window, and pick each token by sampling — with interactive temperature and context-budget explorers.

Last updated

Foundations9 min read

After this section you can

  • Estimate the tokens, cost and latency of a request from its size
  • Explain how sampling picks each token, what temperature and top-p do, and why the newest Claude models use effort instead
  • Budget a context window across system prompt, tools, history, documents and the answer
01

Tokens, Context Windows & Sampling

Every call to a language model does the same thing: turn text into tokens, predict one next token, repeat. Learn that loop and pricing, latency, context limits and hallucination stop being mysterious.

Key idea

A language model does one job. Given the tokens so far, it scores every possible next token, picks one, appends it, and does it again. Chat, tool calls and agents are all built on that loop.

One step of generation. Every output token is one trip round this loop.
1 · Text “The agent called the” 2 · Tokens The·agent·called·the 4 integer IDs 3 · Model one forward pass scores every token 4 · Probabilities search 50% weather 22% …100K more 5 · Pick sample one “search” append “search” to the input and run the whole loop again Input tokens are read together in one pass. Output tokens come out one pass at a time, which is why output is slower and costs more.

Related

More in LLM & Agentic

Get full access to all 74+ sections with code examples, diagrams, and interactive animations.

Unlock Premium