AI / A CONCEPT NOTE

Top-k Sampling

limiting choices to the top k tokens

~65 sec read

Overview · mechanism
pitfall · examples

01 / THE SHORT VERSION

The idea in a few sentences.

A sampling filter that limits next-token choices to the top 'k' most likely tokens (e.g. k=40), dropping lower-probability choices to prevent the model from generating nonsense.

02 / FOLLOW THE MECHANISM

How token sorting flows

  1. Compute logits

    model scores vocabulary tokens probabilities.

  2. Limit sorting

    ranks tokens, keeping only the top 40 candidate options.

  3. Scale probability

    recalculates percentage weights among these 40 options.

  4. Sample select

    samples the next token from this restricted set.

04 / COMMAND NOTES

Read the command, then the result.

Inspect the flags and arguments before trying an example. Snippets can need local setup, replacement values, or resources in your own environment.

EXAMPLE 01 · REFERENCE

query local model with top-k limits

curl -X POST -d '{"model":"llama3","prompt":"hi","options":{"top_k":20}}' http://localhost:11434/api/generate

Explore command anatomy in the CLI lab