New Tool Radar

AI Concepts

How to Stop a Model Repeating Itself With Frequency and Presence Penalties

Set frequency and presence penalties when output loops, then check the loop breaks without pushing the text off-topic.

Schematic with three steps: Confirm the loop first; Raise the penalties a little; Check the loop broke and the topic.

Because generating a word makes that word appear in its own history, language models can fall into positive feedback loops and repeat themselves. APIs provide two penalties to break the cycle: the presence penalty applies once if a word has appeared at all in the text so far, and the frequency penalty grows larger the more times a specific token is repeated. Both range from 0.0 to 2.0, and both encourage the model to introduce fresh topics and vocabulary.

What you need

  • A generation task where the model loops or repeats phrases
  • Access to the frequency_penalty and presence_penalty settings
  • A sample of the looping output to compare against

Step 1: Confirm the loop first

Generate once with both penalties at 0.0 and note where the text starts repeating. A loop is a sampling problem, not a prompt problem, when the same phrase recurs word for word.

Step 2: Raise the penalties a little

Set the frequency penalty and the presence penalty to a small positive value, for example 0.5, and generate again. The frequency penalty subtracts more for every repeat of a token; the presence penalty subtracts once a token has appeared.

Step 3: Check the loop broke and the topic held

Check the new output for the repeated phrase and for topic drift. If the loop is gone and the text stays on topic, keep the settings; if the text drifts off-topic, lower the penalties.

Worked example: synthetic repetition-penalty test

The table below is a synthetic example. It shows the expected result of a penalty test; no model was run.

Penalties Output Result
Both 0.0 Same phrase repeated 4 times Loop
Frequency 0.5, presence 0.5 Phrase appears once Loop broken
Both 2.0 No repeats, but off-topic Too strong
Choice Both 0.5 Loop broken, topic held

Expected result (UNTESTED). penalties at 0.5 break the loop while keeping the text on topic, and 2.0 breaks the loop but pushes the text off-topic. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • Both penalties range from 0.0 to 2.0; too high a value drives the model off-topic.
  • The frequency penalty grows with each repeat; the presence penalty applies once per token, however often it appears.
  • Penalties reshape the distribution; they do not fix a prompt that asks for repetition.
  • A seed makes sampling repeatable, best effort, which helps when debugging a loop.

Sources

See all