Debugging Prompts

How to find why a prompt gives bad replies, fix it with one change at a time, and check every test case so a fix does not break other inputs.

13 minLesson 7 of 8
On this page
  1. The debugging loop
  2. Example: a ticket priority prompt
  3. Finding the cause
  4. A small test runner
  5. Pitfalls
  6. What this changes in practice
  7. What to practise next

Debugging a prompt means finding out why it produces bad replies and changing it so that they stop, without breaking the replies that were already good. A single prompt handles every input your application receives, so a change that fixes one input can quietly change the answers for others. The method that avoids this is to keep a set of test cases and run all of them after every change.

Without that set, you edit the prompt, retry the one bad input, see a good reply, and move on. A week later three other inputs have started failing, and nobody can tell which edit caused it.

The debugging loop

The loop has five steps, and you repeat steps 3 to 5 until the failures are gone.

  1. Collect test cases. Gather 5 to 20 real inputs. Include the ones that fail and a spread of ones that already work, and write down the correct answer for each.
  2. Run every case with the current prompt and record which ones pass.
  3. Read the failures next to the prompt and decide on one likely cause.
  4. Change one thing that addresses that cause.
  5. Run every case again. Keep the change only if more cases pass than before and no important case that used to pass now fails.

Example: a ticket priority prompt

A support team uses a prompt that labels each incoming ticket as urgent, normal or low. They collected six tickets with the correct label for each, then tried three versions of the prompt. The tracer shows the results of each version across the whole set.

Three prompt versions run against six test ticketsUse the arrows or ← → keys
  1. test set
    expectedv1v2v3
    Site down for allurgent
    Calm: no one can payurgent
    Feature idealow
    FURIOUS about typolow
    Reports page slownormal
    One user locked outurgent
    version
    none yet

    1The test set has six real tickets and the correct label for each. It includes tickets that already work, because they are needed to detect new breakage.

  2. test set
    expectedv1v2v3
    Site down for allurgenturgent
    Calm: no one can payurgentnormal
    Feature idealowlow
    FURIOUS about typolowurgent
    Reports page slownormalnormal
    One user locked outurgenturgent
    version
    v1
    passed
    4 / 6

    2Version 1 is "Label this ticket's priority: urgent, normal or low." Four tickets pass. The calm message about failing payments was labelled normal, and the furious message about a typo was labelled urgent.

  3. test set
    expectedv1v2v3
    Site down for allurgenturgent
    Calm: no one can payurgentnormal
    Feature idealowlow
    FURIOUS about typolowurgent
    Reports page slownormalnormal
    One user locked outurgenturgent
    version
    v2
    change
    define urgent

    3The hypothesis is that "priority" is never defined, so the model goes by how the message sounds. Version 2 adds a definition: urgent means many users are affected or data is at risk, and cosmetic problems are low.

  4. test set
    expectedv1v2v3
    Site down for allurgenturgenturgent
    Calm: no one can payurgentnormalurgent
    Feature idealowlowlow
    FURIOUS about typolowurgentlow
    Reports page slownormalnormalnormal
    One user locked outurgenturgentnormal
    version
    v2
    passed
    5 / 6

    4Both original failures are fixed, but running all six tickets shows a new one. The single locked-out user is now normal, because "many users are affected" does not describe them. Checking only the two broken tickets would have missed this.

  5. test set
    expectedv1v2v3
    Site down for allurgenturgenturgent
    Calm: no one can payurgentnormalurgent
    Feature idealowlowlow
    FURIOUS about typolowurgentlow
    Reports page slownormalnormalnormal
    One user locked outurgenturgentnormal
    version
    v3
    change
    blocked, not many

    5The definition was too narrow. Version 3 states the real goal: a ticket is urgent if anyone is fully blocked from working or data is at risk, however calm or angry the message sounds. That covers the tone problem and the single locked-out user.

  6. test set
    expectedv1v2v3
    Site down for allurgenturgenturgenturgent
    Calm: no one can payurgentnormalurgenturgent
    Feature idealowlowlowlow
    FURIOUS about typolowurgentlowlow
    Reports page slownormalnormalnormalnormal
    One user locked outurgenturgentnormalurgent
    version
    v3
    passed
    6 / 6

    6All six tickets now pass. Each version changed one thing and was checked against the whole set, so you know which change fixed which ticket and that nothing else broke.

1 / 6

The tempting fix would have been to add the rule "If the ticket mentions payroll, label it urgent." That passes the test ticket, but it fails on the next ticket, which will be about invoices instead. A fix should address what the failing cases have in common, not the wording of one case.

Finding the cause

Most prompt failures come from a short list of causes. Each one has a typical symptom and a typical fix, and the fix is a change to one part of the prompt.

Missing context

The symptom is a reply that is correct in general but generic or aimed at the wrong audience. The model only knows what is in the prompt, so if it does not know who the reply is for or what it will be used for, it produces something average. The fix is to state the audience, the purpose and any constraints, as described in Prompting Basics.

An undefined term

When a prompt uses a word like "urgent", "short" or "technical" without defining it, different inputs get different readings of that word, and the replies look inconsistent. The ticket example above is this case. The fix is to define the term and give the reason behind the definition, so the model can apply it to inputs you did not anticipate. Be Clear and Specific covers this.

A format that is described but not shown

If the content of the reply is right but the layout varies, the prompt probably describes the format in words. Models follow a worked example much more closely than a description, so add one or two examples of the exact output you want. See Show Examples.

No room for working

On problems with several steps, such as arithmetic or comparing options, a model that must give the answer first has no chance to work through the steps. Ask for the reasoning before the final answer, and ask for the answer in a fixed place so that your code can read it. Give the Model Room to Think explains the technique.

Rules that conflict or are buried

If the model follows a rule on most inputs but ignores it on some, look at the other rules around it. Two rules may pull in different directions, such as "always answer briefly" and "always explain your reasoning", or the rule may be one line in the middle of a long prompt. Say which rule wins when they conflict, and move the important rule to the beginning or end of the prompt.

A prompt that was assembled wrongly

In an application the prompt is usually a template that code fills in with the user's input and other data. If a reply makes no sense, print the exact text that was sent before you edit anything. A common finding is that a variable was empty, a document was cut off, or the user's text landed in the wrong place, and no rewording of the instructions would have helped.

Missing facts

When the reply contains confident details that are wrong, the prompt usually asks for facts it never provided. The model fills the gap with plausible text. Put the facts in the prompt, and say what to write when the answer is not in them, for example "If the policy does not cover the question, say you do not know." The background is in Why Models Hallucinate.

If you cannot see the cause, you can ask the model to explain a single wrong reply, for example "Before the label, say in one sentence which words in the ticket decided it." The explanation is not a reliable account of how the model actually decided, but it often points to the phrase it focused on.

A small test runner

Once you have more than a few cases, run them with code instead of by hand. In real use the getLabel function would send the prompt and the ticket to a model. In this sketch it is a stand-in that returns fixed answers, so you can see the output format.

function runTests(cases, getLabel) {
  const failures = [];
  for (const c of cases) {
    const got = getLabel(c.ticket);
    if (got !== c.expected) failures.push(`${c.ticket}: expected ${c.expected}, got ${got}`);
  }
  console.log(`passed ${cases.length - failures.length} / ${cases.length}`);
  for (const f of failures) console.log("  " + f);
}
 
const cases = [
  { ticket: "Calm: no one can pay", expected: "urgent" },
  { ticket: "FURIOUS about typo", expected: "low" },
  { ticket: "Reports page slow", expected: "normal" },
];
// Stand-in for a real model call, behaving like prompt v1:
const v1 = { "Calm: no one can pay": "normal", "FURIOUS about typo": "urgent", "Reports page slow": "normal" };
runTests(cases, (ticket) => v1[ticket]); // passed 1 / 3
def run_tests(cases, get_label):
    failures = []
    for c in cases:
        got = get_label(c["ticket"])
        if got != c["expected"]:
            failures.append(f'{c["ticket"]}: expected {c["expected"]}, got {got}')
    print(f"passed {len(cases) - len(failures)} / {len(cases)}")
    for f in failures:
        print("  " + f)
 
cases = [
    {"ticket": "Calm: no one can pay", "expected": "urgent"},
    {"ticket": "FURIOUS about typo", "expected": "low"},
    {"ticket": "Reports page slow", "expected": "normal"},
]
# Stand-in for a real model call, behaving like prompt v1:
v1 = {"Calm: no one can pay": "normal", "FURIOUS about typo": "urgent", "Reports page slow": "normal"}
run_tests(cases, lambda ticket: v1[ticket])  # passed 1 / 3

Replies vary from run to run because of sampling (see Sampling and Temperature), so with a real model you should run each case several times. A case that passes two times out of three is a real failure. Evals extends this runner into a proper test suite.

Pitfalls

What this changes in practice

Save every bad reply you see in production, together with the correct answer, and add it to the test set. Over time the set becomes a record of what has gone wrong, and any later change to the prompt, or to the model behind it, can be checked against it. Write down each prompt version next to its results so you can go back to a version that worked.

Try it: Find the cause, then fix it

This prompt translates app text for a Spanish release of a product called Bloom. Some replies translate the name as "Florecer", which confused testers. Name the likely cause in one sentence, then rewrite the prompt so that it fixes the cause and not just this one word.

Show a sample answer

The cause is that the prompt never says some words are names, so "Bloom" looks like an ordinary English word that should be translated.

Rewritten promptIllustrative example. Real replies vary.
You
Translate the app text below into Spanish for users in Spain. Use the informal "tú". Do not translate names of products, features or buttons. Keep these exactly as written: Bloom, Sync Hub, Quick Note. Users search for them in our help pages by their English names, so a translated name sends them nowhere. Reply with the translation only. <text> Bloom keeps your notes in sync across devices. </text>

This works because a general rule plus a list addresses the cause, and the reason lets the model treat other feature names the same way. After the change, rerun your other translation cases to check that ordinary words are still translated.

Quick check

Your change fixes the failing case. What should you do before keeping it?

Tickets written angrily are labelled urgent even when the problem is minor. What is the most likely cause?

A case passes on one run and fails on the next with the same prompt. What does that tell you?

What to practise next

Some bad replies are caused by text that someone else put into the input, which is covered in Prompt Injection, and Evals shows how to turn your test set into an automatic check.