Concept beginner · 3 min read

What is presence penalty in OpenAI API

Quick answer
In the OpenAI API, presence_penalty is a parameter that decreases the likelihood of the model repeating tokens that have already appeared in the generated text. It encourages the model to introduce new topics or words, enhancing output diversity and reducing redundancy.
Presence penalty is a parameter in the OpenAI API that reduces the chance of the model repeating tokens already present in the text, promoting more diverse and novel outputs.

How it works

Presence penalty works by applying a negative score to tokens that have already appeared in the generated text, making the model less likely to reuse them. Think of it as a penalty for repetition: the higher the penalty, the more the model avoids repeating words or phrases it has already used. This encourages the model to explore new topics or vocabulary, increasing the creativity and variety of the output.

Concrete example

The following Python example uses the OpenAI SDK to generate text with and without presence_penalty. Notice how increasing the penalty reduces repetition.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

messages = [{"role": "user", "content": "Write a short story about a cat and a dog."}]

# Without presence penalty
response_no_penalty = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    max_tokens=100
)

# With presence penalty
response_with_penalty = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    max_tokens=100,
    presence_penalty=0.6
)

print("Without presence penalty:\n", response_no_penalty.choices[0].message.content)
print("\nWith presence penalty (0.6):\n", response_with_penalty.choices[0].message.content)
output
Without presence penalty:
Once upon a time, there was a cat and a dog who lived in the same house. The cat loved to nap in the sun, while the dog enjoyed chasing balls.

With presence penalty (0.6):
In a cozy little town, a curious cat and a playful dog became unlikely friends. They explored gardens, climbed trees, and discovered new adventures every day.

When to use it

Use presence_penalty when you want to reduce repetitive phrases or words in generated text, such as in creative writing, brainstorming, or dialogue generation. Avoid using it when repetition is desired for emphasis or style, or when generating factual or structured content where consistency is important.

Key terms

TermDefinition
Presence penaltyA parameter that reduces the likelihood of repeating tokens already present in the generated text.
TokenA piece of text, such as a word or part of a word, used as the basic unit of language modeling.
RepetitionThe occurrence of the same word or phrase multiple times in generated text.
OpenAI APIAn API providing access to OpenAI's language models for text generation and other tasks.

Key Takeaways

  • Presence_penalty reduces repetition by discouraging reuse of tokens already in the output.
  • Higher presence_penalty values increase output diversity and creativity.
  • Use presence_penalty in creative or exploratory text generation, not when repetition is stylistically needed.
Verified 2026-04 · gpt-4o
Verify ↗