Repetition Penalties › Worked Example: Tracing a Penalty Step by Step
Language AI HandbookAll chapters Repetition Penalties › Worked Example: Tracing a Penalty Step by StepOne section · Sections in this chapter8 / 13 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
To solidify understanding, let's trace through a concrete numerical example that shows exactly how frequency penalty modifies the generation decision at one step. We'll use a toy vocabulary of 5 tokens and walk through every calculation.
Suppose we are generating text and have just produced the sequence: "the cat sat on the mat and the cat". The current context window contains these 8 tokens. We track their counts:
- "the": appeared 3 times
- "cat": appeared 2 times
- "sat": appeared 1 time
- "on": appeared 1 time
- "mat": appeared 1 time
- "and": appeared 1 time
The model produces the following raw logits for the next token (we use a tiny vocabulary for clarity):
| Token | Raw Logit |
|---|---|
| "the" | 3.2 |
| "cat" | 2.8 |
| "sat" | 2.1 |
| "again" | 1.5 |
| "purred" | 1.0 |
Without any penalty, softmax would strongly favor "the" and "cat". Let's apply frequency penalty with .
Step 1: Compute modified logits. For each token, subtract :
- "the":
- "cat":
- "sat":
- "again": (no penalty: first use)
- "purred": (no penalty: first use)
Step 2: Apply softmax to modified logits. We compute for each:
- "the":
- "cat":
- "sat":
- "again":
- "purred":
Sum:
Probabilities: "the" = 0.231, "cat" = 0.256, "sat" = 0.209, "again" = 0.189, "purred" = 0.115
Step 3: Compare to unpenalized probabilities. With the original logits, the top choices were overwhelmingly "the" and "cat". After frequency penalty, "cat" is still favored but by a much smaller margin, and "again" and "purred" have become viable competitors. The penalty has not eliminated the model's preference but has substantially leveled the playing field, giving novel tokens a real chance at selection.
The key insight from this example is that frequency penalty is most effective when the penalized tokens have modest logit advantages over alternatives. If "the" had a logit of 10.0 rather than 3.2, the same with count 3 would reduce it to 8.5, which would still completely dominate. This is why very confident model predictions can resist even strong penalties, and why n-gram blocking is sometimes necessary as a fallback guarantee.