Skip to content

Tokens, sampling and thinking

Explain behaviour that comes from tokens, context and sampling, and choose the thinking, effort and zero-, one- or few-shot setup that suits a task.

Scenario

Two tickets land on your board in the same sprint.

  • Duplicate detector. A job spots duplicate complaints by summarizing each one with a fixed prompt at temperature: 0 (on an older model that still accepts it) and comparing hashes of the summaries. About one pair of word-for-word identical complaints in twelve gets two different hashes. Nobody touched the prompt or the model ID.
  • Wrong forecast. Finance estimated the monthly bill for a help-centre assistant by taking each article's characters divided by 4, a ratio someone measured on English blog posts. The articles are in Finnish and Korean and include JSON samples. The real bill is 40% higher.

Both look like bugs in your code. Neither is. Before you explore them, one question from debugging.

Explanation

Tokens, not words. Everything the model reads and writes is counted in tokens: context limits, rate limits and the bill. The characters per token vary by language, content and model. The docs note that newer models use a tokenizer that produces about 30 percent more tokens for the same text than earlier ones, so a count from one model doesn't hold for another. For an exact count, call the token counting endpoint with the model you plan to use (it is free, separately rate-limited, and returns an estimate that can differ slightly from the real request).

The context window is working memory. It holds everything the model can reference for a response, including the response itself. More context isn't automatically better: as the token count grows, accuracy and recall degrade (context rot), so curate what you send.

Next-token generation. The model writes by predicting likely next tokens, one at a time, from everything in context. It does not look facts up, which is why a plausible but wrong detail can appear when the real one isn't in context.

Sampling. Temperature sets how adventurous each pick is. Low values give conservative output, but even temperature 0 isn't fully deterministic. Recent models reject any non-default temperature, top_p or top_k with a 400 error, so check your model's page before you rely on them, and steer style with the prompt instead. Either way, test outputs for properties (fields present, facts right, length), not exact strings.

Thinking and effort.

  • Thinking lets Claude work through a problem in thinking blocks before the answer. With adaptive thinking, Claude decides per request whether and how much to think. Older models use extended thinking with a fixed budget_tokens. Which modes a model accepts differs per model, and thinking is on by default on many recent ones.
  • Thinking tokens are billed as output tokens, even when the thinking text isn't returned, and they count toward max_tokens.
  • Effort (output_config.effort: low, medium, high, and on some models xhigh and max) steers how many tokens Claude spends on the whole response: thinking, text and tool calls. It is a behavioural signal, not a strict budget. Lower effort means fewer, terser tool calls; defaults differ per model. To get less thinking, lower effort before you add prompt instructions.
  • Fast mode, on supported models, gives faster output at premium pricing.

Zero-, one- and few-shot. Zero-shot gives instructions only. Examples are one of the most reliable ways to steer format, tone and structure. One example is easy to over-copy; the docs recommend 3 to 5 examples that are relevant, diverse (so Claude doesn't pick up unintended patterns) and wrapped in <example> tags inside <examples>.