The most surprising thing about A/B testing prompts is that the "better" prompt often isn’t the one that’s more human-sounding, but the one that more precisely constrains the LLM’s output.

Let’s see this in action. Imagine we’re building a customer support bot that needs to extract a user’s order ID from their message.

Prompt A (More Human-like):

"Hey there! I'm having trouble with my recent order. Can you help me find my order number? I think it's something like 12345-ABC."

Prompt B (More Constrained):

"Extract the order ID from the user's message. The order ID is a string that starts with one or more digits, followed by a hyphen, and then followed by one or more alphanumeric characters. If no order ID is found, return 'NOT_FOUND'.

User message: 'Hey there! I'm having trouble with my recent order. Can you help me find my order number? I think it's something like 12345-ABC.'"

If we feed these prompts to an LLM (say, gpt-3.5-turbo via the OpenAI API) and ask it to extract the order ID, we might get:

  • For Prompt A: The LLM might respond with something conversational like, "I can help with that! Could you please provide your order number?" or it might try to guess and get it wrong, "Is your order number 12345-ABC?"
  • For Prompt B: The LLM will likely output exactly 12345-ABC.

This illustrates the core principle: A/B testing prompts isn’t just about tweaking wording; it’s about systematically evaluating how different instructions influence the LLM’s behavior and output format.

The problem Prompt A tries to solve is simple extraction, but LLMs are designed for generation. Without explicit instructions, they default to their generative nature, often leading to verbose, unhelpful, or incorrect responses for structured tasks. Prompt B solves this by providing a clear, unambiguous instruction and a defined output format.

Internally, the LLM processes both prompts. For Prompt A, it recognizes the user’s intent ("help me find my order number") and generates a response that acknowledges the request. For Prompt B, it prioritizes the explicit instruction ("Extract the order ID") and follows the defined format. The key levers you control are:

  1. Instruction Clarity: How directly do you tell the LLM what to do?
  2. Output Formatting: Do you specify how the output should look (e.g., JSON, plain text, specific delimiters)?
  3. Few-Shot Examples: Providing examples of input-output pairs can dramatically improve performance for complex tasks.
  4. Role-Playing: Assigning a persona (e.g., "You are a helpful assistant that only provides JSON output") can guide behavior.
  5. Context Window Management: For longer interactions, how you present the relevant information matters.

When A/B testing, you’d typically set up a system that splits incoming user requests. Half get Prompt A, half get Prompt B. You then measure a predefined success metric. For our order ID example, this metric might be:

  • Success Rate: The percentage of requests where the correct order ID was extracted.
  • Response Latency: How quickly the LLM responded.
  • Cost: The token usage for each prompt/response.

Let’s say our test runs for 1,000 requests.

  • Prompt A: 750 requests (75%) successfully extracted the order ID. Average response time: 2.5 seconds. Average tokens: 150.
  • Prompt B: 980 requests (98%) successfully extracted the order ID. Average response time: 1.8 seconds. Average tokens: 80.

In this scenario, Prompt B is the clear winner. It’s more reliable and efficient.

A common pitfall is believing that more complex language or a longer prompt will always yield better results. In reality, for many structured tasks, brevity and explicit, almost programmatic, instructions are far more effective. The LLM isn’t "reading" your prompt like a human; it’s predicting the most probable sequence of tokens based on its training data, guided by the specific tokens you provide. If you want it to output a specific format, you need to provide tokens that strongly bias it towards that format. For instance, ending your prompt with "Order ID: " before the LLM generates its response can often lead to it completing that line with the extracted ID.

The next step after optimizing for extraction is handling variations in how users might present the order ID.

Want structured learning?

Take the full Prompt-engineering course →