Skip to main content

Overview

The sampling API provides flexible token selection strategies for text generation. Samplers can be chained together to create complex sampling pipelines.

Sampler Chain

A sampler chain applies multiple sampling strategies in sequence:

Chain Parameters

Basic Usage

Sampling Function

llama_sampler *
Sampler chain to use
llama_context *
Context containing logits from latest decode
int32_t
Index of token to sample from (use -1 for last token, supports negative indexing)
llama_token
The sampled token ID
This function is shorthand for getting logits, applying the sampler chain, and accepting the selected token.

Available Samplers

Greedy Sampling

Always selects the token with highest probability:
Use for deterministic, focused output.

Distribution Sampling

Samples from the probability distribution:
uint32_t
Random seed (use LLAMA_DEFAULT_SEED for random seed)
Must be the last sampler in the chain (like greedy).

Top-K Sampling

Keeps only the top K most likely tokens:
int32_t
Number of top tokens to keep (<= 0 disables)
Reference: “The Curious Case of Neural Text Degeneration”

Top-P (Nucleus) Sampling

Keeps tokens with cumulative probability >= p:
float
Cumulative probability threshold (0.0 to 1.0)
size_t
Minimum number of tokens to keep
Reference: “The Curious Case of Neural Text Degeneration”

Min-P Sampling

Keeps tokens with probability >= p * max_probability:
float
Minimum probability threshold (relative to max)
size_t
Minimum number of tokens to keep
Reference: https://github.com/ggml-org/llama.cpp/pull/3841

Temperature Sampling

Scales logits by temperature (higher = more random):
float
Temperature value. t <= 0.0 keeps only the maximum logit, rest set to -inf
Formula: logit' = logit / temperature

Dynamic Temperature

Adaptive temperature based on entropy:
float
Base temperature
float
Temperature adjustment range
float
Entropy scaling exponent
Reference: https://arxiv.org/abs/2309.02772

Typical Sampling

Samples locally typical tokens:
Reference: https://arxiv.org/abs/2202.00666

Mirostat Sampling

Adaptive sampling that targets a specific perplexity:
Reference: https://arxiv.org/abs/2007.14966
Mirostat samplers select the final token, so they should be last in the chain (like greedy or dist).

Penalty Samplers

Penalize repeated tokens:
int32_t
Number of recent tokens to penalize (0 = disabled, -1 = full context)
float
Repetition penalty multiplier (1.0 = no penalty, > 1.0 = penalize)
float
Frequency penalty (0.0 = disabled)
float
Presence penalty (0.0 = disabled)
Avoid using penalties with full vocabulary as searching can be slow. Apply top-k/top-p first.

DRY Sampler

“Don’t Repeat Yourself” sampler:
Reference: https://github.com/oobabooga/text-generation-webui/pull/5677

Adaptive-P Sampler

Maintains target probability over time:
Adaptive-P selects the final token and should be last in the chain. Use mild truncation (e.g., min-p) before this sampler.
Reference: https://github.com/ggml-org/llama.cpp/pull/17927

XTC Sampler

Reference: https://github.com/oobabooga/text-generation-webui/pull/6335

Top-nσ Sampler

Reference: https://arxiv.org/pdf/2411.07641

Grammar Sampler

Constrain output to match a GBNF grammar:
const llama_vocab *
Vocabulary for tokenization
const char *
GBNF grammar production rules
const char *
Start symbol name
See grammars/README.md for grammar syntax.

Logit Bias

Manually bias specific tokens:

Infill Sampler

For fill-in-the-middle tasks:
Use after top-k/top-p. Combines prefix probabilities and handles EOG tokens specially.

Sampler Management

Clone and Free

Do not manually free samplers that have been added to a chain. The chain takes ownership and will free them automatically.

Manual Application

Get Seed

Returns the seed used by the sampler, or LLAMA_DEFAULT_SEED if not applicable.

Performance Monitoring

Performance functions only work with sampler chains created via llama_sampler_chain_init.

Common Sampling Configurations

Complete Sampling Example

Token Data Array (Advanced)

For manual sampling without llama_sampler_sample:

Next Steps

Inference

Learn about batching and decoding

libllama Overview

Return to API overview