How to beginner · 3 min read

How to use negative instructions in prompts

Quick answer
Use negative instructions in prompts by explicitly telling the AI what to avoid or exclude, such as "Do not include" or "Avoid mentioning." This technique helps steer the model away from undesired outputs and improves prompt precision.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your API key as an environment variable to authenticate requests.

bash
pip install openai>=1.0

Step by step

Use negative instructions by clearly specifying what the AI should avoid in the prompt. Below is a complete example using the gpt-4o model with the OpenAI Python SDK v1 pattern.

python
import os
from openai import OpenAI

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

prompt = (
    "Write a short story about a robot, but do not include any violence or scary elements. "
    "Avoid mentioning weapons or conflict."
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": prompt}]
)

print(response.choices[0].message.content)
output
Once upon a time, there was a friendly robot named Robo who loved helping people in a peaceful town. Robo spent his days assisting children with their homework and planting flowers in the community garden...

Common variations

You can use negative instructions with other models like claude-3-5-sonnet-20241022 or in streaming mode. Also, phrasing matters: use clear phrases like "Do not", "Avoid", "Exclude", or "No" to guide the model effectively.

python
import anthropic
import os

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

system_prompt = "You are a helpful assistant."
user_prompt = (
    "Describe a futuristic city, but exclude any mention of pollution or crime. "
    "Avoid negative or dystopian themes."
)

message = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=300,
    system=system_prompt,
    messages=[{"role": "user", "content": user_prompt}]
)

print(message.content[0].text)
output
The city gleamed with towering glass buildings and lush green parks, where clean energy powered every home and vehicle. People enjoyed vibrant cultural festivals and innovative technology that enhanced daily life...

Troubleshooting

If the AI still includes unwanted content, try rephrasing negative instructions more explicitly or placing them at the start of the prompt. Also, consider adding examples of what to avoid or use a system-level instruction to reinforce constraints.

Key Takeaways

  • Use explicit negative phrases like "Do not", "Avoid", or "Exclude" to guide AI output.
  • Place negative instructions clearly in the prompt to improve model adherence.
  • Test and rephrase instructions if the model includes unwanted content.
  • Negative instructions work across models like gpt-4o and claude-3-5-sonnet-20241022.
  • Combine negative instructions with positive guidance for best results.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗