How to Intermediate · 3 min read

How to A/B test prompts in production

Quick answer
Use controlled experiments by randomly splitting user requests between different prompt variants and collecting response metrics. Implement logging and analytics to compare performance, then deploy the best-performing prompt in production.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0
  • Basic knowledge of A/B testing concepts

Setup

Install the openai Python SDK and set your API key as an environment variable for secure access.

bash
pip install openai>=1.0
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl
Installing collected packages: openai
Successfully installed openai-1.x.x

Step by step

This example demonstrates a simple A/B test by randomly selecting between two prompt variants for each user request, sending it to the gpt-4o model, and logging the results.

python
import os
import random
from openai import OpenAI

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

# Define two prompt variants
prompt_variants = {
    "A": "Summarize the following text concisely:",
    "B": "Provide a brief summary of this text:"
}

# Simulate user input
user_input = "Artificial intelligence is transforming industries worldwide."

# Randomly assign variant
variant = random.choice(["A", "B"])
prompt = f"{prompt_variants[variant]}\n{user_input}"

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

# Extract output
output = response.choices[0].message.content

# Log results (here we just print, but in production send to analytics)
print(f"Variant: {variant}")
print(f"Prompt used: {prompt}")
print(f"Model response: {output}")
output
Variant: A
Prompt used: Summarize the following text concisely:
Artificial intelligence is transforming industries worldwide.
Model response: AI is revolutionizing various industries globally by enabling new capabilities and efficiencies.

Common variations

  • Use async calls with asyncio for high throughput.
  • Test more than two prompt variants by expanding the prompt_variants dictionary.
  • Stream responses for faster user feedback using stream=True in chat.completions.create.
  • Use different models like gpt-4o-mini or claude-3-5-sonnet-20241022 depending on cost and latency requirements.

Troubleshooting

  • If you see API rate limit errors, implement exponential backoff and retry logic.
  • Ensure environment variable OPENAI_API_KEY is set correctly to avoid authentication errors.
  • Validate that prompt variants are meaningfully different to detect performance differences.
  • Use consistent metrics like response length, user engagement, or task success rate to evaluate variants objectively.

Key Takeaways

  • Randomly split user requests between prompt variants to run controlled A/B tests.
  • Log prompt versions and model outputs for reliable performance comparison.
  • Use metrics aligned with your product goals to decide the winning prompt.
  • Leverage async and streaming APIs for scalable and responsive testing.
  • Validate environment setup and handle API limits to ensure smooth production runs.
Verified 2026-04 · gpt-4o, gpt-4o-mini, claude-3-5-sonnet-20241022
Verify ↗