HOW-TO · INF

How to tune temperature based on task type

intermediate10 minBy Fredoline Eruo
PREREQUISITES

Ollama or compatible runtime

What this does

Different tasks require different temperature settings. Code generation needs precision (low temperature), creative writing needs variety (high temperature). This guide provides task-specific temperature recipes.

Steps

  1. Use temperature 0.0-0.2 for factual/code tasks.

    curl -s http://localhost:11434/api/generate \
      -d '{"model": "llama3.2", "prompt": "Write a Python function to sort a list",
           "options": {"temperature": 0.1}, "stream": false}' \
      | jq -r '.response'
    
  2. Use temperature 0.3-0.5 for analytical/instruction-following tasks.

    curl -s http://localhost:11434/api/generate \
      -d '{"model": "llama3.2", "prompt": "Analyze the pros and cons of renewable energy",
           "options": {"temperature": 0.4, "top_p": 0.9}, "stream": false}'
    
  3. Use temperature 0.7-0.9 for creative tasks.

    curl -s http://localhost:11434/api/generate \
      -d '{"model": "llama3.2", "prompt": "Write a short story about a robot learning to paint",
           "options": {"temperature": 0.85, "top_p": 0.95}, "stream": false}'
    
  4. Create task-specific aliases in a wrapper script.

    function ask() {
        local task=$1 prompt=$2
        case $task in
            code)    temp=0.1; top_p=0.9 ;;
            analyze) temp=0.4; top_p=0.9 ;;
            creative) temp=0.85; top_p=0.95 ;;
            chat)    temp=0.7; top_p=0.9 ;;
        esac
        curl -s http://localhost:11434/api/generate \
            -d "{\"model\":\"llama3.2\",\"prompt\":\"$prompt\",\"options\":{\"temperature\":$temp,\"top_p\":$top_p}}"
    }
    ask code "Explain Python decorators"
    ask creative "Describe a sunset in space"
    

Verification

# Run a code task at creative temperature vs low temperature
# Expected: Low temperature produces correct code; high temperature may introduce imaginative but non-functional code

Common failures

  • Temperature alone isn't enough: Combine with top_p, repeat_penalty, and max_tokens for best results.
  • Code generation still creative at 0.1: Some models are fine-tuned for creativity. Set temperature=0 for strictly correct code.
  • Over-tuned for a single task: The optimal temperature varies by the specific model, not just the task type. Experiment with ±0.1 increments around the recommended values.

Operator checkpoint

Before treating this as solved, write down the local runtime, model or package version, hardware/backend if relevant, and the verification output. This keeps the guide useful as a Will-It-Run style decision instead of a one-off command transcript.

Related guides

RELATED GUIDES