liveBook · Manning

Temperature

As explained in AI Agents in Action, Second Edition

A temperature parameter adjusts the concentration of a probability distribution, affecting sampling randomness or the likelihood of accepting less favorable transitions.

Temperature adjusts token probabilities before sampling. Higher values make the distribution flatter and outputs more varied; lower values make it sharper and outputs more consistent. It influences generation but does not impose a strict limit. section 2.1.3

Temperature affects which token the model selects. Unlike max_tokens, it does not cap response length; unlike API settings such as authentication and retries, it changes generation rather than the call itself. section 2.1.3

Choose a setting to suit the agent’s role:

  • Use a low temperature when consistent answers or code are important. section 2.1.3
  • Use a higher temperature for creative writing or brainstorming, accepting less factual accuracy. section 2.1.3
  • For routine planning, temperature 0 can make outputs more consistent and easier to evaluate, but does not guarantee identical answers. section 2.3.2
Table 2.1 LLM parameters and how they can be used to control output

Parameter

Typical range

What it really does

When to raise / lower

Temperature

0–2
(default ≈ 1)

Scales every token’s probability before sampling. Higher = flatter distribution ⇒ more randomness/creativity. Lower = sharper ⇒ more deterministic, repetitive output.

Raise for brainstorming, fiction. Lower for consistent answers, code.

top-p (nucleus)

0–1
(default ≈ 1)

Keeps only the smallest set of tokens whose cumulative probability ≥ p; renormalizes, then samples. Controls variety without letting low-probability “tail” tokens in.

Lower(< 0.9) for tight, on-topic prose. Higher (~1) for free-form generation.

max_tokens

1–8192 + (model-dependent)

A hard ceiling on how many tokens the model may generate in the response. (Prompt tokens + max ≤ context window.)

Set just above the expected length. It prevents rambling and keeps cost down.

presence_penalty

–2 … +2

Adds a penalty once a token has appeared; encourages new topics

Use 0.6–1.0 to avoid repeating already-­mentioned concepts.

frequency_penalty

–2 … +2

Penalizes tokens in proportion to their current count; discourages phrase repetition

Use 0.2–0.8 to reduce echoing or loops in long answers.

Compare temperature with top-p and max_tokens, including their effects and typical uses. section 2.1.3

How AI Agents in Action, Second Edition covers it

How deep it goes
Explains temperature’s effect on sampling and demonstrates settings, while warning that zero does not guarantee identical outputs.
Examples
Compares coder and creative-writer settings and tests agent consistency at temperatures 0 and 1.0.
What you can do after
Configure OpenAI Agents SDK model settings for more consistent outputs, then compare repeated runs.
How it connects
Connects temperature with token sampling, top-p, penalties, max_tokens, and agent roles.

Also explained in

Compare how 17 books explain temperature →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents in Action, Second Edition. The section links point to the parts of the book it draws on.