Debugging a failing prompt
When a prompt gives a wrong answer, most people rewrite it and hope. Watch that fail at a train booking desk, then follow a five-step method that finds the real cause. First an empty slot, then two rules that contradict each other.
11 minFree
- Reproduce
- Read
- Find
- Change one
- Re-run
Press Play to watch it run, or step through with ← →. Then switch to Practice and run it yourself.
You are the help desk assistant for Sharma Travels, Lucknow.
We book train tickets for our customers.
<charges>
</charges>
Rules:
- Answer the customer's question in one or two short sentences.
- Give amounts in rupees.
Customer: What is your service charge for one sleeper ticket?Debug notes
Nothing yet. Press Play.
Background: the idea in plain words
Your prompt worked in testing. Now a real customer gets a wrong answer. What do you do?
The natural reaction is to open the prompt and start rewriting. Add "IMPORTANT". Add "be accurate". Move some lines around. Try it once and see. Sometimes the answer changes, which feels like progress. But you have not found out why it failed, so you cannot know that it is fixed.
Debugging means finding the cause of a failure and removing it. For a prompt, the key fact is this: the model can only work with the text it actually received. That text is often not what you think you sent. A prompt in a real app is put together by code from a template (fixed text with slots), data that fills the slots, and sometimes documents and chat history. Any of those can go wrong.
So a prompt can fail in a few different ways. A piece of context (the information the model needs) can be missing. Two instructions can contradict each other. An example can pull the answer in a direction you did not intend. From the outside, all three look the same: a wrong answer. The fix for each is different, which is why guessing wastes time.
This lesson gives you one method with five steps. Reproduce the failure. Read exactly what the model received. Find the part that is missing or contradictory. Change one thing. Run the test set again.
The running example is a travel agent in Lucknow that books train tickets. Its help desk bot answers questions about the agency's own service charges. One customer is told the charge is Rs 20 when it is really Rs 40.
Press Play to watch the guess-and-rewrite approach go nowhere. Then switch Try it to see the method find the same bug, and then a second bug with a different cause.
What to notice
- A changed answer is not a fixed answer. After three edits the bot said Rs 25 in place of Rs 20. It looked like movement. The cause was untouched.
- Read what was sent, not what you wrote. The template looked fine. The request that reached the model had an empty charges block. You only see that in the log of the real request.
- Many prompt bugs are not wording bugs. The first fix was in the code that fills the slot. Not one word of the prompt changed.
- "Sometimes wrong" is a clue. When the same prompt gives different shapes of answer on different runs, look for instructions that cannot both be obeyed.
- One change, then the whole test set. One change tells you what fixed it. The whole set tells you that nothing else broke. Each bug you fix leaves a new test case behind.
Go deeperThe five steps
| Step | What you do | Why |
|---|---|---|
| 1. Reproduce | Save the exact failing input and the output you expected, as a test case. Run it, more than once if the failure comes and goes | A failure you cannot repeat cannot be studied, or proved fixed |
| 2. Read | Open the full request the model received, after all slots were filled. Read it top to bottom | The bug is usually in the gap between what you meant to send and what was sent |
| 3. Find | Ask: is something missing? Do two parts disagree? Is an example steering the answer? | Different causes need different fixes |
| 4. Change one thing | Make the smallest change that removes the cause | If you change three things, you cannot tell which one worked |
| 5. Run the tests again | Run the new case and every old one | A fix for one input can break another (see Testing prompts with evals) |
For step 2, Anthropic's guide offers a useful test. Show the prompt to a colleague who has very little context on the task, and ask them to follow it. If they would be confused, the model will be too. For debugging, give them the real request from the log, empty slots and all.
OpenAI's evaluation guide supports steps 1 and 5 from the other side: log everything during development so that real failures can become test cases, and run your evals on every change.
Go deeperThree common root causes
The animation showed the first two. The third is just as common.
1. Missing context. The model was asked about something it was never given. Google's guide puts the rule simply: include in the prompt the instructions and information the model needs to solve the problem. Check the slots, the attached documents and the history. If the information is there but the model ignored it, that is a different problem: see Grounding and saying "I don't know".
2. Contradicting instructions. Two rules that cannot both be followed. They usually arrive months apart, written by different people. One OpenAI model guide reports that such contradictions are especially harmful, because the model spends effort trying to satisfy both. The fix is not to shout one of them louder. Say when each rule applies, or which one wins.
3. An example the model copies too closely. Suppose the booking desk prompt has a single example:
Customer: Is my ticket for tomorrow confirmed?
Reply: Yes, your ticket is confirmed. Happy journey!Now a customer writes that they had to cancel their trip and asks about the agency's refund. The reply ends with "Happy journey!". Nobody wrote a rule saying to end that way. The model copied it from the only example it had.
The guides explain why. Examples are one of the strongest signals in a prompt. Anthropic's guide says to make examples diverse, so the model does not pick up unintended patterns. Google's guide warns that the model can overfit to examples (follow them too closely). The fix is to change the examples, not to add a rule against one phrase: use a few examples that differ in topic, length and ending, so that only the pattern you want is common to all of them. Few-shot examples have their own lesson: Few-shot examples.
Signs of missing context
The answer is confident and specific, but the detail is nowhere in the request. A slot, a document or a tool result is empty or out of date.
Signs of a contradiction
The answer changes shape between runs. Each version obeys some rules and breaks others. Words like "always" and "never" appear in rules that can clash.
Go deeperWhen the cause is not in the prompt
Sometimes you read the request and everything is there, clear and consistent, and the model still gets it wrong. Then the remaining options are fewer and you can try them one at a time:
- Reword or reorder. Google's guide lists three things to try: say the same thing in different words, switch to a similar task that gives the same result (for example a multiple-choice question in place of an open one), or change the order of the parts of the prompt.
- Split the task. One prompt that does too much can be broken into steps. See Prompt chaining.
- Question the model choice. Anthropic's overview notes that not every failing eval is best solved by prompt engineering. Sometimes a different model is the right fix.
The cost of the method Following all five steps is slower than a quick rewrite, for the first bug. You need a request log and a saved test set, and each run of the set costs tokens. After a few bugs it is faster, because fixed bugs stay fixed and you stop debugging the same failure twice.
Go deeperWords to know
| Word | Meaning in simple words |
|---|---|
| Debugging | Finding the cause of a failure and removing it |
| Reproduce | To make a failure happen again on purpose, so you can study it |
| Root cause | The real reason for a failure, not just what you see on the surface |
| Template | Fixed prompt text with slots that your code fills in for each request |
| Request log | A saved copy of exactly what was sent to the model |
| Contradiction | Two instructions that cannot both be followed |
Check your understanding
Sources: the official documentation
This lesson is based on the official documentation. Check it for the latest details.
Something wrong or unclear in this lesson? Tell us, it gets fixed.