ComfyUI Node

LLM Top-K

The strict token-cutter in the YALLM sampler chain

By asaddi·Created 2 years ago·Updated 14 days ago· 7
LLM Top-K
  • previous
  • llm_sampler
top_k40

LLM Top-K is the bluntest instrument in the YALLM sampler set. Where top-p keeps tokens until their combined probability mass hits a threshold, top-k just says "only the top K most likely tokens are even eligible, period." It's the hard cutoff, and it's the one sampler here that's an integer rather than a float. Default 40 is the classic llama.cpp value, and it's a fine place to leave it.

How it fits the chain

Same pattern as the pack's other samplers: you're building an ordered list of (name, value) pairs, and each node appends one entry to it. Set top_k, chain it off the previous sampler's llm_sampler output if there is one, and hand the result to the pack's LLM Chat node. The list travels to the provider as an ordered samplers array, and on llama.cpp that order is the order of application - so if you chain top-k after min-p, min-p runs first.

Here's the wrinkle that separates top-k from its sibling nodes: the OpenAI API spec only officially supports temperature and top-p. Top-k is not one of them. In the source, top-k (like min-p) gets shoved into extra_body - the escape hatch for provider-specific parameters - rather than the standard request body. What that means in practice: it works great against llama.cpp and its derivatives (ollama, LM Studio), and against providers that happen to accept the extra field. But a strict OpenAI-compatible hosted endpoint may simply ignore it, so if you're pointing at a remote API and wondering why your top-k setting does nothing, that's likely why.

The inputs and output

  • top_k - an integer, minimum 1, default 40. Low values (like 10) make output tight, repetitive, and predictable; high values (200+) barely trim anything. The classic llama.cpp default of 40 is a reasonable middle.
  • previous (optional) - the llm_sampler output of an earlier sampler node.
  • Output: llm_sampler, into LLM Chat.

Installing it

It ships in ComfyUI-YALLM-node, so the install is the pack install - Manager searching "ComfyUI-YALLM-node", or:

cd ComfyUI/custom_nodes
git clone https://github.com/asaddi/ComfyUI-YALLM-node
cd ComfyUI-YALLM-node
pip install -r requirements.txt

Restart, find it under YALLM/samplers.

Gotchas

The trap is stacking top-k and top-p both low. They're two different trims that compound into a very small, very boring token pool - output turns repetitive fast. Pick one as the main dial. Also, since top-k is passed as a nonstandard extra body field, don't debug a "no effect" with a hosted provider by cranking the value; check whether the provider honors it at all first. When you do use it locally, treat it as a determinism dial: lower top-k is the fastest way to make a llama.cpp model stop free-associating.

CategoryYALLM/samplers

Inputs (2)

NameTypeDefaultDescription
top_kINT40
previousoptLLMSAMPLER

Outputs (1)

NameTypeDescription
llm_samplerLLMSAMPLER