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:Distribution Sampling
Samples from the probability distribution:uint32_t
Random seed (use
LLAMA_DEFAULT_SEED for random seed)Top-K Sampling
Keeps only the top K most likely tokens:int32_t
Number of top tokens to keep (
<= 0 disables)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
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
Temperature Sampling
Scales logits by temperature (higher = more random):float
Temperature value.
t <= 0.0 keeps only the maximum logit, rest set to -inflogit' = logit / temperature
Dynamic Temperature
Adaptive temperature based on entropy:float
Base temperature
float
Temperature adjustment range
float
Entropy scaling exponent
Typical Sampling
Samples locally typical tokens:Mirostat Sampling
Adaptive sampling that targets a specific perplexity: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: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.
XTC Sampler
Top-nσ Sampler
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
grammars/README.md for grammar syntax.
Logit Bias
Manually bias specific tokens:Infill Sampler
For fill-in-the-middle tasks: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
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 withoutllama_sampler_sample:
Next Steps
Inference
Learn about batching and decoding
libllama Overview
Return to API overview

