跪拜 Guibai
← All articles
LangChain · JavaScript

Temperature and Top-K Are Not Mysteries: A Practical Guide to Controlling LLM Output Randomness

By 只一 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Uncontrolled randomness is the fastest way to erode trust in an LLM-powered feature. Knowing that a high temperature plus a large Top-K is the specific combination that produces hallucinations gives teams a concrete knob to turn instead of guessing why outputs went off the rails.

Summary

Large language models produce different outputs from the same prompt because of word probability sampling, not magic. Temperature flattens or sharpens the probability distribution, controlling how often low-probability words get chosen. Top-K acts as a safety fence, restricting the model to only the K most likely next words regardless of how high the temperature is set.

A high temperature with a large Top-K is a recipe for hallucinations; the model can pick from a vast pool of unlikely words. The sweet spot for creative writing is a high temperature (0.7–0.9) paired with a small Top-K, which encourages imagination without letting the model veer into nonsense. For code, contracts, or factual answers, a low temperature (0.1–0.3) with a larger Top-K keeps output stable while preserving some expressive range.

LangChain makes this operational by letting you define separate model instances with different sampling parameters and pipe them into the same prompt template. The result is two distinct pipelines — one creative, one precise — that can be swapped in a single workflow without changing business logic.

Takeaways
Temperature controls the flatness of the probability distribution: low values (0.1–0.3) concentrate probability on the safest words, high values (0.7–0.9) give rare words a real chance of being selected.
Top-K restricts sampling to only the K most probable next words, acting as a hard boundary that prevents the model from ever selecting extremely low-probability tokens.
A high temperature combined with a large Top-K is the specific parameter combination that maximizes hallucination risk and should be avoided in production.
Creative writing benefits from high temperature (0.7–0.9) plus a small Top-K, producing varied, imaginative text without semantic collapse.
Rigorous tasks like code generation or contract drafting need low temperature (0.1–0.3) plus a moderately large Top-K to stay factually grounded while retaining some phrasing flexibility.
Two separate LangChain model instances with different sampling parameters can share one prompt template, creating swappable creative and precise pipelines without duplicating logic.
Conclusions

Most developers treat LLM output instability as an inherent flaw, but the entire randomness surface is controlled by exactly two parameters. The black-box feeling comes from not knowing how they interact, not from any true opacity.

The article's core practical insight is counterintuitive: to get more creativity, you should actually shrink the candidate pool (smaller Top-K) while raising temperature. Letting the model choose from everything at high temperature is what produces gibberish, not creativity.

LangChain's value here is not in any complex orchestration but in making parameter configuration a first-class, reusable asset. Two model instances with different sampling settings become two reliable tools in a toolkit rather than ad-hoc tweaks scattered across code.

Concepts & terms
Temperature
A parameter (typically 0–1) that controls the flatness of the probability distribution when sampling the next token. Low values sharpen the distribution, making high-probability tokens even more likely; high values flatten it, giving low-probability tokens a greater chance of being selected.
Top-K sampling
A sampling strategy that sorts all possible next tokens by probability and retains only the K highest-scoring ones. The model then samples exclusively from this restricted set, preventing it from ever selecting extremely low-probability tokens regardless of the temperature setting.
Autoregressive text generation
The process by which a language model generates text one token at a time, where each new token is conditioned on all previously generated tokens. The model computes a probability distribution over its entire vocabulary for the next position and samples from it.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗