Inferensys

Glossary

Repetition Penalty

A decoding parameter that applies a penalty to the logits of tokens that have already been generated in the sequence, discouraging the model from producing repetitive or looping text.
Developer working on RAG retrieval system, document chunks visible on screen, technical workspace with code editor.
DECODING PARAMETER

What is Repetition Penalty?

A mechanism to suppress degenerate text loops in language model generation.

A repetition penalty is a decoding parameter that modifies the probability scores (logits) of tokens that have already appeared in the generated sequence, directly discouraging a language model from producing repetitive or looping text. By applying a multiplicative penalty to previously generated tokens during each step of autoregressive generation, it reduces the likelihood that those tokens will be selected again, promoting lexical diversity.

This technique is distinct from frequency penalty, which penalizes tokens based on their overall count in the sequence. The repetition penalty applies a fixed discount regardless of how many times a token has appeared, making it particularly effective at breaking immediate, adjacent loops. It is a critical control in answer synthesis pipelines to prevent degenerate outputs when models over-rely on high-probability phrases from retrieved context.

DECODING CONTROL

Key Characteristics of Repetition Penalty

A critical inference-time parameter that modifies token probabilities to suppress previously generated tokens, preventing degenerate loops and improving output diversity.

01

Logit Modification Mechanism

Repetition penalty operates directly on the raw logits (unnormalized scores) before the softmax function. For each token already present in the generated sequence, the penalty subtracts a value from its logit or scales it down, making it less likely to be sampled again. This is distinct from frequency penalty, which applies a penalty proportional to how many times a token has appeared. The standard formula applies a multiplicative penalty p where logit_i = logit_i / p if logit_i > 0 and logit_i = logit_i * p if logit_i < 0, preventing sign inversion.

02

Degenerate Loop Prevention

Without a repetition penalty, language models are prone to positive feedback loops where high-probability tokens reinforce themselves. A classic failure mode is infinite repetition of a phrase like 'I am. I am. I am.' The penalty breaks this cycle by progressively reducing the probability of tokens that have already been emitted. This is especially critical in greedy decoding and beam search, where deterministic selection amplifies the model's tendency to get stuck in local probability maxima.

03

Diversity vs. Coherence Trade-off

Setting the penalty too high introduces a distinct failure mode: the model avoids not just repeated words but also functionally necessary tokens like 'the', 'a', and 'is'. This produces grammatically broken, nonsensical output. Typical values range from 1.0 (no penalty) to 1.2, with values above 1.5 often degrading quality. The optimal setting is task-dependent:

  • Creative writing: 1.1–1.2 for lexical diversity
  • Factual summarization: 1.0–1.05 to preserve named entities
  • Code generation: 1.0, as repetition of syntax tokens is expected
04

N-gram vs. Token-Level Application

Advanced implementations extend the penalty beyond single tokens to n-gram sequences. An n-gram repetition penalty (often called no_repeat_ngram_size) prevents any sequence of n consecutive tokens from appearing more than once. This is particularly effective at preventing the model from repeating entire sentences or paragraphs verbatim. For example, setting no_repeat_ngram_size=3 ensures no trigram appears twice, forcing the model to rephrase concepts rather than copy-pasting prior output.

05

Interaction with Sampling Strategies

Repetition penalty interacts non-trivially with other decoding parameters:

  • Top-k sampling: The penalty is applied before the top-k filter, so penalized tokens may still be excluded if they fall below the threshold
  • Top-p (nucleus) sampling: Penalty reshapes the cumulative probability mass, potentially excluding tokens that would otherwise be in the nucleus
  • Temperature: High temperature flattens distributions, amplifying the relative effect of the penalty; low temperature sharpens distributions, making the penalty less impactful
  • Beam search: Penalty must be applied per-beam to prevent all beams from collapsing to the same repetitive sequence
06

Context Window Considerations

The scope of repetition tracking is a critical design choice. A full-context penalty tracks all tokens from the beginning of the generation, which can unfairly penalize common words in long outputs. A sliding window penalty only considers the last N tokens, allowing natural lexical recycling over longer distances. Some implementations exclude stop words and punctuation from penalty calculation entirely, recognizing that these function words must repeat frequently for grammatical English. The repetition_penalty_range parameter in many inference engines controls this window size.

DECODING STRATEGY COMPARISON

Repetition Penalty vs. Related Decoding Parameters

A comparison of Repetition Penalty with other decoding parameters that control token selection, diversity, and output quality in language models.

FeatureRepetition PenaltyTemperatureTop-p (Nucleus Sampling)Frequency Penalty

Primary Mechanism

Penalizes logits of previously generated tokens

Scales logits to flatten or sharpen probability distribution

Truncates sampling to smallest set of tokens with cumulative probability p

Penalizes logits proportionally to token occurrence count in sequence

Target Problem

Degenerate looping and phrase repetition

Overly deterministic or random outputs

Tail probability mass from low-quality tokens

Word and phrase overuse within a single generation

Applied Per Token

Modifies Logits Before Softmax

Penalty Based on Token Frequency

Penalty Based on Token Presence

Typical Value Range

1.0–1.2 (above 1.0 penalizes)

0.0–2.0 (lower = more deterministic)

0.9–1.0 (higher = more diverse)

0.0–2.0 (higher = stronger penalty)

Risk of Over-Application

Degraded syntactic fluency and unnatural phrasing

Nonsensical output at high values

Truncation of viable low-probability tokens

Excessive penalty on legitimate repeated function words

DECODING PARAMETERS

Frequently Asked Questions

Explore the mechanics of repetition penalty, a critical decoding parameter used to prevent language models from generating degenerate, looping text during answer synthesis.

A repetition penalty is a decoding parameter that modifies the probability distribution of the next token by applying a penalty to the logits of tokens that have already appeared in the generated sequence. During autoregressive generation, the model calculates a score for every token in its vocabulary. Before sampling the next token, the repetition penalty algorithm identifies tokens present in the preceding context window and divides their logits by a hyperparameter value (typically between 1.0 and 1.2). This logit manipulation reduces the likelihood of those tokens being selected again, effectively discouraging the model from producing repetitive or looping text. Unlike frequency penalty, which scales with the number of occurrences, a standard repetition penalty applies a flat, binary penalty based solely on prior presence, making it a strict gate against immediate self-repetition.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.