AlgoMaster Logo

Exercise: Understanding LLM Parameters

3 min readUpdated June 22, 2026
Listen to this chapter
Unlock Audio

Exercise 1: Sweep the Temperature

Send the same prompt at three different temperatures and compare the outputs, so you can see how temperature controls the randomness of a model's response.

Temperature is the parameter people reach for first and understand least. The fastest way to build intuition for it is to hold everything else fixed and watch what changes as you turn it up. Low temperature gives focused, repeatable answers; high temperature gives varied, surprising ones.

Requirements

  • Use one fixed prompt that invites some creativity, such as naming a product.
  • Call the model at temperatures 0.0, 1.0, and 2.0.
  • Run each temperature a few times so you can see how much the output varies.
  • Print the temperature alongside each response.
main.py
Loading...

Expected Output

At temperature 0, the three runs are nearly identical. As temperature rises, the runs diverge.

Solution

main.py
Loading...

Holding the prompt and model fixed isolates temperature as the only variable. The inner loop runs each setting three times so the variation is visible rather than something you have to infer from one sample. The contrast between the repeatable output at 0 and the scattered output at 2.0 is the whole point: temperature is the dial you set low when you need consistency and higher when you want variety.

Exercise 2: Control Length with max_tokens and stop

Temperature controls randomness; max_tokens and stop control where generation ends. Use both on the same prompt and read the finish_reason to see why each response stopped: hitting the token cap versus reaching a stop sequence.

Requirements

  • Call the model with a small max_tokens and print the content and finish_reason.
  • Call again with a stop sequence and print the content and finish_reason.
  • Compare how each response was cut off.
main.py
Loading...

Expected Output

The capped run stops mid-list with finish_reason 'length'; the stop run halts right before the stop sequence with finish_reason 'stop'.

Solution

main.py
Loading...

max_tokens is a hard ceiling on output: when it is reached the response is truncated and finish_reason is length, which is the signal to raise the limit or shorten the task. A stop sequence ends generation as soon as that text would appear, giving finish_reason stop. Reading finish_reason rather than guessing is how you tell a complete answer from a truncated one, which matters when you parse or store the output.