Skip to main content

Inference and sampling

If you submit the exact same prompt to a language model twice, you will often receive two distinct answers. This variability is not a glitch—it is an intentional feature introduced during generation by a configurable sampling layer outside the neural network itself.

In machine learning terminology, refers to running an existing, trained model to evaluate inputs and generate predictions. During inference, model weights remain entirely static: the neural network doesn't learn new information or adjust its parameters. It simply maps input numbers to output numbers.

What a single forward pass produces

When input tokens pass through a model's weights, a single forward pass calculates a raw numerical score—called a logit—for every single token in the tokenizer's vocabulary. If a model operates with a vocabulary of 128,000 tokens, it outputs 128,000 individual logits.

These raw logits are normalized through a softmax function to produce a : a complete list of 128,000 probabilities that sum to 1.0. For an incomplete sentence like "The capital of France is", the resulting distribution might look like this:

CandidateProbability
Paris0.92
located0.03
a0.02
the0.01
All other tokens combined0.02

The neural network's internal calculation concludes here. It has assigned a likelihood to every candidate in its vocabulary, but it hasn't actually selected which token will appear next.

Two phases: prefill and decode

Turning an initial prompt into a completed response involves two computationally distinct stages:

processes the incoming prompt all at once. Because all input tokens are known from the start, GPUs can process them concurrently across parallel cores, calculating attention matrices and populating the key-value (KV) cache. Prefill duration scales primarily with prompt length: a prompt containing 100,000 tokens will take noticeably longer to digest than a 50-token query.

generates the response sequentially, one token at a time. Each generation step evaluates the accumulated context, computes a new probability distribution, samples a token, appends that token to the context, and feeds the updated sequence into the next pass. Because token 200 cannot be determined until token 199 has been generated, decode is inherently serial. The vast majority of the wall-clock time spent waiting for a long response occurs during this decode phase.

This architectural difference explains user-perceived latency. (TTFT) reflects prompt prefill plus network transmission and server queueing; it increases when you send larger prompts. Once generation begins, subsequent tokens appear at a steady pace determined primarily by hardware memory bandwidth and model size. In other words, a massive prompt makes a completion slow to start, while a lengthy response makes it slow to finish.

bridges this gap for interactive applications. Rather than buffering the entire completion until generation completes, the serving infrastructure streams individual tokens over a connection as they decode. While streaming does not change the total generation time, it allows users to read output immediately, dramatically improving perceived responsiveness.

Where the randomness enters

The model outputs a probability distribution, but the downstream algorithm decides which token actually gets appended to the sequence. This sampling step runs outside the neural network within the model serving stack, and it is where output variability originates.

The simplest selection strategy is , which always chooses whichever token holds the highest probability. While predictable, greedy generation often leads to repetitive or unnatural phrasing. Most production applications use parameterized sampling to introduce controlled diversity.

Temperature

scales logits before the softmax normalization occurs. Setting temperature below 1.0 sharpens the distribution, exaggerating the gaps between candidates so the highest-scoring token becomes overwhelmingly dominant. Raising temperature above 1.0 flattens the distribution, giving lower-ranked alternatives a higher likelihood of selection.

Consider how temperature alters candidate probabilities following the phrase "The report was":

CandidateTemperature 0.2Temperature 1.0Temperature 1.5
clear0.980.600.42
thorough0.020.250.27
late0.000.100.18
damning0.000.050.13

These numbers illustrate the underlying shift: at temperature 0.2, the model selects "clear" almost every time. At temperature 1.5, "damning" gets chosen roughly one in eight runs. Adjusting temperature does not make a model more capable or imaginative; it simply controls how frequently the algorithm selects lower-probability tokens.

Top-p (nucleus sampling)

(or nucleus sampling) controls the selection pool by probability mass rather than mathematical scaling. The sampler sorts tokens by descending probability, calculates a cumulative sum, and cuts off the list as soon as the total reaches threshold $p$. Tokens falling outside this cumulative threshold are completely discarded, and the remaining subset is re-normalized.

For example, with top-p set to 0.9 on the temperature 1.0 column above, the sampler includes clear (0.60), thorough (0.85 cumulative), and late (0.95 cumulative, crossing the 0.9 mark). The tail candidate damning is excluded entirely.

Combining both parameters provides fine-grained control: temperature controls the relative flatness of the candidate distribution, while top-p truncates the long tail of implausible tokens. Raising temperature while holding top-p at 0.85 or 0.90 encourages stylistic variety without letting nonsensical or ungrammatical words slip through. Most providers recommend tuning one parameter while keeping the other at its default to avoid unpredictable interactions.

What temperature and top-p do to the same next-token distribution Three small bar charts side by side, each showing the same four candidate next tokens: clear, thorough, late and damning. In the left chart, at temperature 0.2, the bar for "clear" is almost full height at 0.98 and the other three are barely visible. In the middle chart, at temperature 1.0, the distribution is much flatter: clear 0.60, thorough 0.25, late 0.10 and damning 0.05. The right chart applies top-p 0.9 to that same temperature 1.0 distribution. Clear, thorough and late survive with slightly raised values of 0.63, 0.26 and 0.11, drawn with a dashed outline, and the bar for damning has been removed, marked with a cross and the word "cut". The point is that temperature changes the height of every bar while top-p removes bars from the bottom of the list. The same four candidates, under three sampler settings Temperature 0.2 Sharpened. One winner. 0.98 clear 0.02 thorough 0.00 late 0.00 damning Temperature 1.0 The distribution as the model gave it. 0.60 clear 0.25 thorough 0.10 late 0.05 damning Top-p 0.9, temperature 1.0 Tail cut, survivors renormalised. 0.63 clear 0.26 thorough 0.11 late cut damning Temperature reshapes every bar. Top-p deletes bars from the bottom of the ranking. Figures are illustrative, not measured.
Temperature reshapes the whole distribution. Top-p cuts the tail off it. The two do different jobs.

Sampler controls on newer models

Before designing custom user interfaces around temperature and top-p sliders, verify whether your target model supports them. Several modern reasoning models reject non-default temperature settings or lock sampling parameters entirely. Providers often find that deviating from default sampling values degrades performance on reasoning-tuned models, sometimes causing repetitive loops or broken logic. Check vendor-specific parameters before relying on legacy sampling knobs.

Why greedy decoding is not fully deterministic

If you set temperature to 0, the sampling algorithm becomes deterministic: it will always choose the single highest-probability token. However, sending the identical prompt to a commercial hosted API can still produce different outputs across calls.

This discrepancy occurs because the underlying logits themselves are rarely bit-identical across runs, due to several operational factors:

  • Dynamic server-side batching: Cloud providers bundle concurrent user requests into shared matrix multiplications on GPUs to maximize throughput. Because floating-point addition is non-associative—(a + b) + c does not strictly equal a + (b + c) in hardware arithmetic—differing batch compositions alter rounding in the final decimal places. When two top candidates have nearly identical probabilities (such as 0.4999 vs 0.5001), minor rounding differences can flip the winning token, causing the entire subsequent response to diverge.
  • Heterogeneous hardware and kernels: Providers route traffic across clusters containing mixed accelerator generations and optimized compute kernels. Internal matrix reduction orders vary across hardware revisions, introducing slight numerical drift.
  • Mixture-of-experts (MoE) routing: In MoE architectures where only a subset of specialized sub-networks activate per token, cross-request batching dynamics can subtly influence expert routing decisions.
  • Model aliases and rolling updates: Pointing requests to generic aliases like latest or unversioned model names means your calls resolve to whichever build the vendor is currently serving. Providers frequently roll out minor updates, optimizations, or safety patches without changing base model identifiers.

Reproducibility with random seeds

Some model APIs support an optional seed parameter to initialize the sampler's pseudo-random number generator. While setting a seed stabilizes random token selection, it cannot eliminate variability caused by server-side batching, kernel differences, or backend infrastructure updates. Most providers document seed support as best-effort rather than an absolute guarantee of bit-level determinism.

When running local models on dedicated hardware where you control batch size, precision, and GPU environment, setting a seed does produce strictly reproducible completions, as discussed in local and cloud inference.

Making a model call testable

Pin every part of the call that you can control. Accept that the output is still not guaranteed byte-identical.

  1. Name an exact model version. Do not call an alias such as latest.
  2. Set temperature to 0 if the model still accepts the parameter.
  3. Set the seed if the provider offers one.
  4. Record the model version and the fingerprint the provider returns with each response.
  5. Assert on facts in the output. Do not assert on the exact string.
  6. Run the assertion several times before you trust a pass.
  7. Re-run your evaluation set when the provider announces any model change.

Unit tests that compare model responses against static string snapshots are notoriously brittle, leading teams to dismiss test failures as noise. Instead, assert against semantic criteria: verify that output conforms to valid JSON, that mandatory schema fields exist, that extracted numerical values fall within acceptable tolerances, and that classifications match expectations.

Terms introduced

  • Inference: running a trained model to produce an output. No weights change.
  • Prefill: the phase that processes the whole prompt in parallel, before any output token exists.
  • Decode: the phase that generates output tokens one at a time, each one depending on the last.
  • Next-token distribution: the probability the model assigns to every token in its vocabulary as the next one.
  • Sampling: the step outside the model that picks one token from that distribution.
  • Temperature: a sampler setting that flattens or sharpens the distribution before a token is picked.
  • Top-p: a sampler setting that keeps only the smallest set of candidates whose probabilities sum to p, and discards the rest.
  • Greedy decoding: always taking the highest-probability token, which removes the sampler's randomness but not the provider's.
  • Time to first token: the delay before the first output token arrives, driven mostly by prompt length and queueing.
  • Streaming: sending each output token to the caller as it is decoded, rather than holding the whole response.

How providers do it

The direction of travel is the story here. All three vendors are taking the sampling knobs away on their newest models rather than adding more.

QuestionAnthropicOpenAIGoogle
Temperature range and default0.0 to 1.0, default 1.00 to 2, no default stated0.0 to 2.0, default 1.0
Top-ptop_ptop_ptopP, default 0.95
Top-ktop_kNot offeredtopK, default 40
Still settable on the newest models?No. Non-default values return a 400Not on reasoning models; migration says remove themMigration says strip them from generation configs
SeedNot offeredYes, "best effort", with system_fingerprintYes, seed in generationConfig
Determinism guaranteeExplicitly disclaimed, first-party and third-party alike"Determinism is not guaranteed"Not confirmed on a page that would load
Streaming"stream": true, server-sent eventsstream=True, semantic eventsstreamGenerateContent, or stream=True

Statuses differ per row and are marked in the tabs. Google's determinism hedge, OpenAI's seed support on the Responses API and OpenAI's per-model temperature rules are the open ones.

Two vendors ship a seed and neither promises it works. If a test needs the same bytes twice, it needs a model running on hardware you control, not an API.

What this maps to: the Messages API. The notable thing here is that Anthropic has removed the sampling knobs on its newest models rather than exposing more of them.

QuestionAnswerStatus
Sampling parameters exposedtemperature, top_p and top_k, all three now marked deprecated in the API referenceconfirmed
Documented range and defaulttemperature "defaults to 1.0. Ranges from 0.0 to 1.0". top_k samples only from the top K optionsconfirmed
Are they still accepted?Not on current models. "Models released after Claude Opus 4.6 do not support setting temperature. A value of 1.0 will be accepted for backwards compatibility, all other values will be rejected with a 400 error." top_p below 0.99 and any top_k are rejected the same wayconfirmed
Which models reject themFable 5.1, Mythos 5.1, Fable 5, Mythos 5, Mythos Preview, Opus 5, Opus 4.8, Opus 4.7 and Sonnet 5, "regardless of whether thinking is used"confirmed
Guidance on combining temperature and top-pNone given; both are deprecated instead. On older models with thinking enabled, temperature and top_k are incompatible with thinking and top_p is allowed only between 0.95 and 1confirmed
Is a seed offered?No. The Create a Message body has no seed fieldconfirmed
Determinism guaranteeExplicitly disclaimed. "Even with temperature set to 0, the results will not be fully deterministic and identical inputs may produce different outputs across API calls. This applies both to Anthropic's first-party inference service and to inference through third-party cloud providers"confirmed
StreamingYes, "stream": true, over server-sent events. The sequence is message_start, then content_block_start, content_block_delta and content_block_stop per block, then message_delta and message_stopconfirmed
Delta typestext_delta, input_json_delta, thinking_delta and signature_delta. usage on message_delta is cumulativeconfirmed
Errors mid-streamArrive as error events after a 200 has already been sentconfirmed
Pinning a buildDated model IDs exist, such as claude-haiku-4-5-20251001, alongside undated aliases such as claude-haiku-4-5confirmed

Their vocabulary

Standard termTheir term
SamplingSampling, via temperature, top_p and top_k, all deprecated
StreamingStreaming Messages, over server-sent events
Greedy decodingNot named. Temperature 0 was the route to it and is no longer settable on current models

Where to look

The Create a Message reference lists the full accepted request body, which is the quickest way to see what a given model still takes. Mid-stream errors are visible only if you read the event stream rather than the final assembled message.

Last verified: 2026-09-09 against the Anthropic platform docs: Create a Message, streaming, thinking, and the glossary (platform.claude.com/docs/en/api/messages/create, /build-with-claude/streaming, /build-with-claude/thinking, /about-claude/glossary).


Check your understanding

0 of 4 answered

  1. A summarisation job sends 80,000 tokens and asks for a 200-token answer. Users report a long wait before anything appears, then a fast finish. Where did the time go?
  2. What does one forward pass through a model's weights actually produce?
  3. A test sets temperature to 0 against a hosted API and asserts on the exact response string. It passes locally and fails once a week in CI. What is the most likely cause?
  4. A team pins a seed and still sees answers change after a quiet weekend with no deploys. What should they check first?