Skip to main content

Tokens, Context & Sampling

Tokens​

  • Models read and write tokens (sub-word pieces from a tokenizer), not characters or words.
  • Price, rate limits, latency and context limits are all counted in tokens.
  • Each vendor (and sometimes each model generation) has its own tokenizer, so the same text yields different counts.
  • Code, non-English text, numbers and whitespace often cost more tokens than plain English prose.
  • Count before you send: Anthropic has a token counting endpoint; for estimates, use the vendor's tokenizer rather than a rule of thumb.
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic();
const { input_tokens } = await client.messages.countTokens({
model: "claude-sonnet-5-5",
messages: [{ role: "user", content: "How many tokens is this?" }],
});

Context window​

TermMeaning
Context windowMax tokens the model can attend to in one request (system + history + tools + docs + output)
Max output (max_tokens)Cap on generated tokens; separate limit, usually far smaller than the window
Knowledge cutoffDate after which the model has no training knowledge; give fresh facts in context

Practical rules:

  • Bigger context is not free: more input tokens means more cost and latency, and quality can drop on facts buried mid-context.
  • Put long documents first, the question last - this tends to work better for long inputs.
  • Trim history: summarize old turns or drop tool outputs you no longer need.
  • Read limits from the model docs (or the Models API) instead of hard-coding them.

Sampling parameters​

ParamWhat it doesTypical use
temperatureScales randomness; low = focused, high = variedLow for extraction/classification, higher for brainstorming
top_pNucleus sampling: sample only from the smallest token set whose probability sums to pTune temperature or top_p, not both
top_kSample only from the k most likely tokensRarely needed
stop_sequences / stopStop generation when a string appearsDelimited outputs
max_tokensHard cap on output lengthAlways set; check the stop reason

Gotchas:

  • Temperature 0 is not fully deterministic; expect small variation.
  • Reasoning / "thinking" models may fix or restrict sampling params; current Claude models use adaptive thinking steered by effort instead.
  • If the reply was cut off, the stop reason says so (max_tokens on Anthropic, incomplete status on OpenAI Responses) - do not parse a truncated JSON.

Stop reasons (Anthropic)​

stop_reasonMeaning
end_turnModel finished naturally
max_tokensHit your cap - raise it or continue
stop_sequenceHit one of your stop strings
tool_useModel wants you to run a tool