# Sampling

> Same model, different output: experiment with temperature, truncation strategies, and your own house rules, then put it all together in a poetry slam.

When generating text, your model offers several options for the next word.
Sampling strategies decide which one to pick, and the strategy you choose can
change the character of the output without changing the model. The same grid or
cutouts spread can produce flat prose, structured poetry, or pure nonsense,
depending on how you sample.

![](../../assets/images/hero-sampling.avif)

## You will need

- a completed model from an earlier module
- pen, paper, and dice as per [Generation](https://www.llmsunplugged.org/modules/generation/) (grid method),
  or the ledger sheets and a cup of counters (ledger method)

## Your goal

Generate text using at least two temperatures and at least two truncation
strategies. Stretch goal: design a whole poetry model of your own (training
text, temperature, house rules) and hand it to another pair to build and
perform in the [poetry slam](#poetry-slam).

## Key idea

Sampling choices (<GlossaryTerm id="temperature">temperature</GlossaryTerm> and <GlossaryTerm id="truncation-strategy">truncation</GlossaryTerm>) can make the same model sound
cautious, wild, repetitive, or inventive.

## Temperature

Temperature is a dial on how closely the model follows its own counts. Turned
down, it sticks to the favourites; turned up, it treats every option as roughly
equal. A version you can run by hand has four settings:

- **cold**: no dice; take the option with the highest count (if there's a tie,
  roll among the tied ones). This is <GlossaryTerm id="greedy-sampling">greedy decoding</GlossaryTerm>.
- **normal**: roll on the counts exactly as they are, as in
  [Generation](https://www.llmsunplugged.org/modules/generation/).
- **hot**: before you roll, add 1 to _every_ option's count, then roll on the
  new totals. The favourites are still ahead, but by less.
- **boiling**: ignore the counts and roll evenly among all the options.

Example with counts `spot` (4), `run` (2), `jump` (1), `.` (1):

- cold → `spot`, every time
- normal → `spot` 4 in 8, `run` 2 in 8, `jump` and `.` 1 in 8 each
- hot → counts become 5, 3, 2, 2, so `spot` 5 in 12, `run` 3 in 12, `jump` and
  `.` 2 in 12 each, so the rare words have gone from 1 in 8 to 1 in 6
- boiling → 1 in 4 each

Turn the dial up further by adding 2 (or 3) to every count instead of 1; the
bigger the number you add, the closer you get to boiling. Dividing every count
by the same number won't do it, because that leaves the odds exactly where they were.

## Truncation strategies

Truncation narrows which next-word options are allowed. Mix and match with
temperature.

<Callout type="tip" title="Booklet shortcuts">

When using a [pre-trained booklet](https://www.llmsunplugged.org/modules/pretrained-generation/), the next-word
candidates are already sorted from most likely to least likely. This makes
several strategies easier: greedy = pick the first option, non-sequitur = pick
the last option, and top-k = only roll among the first k options.

</Callout>

<Callout type="tip" title="Ledger shortcuts">

With [ledger sheets](https://www.llmsunplugged.org/modules/generation/) and a cup of counters, the dial and
the cut both happen as you fill the cup. Cold = no draw: take the box with the
most marks. Hot = add one extra counter of each colour in use on the row, and
boiling = put in just one counter of each colour in use. Top-k = only the k
boxes with the most marks go in the cup. The printed picture-book sets are
already top-4: each word keeps only its four most common followers, so it fits
one row of four boxes.

</Callout>

<div class="at-card-grid at-card-grid--3">
<div class="at-card"><div class="at-card-body">

### Greedy

Pick the highest count; if tied, roll among the top options.

To look a few words ahead before committing, see
[Beam Search](https://www.llmsunplugged.org/modules/beam-search/).

</div></div>
<div class="at-card"><div class="at-card-body">

### Haiku

Track syllables per line (5-7-5). Roll as normal; if the word would overflow the
line's syllable limit, re-roll.

</div></div>
<div class="at-card"><div class="at-card-body">

### Non-sequitur

Pick the lowest non-zero count; if tied, roll among the least likely options.

</div></div>
<div class="at-card"><div class="at-card-body">

### No-repeat

Track words used in the current sentence. If you roll a repeat, reroll; if
nothing valid remains, insert `.` and continue.

</div></div>
<div class="at-card"><div class="at-card-body">

### Alliteration

Prefer options that start with the same letter/sound as the previous word;
otherwise sample normally.

</div></div>
<div class="at-card"><div class="at-card-body">

### Top-k

Choose a number k (e.g. 2 or 3). Keep only the k options with the highest
counts; if tied for the last spot, include all ties. Roll among those only.

</div></div>
<div class="at-card"><div class="at-card-body">

### Alphabet chain

The next word must start with the last letter of the previous word. If no option
qualifies, sample normally.

</div></div>
<div class="at-card"><div class="at-card-body">

### Short/long

Pick a length threshold (e.g. 4 letters). Only allow words at or below that
length (short mode) or above it (long mode). If nothing qualifies, re-roll.

</div></div>
</div>

## Poetry slam

This uses everything above: design a poetry model, hand it to another pair, and
see what they make of it. Allow about an hour: 20 minutes to design, 10 to
prepare, 15 for the slam, 5 to talk about it.

### Design your model (20 min)

In pairs, design a poetry language model from scratch around three decisions:

- **training text**: a [pre-trained booklet](https://www.llmsunplugged.org/modules/pretrained-generation/),
  your own [grid](https://www.llmsunplugged.org/modules/training/), or a <GlossaryTerm id="cutouts">cutouts spread</GlossaryTerm>
- **temperature**: cold, normal, hot, or boiling
- **house rules**: one or more of the truncation strategies above, or a rule you
  invent yourself

Two things to argue about while you decide: what isn't poetry, and what your
model does about it.

Write the recipe on a blank card, complete enough that another pair can run it
without asking you anything:

```text
training text:
temperature:
house rules:
starting word:
stop rule:      (e.g. stop after 20 words, or at the third full stop)
```

### Prep (10 min)

Swap cards with another pair. You have ten minutes to generate as much text as
you can from _their_ model, then plan a one-minute performance of the result.
Read it, chant it, or split it between you.

### The slam (~15 min)

Each pair performs for about a minute. A room of eight pairs runs to roughly
fifteen minutes with applause.

### Discussion (5 min)

What was the hardest part of designing your model? The hardest part of preparing
a performance from someone else's card? And how does this relate to <GlossaryTerm id="chatbot">Claude or
ChatGPT</GlossaryTerm>? Two words to name here are _temperature_
(the same dial, on a much bigger model) and <GlossaryTerm id="prompt">prompt engineering</GlossaryTerm> (wording an
input to get the output you want, which is what a model card is).

<Callout type="tip" title="What counts as done">

The model card plus the other pair's performance is the completion task. A model
that produced something strange has still succeeded, as long as the recipe was
followable, and the pair performing it have done their job.

</Callout>

## Instructor notes

### Discussion questions

- which strategy produces the most "human-like" text?
- when would you want predictable vs surprising output?
- how do constraints (haiku, no-repeat) spark creativity?
- can you invent your own sampling strategy?
- (slam) which parts of your model card did the other pair have to guess at?
  What does that suggest about writing a prompt for an LLM?
- (slam) when a performance worked, how much of that was the model and how much
  the performers?

### Troubleshooting

- **"On hot, the row doesn't add up to 10 any more."** It never had to: share
  the dice faces out across the new totals the same way as in
  [Generation](https://www.llmsunplugged.org/modules/generation/), or roll two d10 as a
  number from 1 to 100 and scale.
- **"Isn't hot just the same odds with bigger numbers?"** No---adding the same
  amount to every count narrows the _ratio_ between favourite and rare. Adding
  1 takes `spot` from four times as likely as `jump` to two and a half times.
  Dividing would leave the ratio alone.

### Connection to current LLMs

Current <GlossaryTerm id="llm">LLMs</GlossaryTerm> use these same mechanisms, though the specific strategies differ.

- **Temperature control:** a real model scores every possible next <GlossaryTerm id="token">token</GlossaryTerm>, and
  the temperature parameter divides those scores _before_ they're turned into
  probabilities. Below 1 the distribution sharpens towards the favourite; above
  1 it flattens. Your dial does the same job with counts: cold is temperature
  near zero (greedy), boiling is temperature near infinity (uniform), and hot
  sits in between. LLMs apply it computationally before every token.
- **Truncation techniques in modern LLMs:** <GlossaryTerm id="top-k-sampling">top-k sampling</GlossaryTerm> (only consider k most
  likely tokens), <GlossaryTerm id="top-p-sampling">top-p/nucleus sampling</GlossaryTerm> (consider tokens until cumulative
  probability reaches p), <GlossaryTerm id="repetition-penalty">repetition penalties</GlossaryTerm>, frequency penalties, and
  presence penalties all prune options before sampling.
- **Truncation techniques in this module:** greedy, haiku, non-sequitur,
  no-repeat, alliteration, top-k, alphabet chain, and short/long are designed
  for dice-based sampling but work on the same idea, changing which tokens are
  eligible before you roll. Top-k directly mirrors the top-k parameter in LLM
  APIs.

Your paper model has two controls: temperature (the shape of the <GlossaryTerm id="probability-distribution">probability distribution</GlossaryTerm>) and truncation
(which tokens to exclude). The same trained model can produce scholarly essays
(low temperature, strict truncation) or wild poetry (high temperature,
constraint-based truncation) just by changing these parameters.

Source: <https://www.llmsunplugged.org/modules/sampling/>\
Licence: [CC-BY-SA-4.0](https://creativecommons.org/licenses/by-sa/4.0/)\
Cite as: Swift, B. (2026). _LLMs Unplugged: Understand how AI language models work by building one yourself._ https://doi.org/10.5281/zenodo.17403824
