When a Large Language Model (LLM) generates code or molecular structures, it isn't just picking the "best" next token. It is navigating a probability landscape. The sampling temperature is the primary dial researchers use to reshape this landscape, determining whether the model plays it safe with high-probability sequences or ventures into the "long tail" of creative but risky possibilities.
Recent research across software engineering, biology, and chemistry has converged on a singular insight: the most effective way to use LLMs for structured tasks is not to find the single best output, but to use temperature to explore a diverse candidate pool and then filter for the "needle in the haystack."
The Sharpness of Choice
At its core, temperature ($T$) modifies the softmax function of a model's output. A low temperature (near 0) makes the distribution "sharp," forcing the model to choose only the most likely tokens. This is "greedy" behavior—reliable and predictable. As $T$ increases, the distribution flattens, allowing the model to sample lower-probability tokens.
In the context of code, Mark Chen and the OpenAI team demonstrated in Evaluating Large Language Models Trained on Code that while low temperatures are better for a single attempt, higher temperatures are far superior when the model is allowed multiple independent samples. This is the "pass@k" metric: the probability that at least one of $k$ generated samples is correct.
The Creativity Reliability Trade-off
The challenge with increasing temperature is that structured data—like Python code or protein sequences—is fragile. A single "creative" token in a code block can lead to a syntax error or a runtime crash.
Hongzheng Chen and colleagues, through the HeuriGym benchmark, identified a fundamental tension: higher randomness yields more innovative heuristic algorithms but simultaneously increases the frequency of code "hallucinations" and execution failures. This same pattern appears in biological design. Ruihao Zhang found that in T-cell receptor design (LSMTCR), temperature acts as a toggle between "precision design" (high-fidelity sequences) and "high-throughput discovery" (exploring novel sequence spaces).
Finding the Sweet Spot
If high temperature provides the "raw material" of diversity and low temperature provides the "safety" of convention, how do we get both? The emerging consensus is a two-stage pipeline: Stochastic Exploration followed by Deterministic Filtering.
- High-Temperature Sampling: Generate a large, diverse pool of candidates. This is particularly useful for underspecified problems. Mansi Uniyal showed in the MBUPP benchmark that high randomness allows models to find different valid interpretations of vague natural language prompts.
- Execution-Based Reranking: Use external tools (compilers, unit tests, or simulators) to throw away the failures. Haau-Sing Li describes this in the DOCE framework as the "sweet spot": using high temperatures to ensure the correct solution exists in the pool, then using majority-vote execution (MBR-Exec) to pick the winner.
Beyond Correctness: Efficiency and Low Resources
Temperature tuning isn't just about finding a solution; it's about finding the best solution. Lun Wang and others have used multi-sample strategies to evaluate the efficiency of AI-generated code. By sampling across a range of temperatures, they can identify solutions that aren't just functionally correct but also optimized for runtime and memory.
This exploration is even more critical when the model is "uncertain." Kaixin Wang noted in CodeChemist that for low-resource programming languages, models often produce "flat" probability distributions. In these cases, multi-temperature sampling is essential to force the model out of repetitive, low-quality loops and into more diverse structural paths.
By treating temperature as a tool for search rather than just a parameter for "creativity," researchers have turned the inherent stochasticity of LLMs from a bug into a feature for scientific and engineering discovery.