Writing an Effective Prompt to Get Help Debugging Code
BehavioralModel answerLast updated
Decide which way of asking an AI assistant for debugging help gets the most reliable diagnosis, and justify the choice with trade-offs, edge cases and a worked example.
You are stuck on a bug and decide to ask an AI chat assistant for help. You can phrase your request in one of four ways:
- A. Paste all of your code and ask, "What is wrong with this?"
- B. Share the error message or stack trace, the code snippets that are relevant to the failure, and a side-by-side description of what you expected versus what actually happened.
- C. Describe the symptoms in plain language only, with no code attached.
- D. Tell the assistant to throw away the faulty module and write a new one from scratch.
Task:
- Choose the approach that is generally the most effective for finding and fixing the bug.
- Explain why it works better than each of the other three.
- Discuss situations where the choice needs adjusting, for example when no error is thrown, when the bug spans several files, or when the code is confidential.
- Show how you would verify whatever the assistant suggests before trusting it.
A short code example illustrating your reasoning is welcome but not required.
Hints
Hint 1
Think about what a colleague would ask you before they could help with a bug: what did you run, what did you expect, what happened, and where is the code?
Hint 2
Consider what each option leaves out. Which ones fail to define what 'correct' means, and which ones remove the code or the diagnostic output the assistant would need?
Hint 3
Many bugs throw no exception at all. Ask yourself which part of a good prompt still lets the assistant find a silent logic error, and why a full rewrite does not teach you the root cause.
Model answer
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Answer
Option B is the strongest: give the error output, the relevant code, and a clear comparison of expected versus actual behavior.
Why B works
An assistant can only reason about what you give it. Debugging is the act of closing the gap between intended and observed behavior, and B supplies all three ingredients for that:
- Error message or stack trace. It names the failure type, the location and the call path, often the single best clue.
- Relevant code only. The failing path, not the whole repository. This keeps the context focused and cheap.
- Expected vs. actual behavior. It defines "correct". Many defects raise nothing: wrong output, boundary mistakes, mishandled edge cases. Without a stated expectation the assistant must guess your intent.
This mirrors a good bug report, and it is what a senior colleague would ask for before helping.
Why the others fall short
A. Whole code plus "what is wrong?"
- "Wrong" is undefined, so the assistant may comment on style or hypothetical issues and miss the real defect.
- Large inputs dilute attention and can hit context limits.
- Pasting everything may expose proprietary code or secrets unnecessarily.
C. Symptoms only.
- The assistant cannot see the defect, so it lists generic possibilities (null reference, race condition, and so on).
- The advice is hard to verify and often unrelated to your implementation. Expect several follow-up rounds just to collect the missing information.
D. Rewrite from scratch.
- It skips diagnosis, so you never learn the root cause and may repeat the mistake.
- It can introduce new bugs, break interfaces other code relies on, and lose subtle business rules.
- It yields a large diff that is hard to review.
Cost and efficiency
- Context cost: B sends roughly the size of the failing path plus diagnostics. A sends the whole codebase, which is slower, pricier and less accurate.
- Iteration cost: C usually needs multiple turns to gather basics. B often gets a useful diagnosis in one or two.
Adjustments and edge cases
- No error message. Lean on the expected/actual comparison: give a minimal concrete input, the output you wanted and the output you got.
- Multi-file bugs. Include the interfaces and call sites that matter, ideally as a minimal reproducible example, not every file.
- Environment-dependent bugs. State language, library and OS versions and relevant configuration.
- Prior attempts. List what you already tried so it is not suggested again.
- Sensitive data. Redact credentials, tokens and customer data from code and logs.
- When a rewrite is fine. For a tiny throwaway script, or after the root cause is understood and a refactor is a deliberate decision. It is not a debugging strategy.
Worked example
This function raises no exception, so only an expected-vs-actual comparison exposes the problem:
def moving_average(xs, k):
"""Average of every contiguous window of size k."""
return [sum(xs[i:i + k]) / k for i in range(len(xs) - k)]
A prompt in the style of B:
Goal: average of every contiguous window of size k.
Code: (the function above)
Expected: moving_average([1, 2, 3, 4], 2) -> [1.5, 2.5, 3.5]
Actual: moving_average([1, 2, 3, 4], 2) -> [1.5, 2.5] (last window missing)
Environment: Python 3.11
Already tried: checked the slice xs[i:i+k]; it looks right.
Request: identify the root cause and propose a minimal fix.
With this, the assistant can point straight at range(len(xs) - k), which stops one window early. The minimal fix:
def moving_average(xs, k):
if k <= 0:
raise ValueError("k must be positive")
return [sum(xs[i:i + k]) / k for i in range(len(xs) - k + 1)]
On the same bug, the alternatives fare worse:
- A might yield remarks about efficiency (a running sum) while the off-by-one goes unmentioned.
- C ("my moving average is missing a value") yields generic guesses.
- D gives a rewrite you must re-verify, and you never learn it was a range boundary.
Verify before trusting
Treat the assistant's answer as a hypothesis:
- Reproduce the bug first, so you know the failing case.
- Apply the proposed change and confirm the failing case now passes.
- Add a regression test, e.g.
assert moving_average([1, 2, 3, 4], 2) == [1.5, 2.5, 3.5]. - Run the wider test suite to catch side effects.
- Check that the explanation of the root cause actually matches the code, not just that the symptom disappeared.
Common mistakes
- Dumping the entire codebase and hoping the assistant finds the issue.
- Describing a symptom with no code or reproduction.
- Omitting the expected behavior, so the assistant guesses what "correct" means.
- Asking for a rewrite and accepting it without review or tests.
- Leaking secrets or customer data in pasted logs.
- Failing to mention versions or what was already tried.
Summary
Good AI-assisted debugging is good bug reporting: a focused reproduction, the diagnostic output, and a precise statement of expected versus actual behavior. Keep the human in control of the fix and verify it with tests.