P

Frequency Penalty: Definition and Examples

The Frequency Penalty is a language model parameter that penalizes tokens proportionally to the number of times they appear in the generated text, thus reducing repetitions.

Full definition

The Frequency Penalty is a hyperparameter available in language model APIs such as OpenAI's GPT. It applies an increasing penalty to each token based on how many times that token has already been used in the generated response. The more frequently a word appears, the less likely it is to be selected again.

Concretely, this parameter works by subtracting a value proportional to the number of occurrences of a token from the logits (probability scores) before sampling. For example, with a Frequency Penalty of 0.5, a word that has appeared 3 times will have its score reduced by 1.5 (3 × 0.5). This penalty is linear: it increases with each new occurrence, unlike the Presence Penalty which applies a fixed penalty upon first appearance.

The parameter typically accepts values between 0 and 2. At 0, no penalty is applied and the model can freely repeat the same words. Moderate values (0.3-0.8) reduce repetitions while keeping the text natural. High values (above 1.5) force the model to use very varied vocabulary, risking artificial or incoherent phrasing.

In prompt engineering, the Frequency Penalty is particularly useful for creative content generation, article writing, or any task where lexical diversity is desirable. It differs from the Temperature parameter, which controls overall randomness, by specifically targeting the issue of word repetition.

Etymology

The term comes from English 'frequency' (number of occurrences) and 'penalty' (penalty). It literally describes the mechanism: a penalty applied based on the frequency of appearance of a token. This concept originates from decoding techniques in natural language processing (NLP), where different penalization strategies have been developed to improve the quality and diversity of generated texts.

Concrete examples

Writing a blog article without repetitions

Write an article about the benefits of sleep. Use varied vocabulary and avoid repetitions. [frequency_penalty: 0.6]

Generating creative advertising slogans

Generate 10 original slogans for an artisanal coffee brand. Each slogan must use different words. [frequency_penalty: 1.0]

Technical answer requiring precision without penalty

Explain how a compiler works step by step. [frequency_penalty: 0] — Here, we keep the penalty at 0 because repetition of technical terms is necessary for clarity.

Practical usage

Use a Frequency Penalty between 0.3 and 0.8 for long-form content to avoid repetitive phrasing while keeping text natural. For technical or factual tasks where repetition of key terms is normal, keep it at 0. Combine it with the Temperature parameter for fine control: Temperature manages overall creativity, while Frequency Penalty specifically targets repetitions.

Related concepts

Presence PenaltyTemperatureTop-P (Nucleus Sampling)Logits

FAQ

What is the difference between Frequency Penalty and Presence Penalty?
The Frequency Penalty applies a penalty proportional to the number of occurrences of a token (the more a word is repeated, the more it is penalized). The Presence Penalty applies a fixed penalty as soon as a token appears at least once, regardless of the number of repetitions. In summary, the Frequency Penalty combats excessive repetitions, while the Presence Penalty encourages the introduction of new topics.
What default Frequency Penalty value should I use?
For most uses, a value between 0.3 and 0.6 offers a good balance between diversity and naturalness. For creative content (poetry, slogans), go up to 0.8-1.2. For technical responses or code, keep it at 0 or very low (0.1-0.2) because repetition of specific terms is often necessary.
Is the Frequency Penalty available on all AI models?
No. This parameter is mainly available in OpenAI's API (GPT-3.5, GPT-4). Other providers offer similar mechanisms under different names or integrated into other parameters. For example, Anthropic's API (Claude) does not directly offer this parameter, as its models handle lexical diversity differently through other internal techniques.

See also

How to use this prompt

  1. Copy the prompt with the button above.
  2. Paste it into ChatGPT, Claude or your favorite AI assistant.
  3. Replace the bracketed variables with your details, then refine the result.

About Prompt Guide

Prompt Guide is a free library of 2500+ ready-to-use prompts for ChatGPT, Claude and other AIs, with guides to learn prompting and tools to build and optimize your own prompts.

More definitions

Get new prompts every week

Join our newsletter.