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.
Glossary
Repetition Penalty

What is Repetition Penalty?
A mechanism to suppress degenerate text loops in language model generation.
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.
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.
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.
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.
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
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.
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
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.
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.
| Feature | Repetition Penalty | Temperature | Top-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 |
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.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Repetition penalty is one of several decoding strategies used to control the quality and diversity of generated text. These related concepts form the toolkit for fine-tuning language model output.
Temperature Calibration
A hyperparameter that controls the randomness of token predictions by scaling the logits before softmax. Lower values (e.g., 0.2) make the model more deterministic, favoring high-probability tokens. Higher values (e.g., 0.8) flatten the distribution, increasing diversity but risking incoherence. Unlike repetition penalty, temperature affects the entire vocabulary uniformly rather than targeting previously generated tokens.
Top-k Sampling
A decoding strategy that restricts the model to selecting from only the k most probable next tokens at each step. After filtering, probabilities are renormalized. This prevents the model from considering highly unlikely tokens that could derail coherence. Top-k is often combined with repetition penalty to simultaneously enforce diversity and suppress loops.
Top-p (Nucleus) Sampling
An alternative to top-k that selects from the smallest set of tokens whose cumulative probability exceeds a threshold p. This dynamically adjusts the candidate pool based on the confidence distribution. A value of p=0.9 means the model considers only tokens comprising 90% of probability mass. More adaptive than fixed-k approaches.
Frequency Penalty
A related but distinct penalty that applies a uniform penalty to a token based on its total count in the generated sequence so far. Unlike repetition penalty, which penalizes tokens regardless of frequency, frequency penalty scales with how many times a token has appeared. This allows occasional reuse while discouraging overuse.
Presence Penalty
A binary-style penalty that applies a flat penalty to any token that has appeared at least once in the generated text. This encourages the model to introduce new vocabulary and topics rather than revisiting already-mentioned concepts. More aggressive than frequency penalty for promoting topical diversity.
Self-Consistency
A decoding strategy that samples multiple diverse reasoning paths using a non-zero temperature, then selects the most consistent final answer via majority voting. This improves performance on tasks with a fixed answer by exploring multiple chains of thought. Often used alongside repetition penalty to ensure each sampled path remains coherent.

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us