Module 1 • Topic 4

Temperature, Sampling & Generation Parameters

Master the parameters that control how LLMs generate text — temperature, top-p, top-k, and more.

Temperature, Sampling & Generation Parameters

The same prompt can produce wildly different outputs depending on the generation parameters you set. Understanding these controls is fundamental to consistent prompting.

Key Concepts

  • Temperature: Controls randomness. 0 = deterministic (always picks most likely token). 1 = balanced. 2 = very creative/random.

The same prompt can produce wildly different outputs depending on the generation parameters you set. Understanding these controls is fundamental to consistent prompting.

Key Concepts

  • Temperature: Controls randomness. 0 = deterministic (always picks most likely token). 1 = balanced. 2 = very creative/random.
  • Top-p (Nucleus Sampling): Instead of picking from all tokens, only consider tokens whose cumulative probability reaches p (e.g., 0.9 = top 90% probability mass).
  • Top-k: Only consider the k most likely next tokens (e.g., top-k=50).
  • Max Tokens: Hard limit on output length. The model stops generating when reached.
  • Frequency/Presence Penalty: Discourages repetition by penalizing tokens that have already appeared.

Frameworks

  1. Task-Temperature Mapping: Factual tasks → temp 0-0.3 | Creative tasks → temp 0.7-1.0 | Code generation → temp 0-0.2
  2. Parameter Interaction Rule: Adjust temperature OR top-p, rarely both simultaneously

Practical Guidelines

Task TypeTemperatureTop-pNotes
Factual Q&A01.0Deterministic, reproducible
Code generation0–0.20.95Precise, minimal variation
Creative writing0.7–1.00.9Varied, interesting output
Brainstorming1.0–1.50.95Maximum diversity

Common Mistakes

  • Using high temperature for factual tasks (causes hallucination)
  • Setting both temperature and top-p to extreme values simultaneously
  • Forgetting to set max_tokens and getting unexpectedly long/costly responses
  • Not testing different parameter combinations for your specific use case

Read to the end of the lesson to mark it as read.