Used by GPT-4, Claude, and all modern LLMs. Selects the smallest set of tokens whose cumulative probability exceeds threshold p.
1. Sort tokens by probability (descending)
2. Accumulate until sum ≥ p
3. Return the selected token indices
Adapts vocabulary size based on confidence — uses fewer tokens when model is confident, more when uncertain.
nucleus_indices([0.5, 0.3, 0.15, 0.05], p=0.8) # cumsum: 0.5, 0.8 → stop after 2 tokens → [0, 1] (indices sorted by probability)
Similar Problems
Test Cases (2 visible · 2 hidden)
Case 1: p=0.8 selects top 2
Input: nucleus_indices([0.5,0.3,0.15,0.05],0.8)
Expected: [0, 1]
Case 2: p=1.0 selects all
Input: nucleus_indices([0.5,0.3,0.15,0.05],1.0)
Expected: [0, 1, 2, 3]
⌘↵ Run · ⌘⇧↵ Submit