First, meet the disease — the doom loop
This page hosts a tiny language model trained on a short lighthouse story. With greedy decoding (always pick the most likely next word) it takes a deterministic path through a finite vocabulary — so it must eventually revisit a state, and from there it repeats the same phrase forever. Run it and catch the loop; the repeated stretch gets underlined.
The model is a real word-level trigram model (with a pinch of unigram smoothing) built live from a ~160-word story — a miniature of what GPT does with billions of parameters.
Now the cure — slide the tax from 1.0 to 2.0
Same model, same greedy decoding, one change: before picking, we divide each candidate's probability by penaltytimes already used. At 1.0 nothing changes. Around 1.2 the loop from EP 01 quietly dies while the text stays grammatical. Slide, run, and watch the two live meters: is there a loop, and how many distinct words survived?
Penalty 1.00 — no tax. This is plain greedy decoding: expect the exact loop from EP 01.
repetition_penalty / frequency_penalty field in every LLM API.See the bars bend — the mechanism in slow motion
Here is the model's actual next-word distribution after the word “the”. Click any candidate to pretend it already appeared in the text (click again for twice, three times…), and drag the penalty. Grey ticks show each word's original probability; the gold bars show what the penalty leaves behind. Probability doesn't vanish — it flows to the words you haven't used.
Crank it to 2.0 — and watch “the” get banned
The tax can't tell a doom loop from grammar. English needs “the” dozens of times; a story needs its hero's name on every line. Generate at a gentle 1.15, then at a harsh 1.9. Words shown in red are places where the penalty overrode the model's natural choice — hover them to see which word was blocked. Watch articles disappear and sentences turn to telegraphese.
Lists and code — where repetition is the right answer
Sometimes repeating a token isn't a rut — it's the spec. A shopping list must repeat “buy”; a program must repeat “;” and “(”. Below, a tiny model writes each format while a live checker counts well-formed items. At penalty 1.0 the structure holds; push the tax up and the checker watches semicolons and “buy” get priced out of the language.
This model samples with a little randomness (T = 0.45), so run it a few times — the trend, not any single run, is the lesson.