Generation parameters

Ollama · Practical · 10 min

The right settings make outputs consistent for automation and lively for creative tasks.

Parameters change how the model picks each next token. You can set them in four places: /set parameter in a chat, PARAMETER in a Modelfile, the options field of an API request, or options= in the Python library. Request options override Modelfile defaults.

The ones you'll use most:

  • temperature: randomness. Low values (0 to 0.3) give focused, repeatable answers for extraction, classification and code; higher values (0.7 to 1) give more variety for brainstorming and writing. Ollama's default is 0.8, but many models ship their own defaults.
  • top_p and top_k: limit which tokens may be picked to the most likely ones. Change these rarely; adjust temperature first.
  • num_predict: the maximum number of tokens to generate. The default -1 means no limit; set a limit to bound cost and time.
  • stop: text sequences that end the answer immediately, such as "\n\n" or a closing tag.
  • seed: with a fixed seed and the same prompt and settings, the output is usually repeatable, which helps testing.
  • repeat_penalty: discourages repeating the same words; raise it slightly if a model loops.
  • num_ctx: the context length (its own lesson).

Model creators tune defaults for their model, visible with ollama show MODEL --parameters. Start from those and change one setting at a time while testing on real examples.

Reasoning ("thinking") models often recommend specific settings in their model card. Follow them; very low temperatures can make some of these models repeat themselves.

Example

from ollama import chat

prompt = [{"role": "user", "content": "Invent a name for a coffee shop."}]

for t in (0.0, 1.0):
    names = [chat(model="gemma4:e2b", messages=prompt,
                  options={"temperature": t, "num_predict": 12}).message.content
             for _ in range(3)]
    print(t, names)
# 0.0 ['Bean There', 'Bean There', 'Bean There']        (same every time)
# 1.0 ['The Daily Grind', 'Brew Haven', 'Morning Ritual'] (varies)

fixed = {"temperature": 0.7, "seed": 42}
a = chat(model="gemma4:e2b", messages=prompt, options=fixed).message.content
b = chat(model="gemma4:e2b", messages=prompt, options=fixed).message.content
print(a == b)                                             # usually True: same seed, same output

short = chat(model="gemma4:e2b", messages=prompt, options={"stop": ["\n"]})

Remember

  • Set options via chat, Modelfile, API options or options= in Python
  • Low temperature for automation, higher for creative work
  • num_predict, stop and seed bound length and aid reproducibility

Watch out for

  • Tuning top_p, top_k and temperature all at once, making it impossible to tell which change helped.

Check yourself

  1. A script extracts invoice numbers and sometimes returns different results for the same input. What should you change first?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: docs.ollama.com/modelfile