n4nAI

How temperature and top-p affect your prompts

A practical guide to temperature and top-p prompting with code examples, common pitfalls, and tradeoffs for engineers building LLM applications.

n4n Team5 min read1,136 words

Audio narration

Coming soon — every post will get a voice note here.

Temperature and top-p prompting are the two knobs that control how deterministic or creative your model outputs feel. Most engineers reach for temperature first, but top-p (nucleus sampling) often gives you finer control over the tail of the distribution. Understanding both — and how they interact — lets you dial in the exact behavior your use case needs without guessing.

What these parameters actually do

Temperature scales the logits before the softmax. A temperature of 1.0 leaves the distribution unchanged. Below 1.0 sharpens it (more confident, more deterministic). Above 1.0 flattens it (more diverse, more chaotic). At 0, you get greedy decoding — always the highest-probability token.

Top-p works differently. Instead of reshaping the whole distribution, it truncates the long tail. You sort tokens by probability, accumulate until you hit probability mass p, then renormalize and sample only from that nucleus. At p=1.0 you sample from the full distribution. At p=0.1 you only consider the top 10% of probability mass.

# Conceptual implementation
def sample_with_temperature_top_p(logits, temperature=1.0, top_p=1.0):
    if temperature != 1.0:
        logits = logits / temperature
    
    probs = softmax(logits)
    
    if top_p < 1.0:
        # Nucleus sampling
        sorted_indices = torch.argsort(probs, descending=True)
        sorted_probs = probs[sorted_indices]
        cumulative_probs = torch.cumsum(sorted_probs, dim=-1)
        
        # Keep tokens where cumulative prob <= top_p
        mask = cumulative_probs <= top_p
        # Always keep at least one token
        mask[0] = True
        
        filtered_probs = torch.zeros_like(probs)
        filtered_probs[sorted_indices[mask]] = sorted_probs[mask]
        probs = filtered_probs / filtered_probs.sum()
    
    return torch.multinomial(probs, num_samples=1)

The key insight: temperature changes the shape of the distribution globally. Top-p changes the support — which tokens are even eligible for selection.

When to use each one

Start with this mental model: temperature controls creativity level. Top-p controls consistency floor.

Low temperature (0.0–0.3), any top-p: Code generation, extraction, classification, formatting. You want the model to pick the single most likely correct answer. Temperature 0 is fine here — but some APIs don’t support true 0, so 0.1 is a safe default.

Medium temperature (0.4–0.7), top-p 0.9–0.95: General chat, summarization, rewriting. Enough diversity to feel natural, but the nucleus keeps the really weird tokens out.

High temperature (0.8–1.2), top-p 0.9–1.0: Brainstorming, creative writing, generating diverse candidates for reranking. You want the model to explore.

Low top-p (0.1–0.5), any temperature: Constrained generation where you need strict adherence to a pattern. Useful for structured output when you can’t use grammar-constrained decoding.

// Typical API payloads for common scenarios
{
  "code_generation": { "temperature": 0.1, "top_p": 0.95 },
  "chat_assistant":  { "temperature": 0.7, "top_p": 0.9 },
  "creative_writing": { "temperature": 0.9, "top_p": 0.95 },
  "classification":  { "temperature": 0.0, "top_p": 1.0 },
  "few_shot_json":   { "temperature": 0.2, "top_p": 0.5 }
}

The interaction trap

Here’s where engineers get bitten: temperature and top-p compound in non-obvious ways.

At high temperature, the distribution flattens. More tokens have similar probability. Top-p then has to reach deeper into the sorted list to accumulate the same probability mass. Your effective vocabulary expands even if top-p stays constant.

At low temperature, the distribution sharpens. The top token might have 0.9 probability. With top-p=0.9, you’re sampling from exactly one token — effectively greedy. But with top-p=0.95, you suddenly include the second token. A tiny top-p change creates a discrete jump in behavior.

# This demonstrates the interaction
def effective_vocab_size(logits, temperature, top_p):
    probs = softmax(logits / temperature)
    sorted_probs = torch.sort(probs, descending=True).values
    cumulative = torch.cumsum(sorted_probs, dim=-1)
    return (cumulative <= top_p).sum().item()

# Same top_p, different temperatures -> wildly different vocab sizes
logits = torch.randn(50000)  # vocab size
for temp in [0.2, 0.7, 1.0, 1.2]:
    vocab = effective_vocab_size(logits, temp, 0.9)
    print(f"temp={temp}, top_p=0.9 -> {vocab} tokens in nucleus")

Practical rule: pick one as your primary knob, keep the other at a sensible default. Most teams standardize on temperature as the user-facing control and fix top-p at 0.9 or 0.95.

Common pitfalls

Pitfall 1: Setting temperature=0 expecting deterministic output

Many APIs don’t implement true greedy decoding at temperature=0. They clamp to a minimum (often 0.01 or 0.1). Even with true 0, floating-point non-determinism in attention kernels can produce different outputs across runs or hardware. If you need strict determinism, use a seed parameter if the API offers one — but don’t rely on it for production idempotency.

Pitfall 2: Cranking temperature to fix “boring” outputs

High temperature doesn’t make outputs more interesting — it makes them more random. You’ll get hallucinations, syntax errors, and topic drift before you get creativity. Instead: improve your prompt, add few-shot examples, or use a more capable model. Temperature is a sampling parameter, not a quality parameter.

Pitfall 3: Using top-p alone for “more focused” output

Low top-p with high temperature is a contradiction. You’re flattening the distribution and aggressively truncating it. The result is sampling from a small set of tokens that all have similar (low) probability — essentially random choice among mediocre options. If you want focus, lower temperature.

Pitfall 4: Ignoring the interaction with repetition penalties

Repetition penalty, presence penalty, and frequency penalty all modify logits before temperature scaling. The order of operations matters:

logits -> repetition_penalties -> temperature_scaling -> top_p_truncation -> softmax -> sample

A high repetition penalty at high temperature can push the model into incoherence because the penalty dominates the already-flat distribution.

Practical tuning workflow

Don’t guess. Run a quick grid search on a representative prompt set.

import itertools

def evaluate_params(prompt, params_grid, n_samples=5):
    results = {}
    for temp, top_p in params_grid:
        outputs = []
        for _ in range(n_samples):
            out = generate(prompt, temperature=temp, top_p=top_p)
            outputs.append(out)
        # Measure: uniqueness, validity, quality proxy
        results[(temp, top_p)] = {
            "unique_ratio": len(set(outputs)) / len(outputs),
            "avg_length": sum(len(o) for o in outputs) / len(outputs),
            "samples": outputs
        }
    return results

# Coarse grid first
grid = list(itertools.product([0.0, 0.3, 0.5, 0.7, 1.0], [0.5, 0.9, 0.95, 1.0]))
results = evaluate_params(your_test_prompt, grid)

# Then refine around the best region

Look for the region where unique_ratio is high enough for your use case but outputs still pass your validity checks (JSON parses, code compiles, facts check out).

Per-use-case defaults I actually use

Use case Temperature Top-p Rationale
SQL generation 0.0 1.0 Deterministic, single correct answer
Code completion 0.1 0.95 Near-deterministic, allow minor style variance
Data extraction 0.0 1.0 No creativity wanted
Summarization 0.3 0.9 Faithful but natural phrasing
Chat (general) 0.7 0.9 Balanced conversational feel
Idea generation 0.9 0.95 Broad exploration
Few-shot JSON 0.1 0.5 Strict structure, minimal variance
RAG answer synthesis 0.2 0.9 Grounded, slight phrasing variety

What about top-k?

Top-k is the older sibling: keep only the k most likely tokens, renormalize, sample. It’s simpler but less adaptive. Top-p adjusts the candidate set size based on how peaked the distribution is. Top-k uses a fixed budget regardless of context.

Most modern APIs support both. If you must choose, prefer top-p. If you use both, top-k applies first (hard cap), then top-p (probability mass cap). This combination is rarely useful — pick one.

Debugging sampling weirdness

When outputs feel wrong, check these in order:

  1. Is temperature actually what you think? Log the effective parameters from the API response. Some providers clamp or transform values.
  2. Are you getting cached deterministic outputs? Some systems cache at temperature=0. Add a dummy varying parameter or use a seed.
  3. Is the prompt the problem? No sampling parameter fixes a prompt that admits multiple valid interpretations. Disambiguate in the prompt first.
  4. Check the logprobs. If your API returns them, inspect the top-5 token probabilities at the first divergence point. You’ll see whether the model is uncertain (flat distribution) or confident but wrong (peaked on bad token).
# Quick curl to inspect logprobs (OpenAI-compatible)
curl -X POST https://api.example.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o-mini",
    "messages": [{"role": "user", "content": "Say hello"}],
    "temperature": 0.7,
    "top_p": 0.9,
    "logprobs": true,
    "top_logprobs": 5
  }'

One note on routing

If you’re routing across multiple providers (as n4n.ai does across 240+ models), remember that sampling behavior can differ subtly between providers even at identical parameters. Different tokenizer vocabularies, different kernel implementations, different floating-point handling. Treat sampling parameters as per-model tuning, not global constants. Store them in your model registry alongside the model ID.

Summary checklist

  • Default to temperature=0.7, top-p=0.9 for general chat
  • Use temperature=0.0–0.1 for code, extraction, classification
  • Fix top-p at 0.9 or 0.95; tune temperature as primary knob
  • Never set both high simultaneously
  • Run a small grid search on your actual prompts before shipping
  • Log effective parameters and logprobs in production for debugging
  • Treat sampling config as part of the model contract, not global defaults

Temperature and top-p prompting aren’t magic creativity dials. They’re sampling controls with precise, predictable effects on the output distribution. Treat them like any other hyperparameter: measure, tune, document, and version.

Tagstemperatureprompt-engineeringllm-basics

Written by

n4n Team

The team building n4n — a single OpenAI-compatible API in front of 240+ models, with automatic fallback, load balancing and pay-per-token metering.

More from n4n Team →

All prompt engineering fundamentals posts →