Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
The Agent Loop  ›  Capstone Project

Sampling Settings — Temperature, Top-p, Penalties and Length

Temperature 15 minTemperature, top-p, frequency and presence penalties and max length shape how the next token is chosen, so tool calls want low randomness while open-ended writing can use more.

Sampling Settings — Temperature, Top-p, Penalties and Length

Tripmate writes the arguments for book_ticket a little differently each time, and once it picks the wrong train. Meanwhile, its trip descriptions for Asha read like the same sentence over and over. Both problems come from the same place: the settings that control how the model picks each next token.

The rule

Tools want low randomness; writing can afford a little more.

At every step the model scores every possible next token, and those scores become probabilities. Sampling settings shape how the next token is chosen from them:

  • Temperature — sharpens or flattens the probabilities. Low (near 0) almost always takes the most likely token: steady and repeatable. Higher values give unlikely tokens more chance: more varied, and more error-prone.
  • Top-p — keeps only the smallest group of top tokens whose probabilities add up to p, then samples among them. With top_p = 0.8, rare long-shot tokens are never picked.
  • Frequency penalty — lowers a token's chance more each time it has already appeared. Cuts repeated words and phrases.
  • Presence penalty — lowers a token's chance by a flat amount once it has appeared at all. Nudges the model toward new words and topics.
  • Max length (max output tokens) — a hard cap on how much the model may write. When it is hit, the answer is simply cut off.

Providers name and support these a little differently, and a common piece of advice is to tune temperature or top-p, not both at once.

The shortcut

Ask: "Would I want the same output if I asked twice?" Yes → low temperature, no penalties (tool calls, data extraction, classification). No → moderate temperature, maybe a small penalty (brainstorming, descriptions).

Top-p on Tripmate's next wordStep 1 of 4
  1. The model's options

    After "Your train leaves on", the probabilities are: Friday 0.50, Thursday 0.30, Saturday 0.15, banana 0.05.

The trap with max length is treating it as "be brief". It does not make the model plan a shorter answer; it chops the answer mid-sentence. To get short answers, ask for them in the prompt and keep the cap as a safety limit.

Drills

Answer fast — trust the rule.

quick drill

Which temperature suits Tripmate filling in book_ticket arguments?

quick drill

With probabilities Friday 0.50, Thursday 0.30, Saturday 0.15, banana 0.05, which tokens remain at top_p = 0.9?

quick drill

A description keeps repeating "scenic" five times. Which setting targets that most directly?

quick drill

Setting max output tokens to 50 on a long itinerary will most likely…

quick drill

What is the difference between presence and frequency penalties?

Remember

  • Tools and extraction want low randomness; open-ended writing can take a little more.
  • Shortcut: "Would I want the same output if I asked twice?"
  • Trap: using max length to mean "be brief" — it truncates, it does not summarise.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPick the settings

Tripmate makes two kinds of model call: one fills in search_trains arguments, the other writes a friendly two-line description of each city. Recommend sampling settings for each and explain why, including what you would do about max length.