Topic 3 of 5 · 4 min read
Testing one bug hypothesis at a time
Goal: Direct an agent to test one hypothesis with a single probe and read the result before any code changes.
Debug by listing possible causes, picking the cheapest one to test, and changing nothing but a probe until the result is in. An agent left alone tends to edit several things at once. If the bug disappears, you do not know which edit fixed it, and the others stay in your code.
Check yourself · 1 of 3
An agent suggests adding a null check, raising a timeout and wrapping the call in try/catch for one bug. What is the problem?
Why it matters
Agents want to be useful, so they act. A common pattern is the shotgun edit: add a null check, bump a timeout, wrap the call in try/catch, and call it done. The symptom may vanish while the cause remains. You also get a diff full of changes nobody needs.
The loop
- Ask for a ranked list. "List the three most likely causes given this trace. For each, say what you would see if it were true."
- Pick one. Usually the cheapest to test, or the likeliest. Not all of them.
- Design a probe. A probe reads, it does not fix: a log line, a breakpoint, a print of a value, an assertion, a debugger session such as Python's
pdb, or a one-off script. - Run it and read the output together. Say what it shows and which hypotheses it rules out.
- Decide. Confirmed: now make the smallest fix. Rejected: go to the next hypothesis.
- Remove the probe when done.
Tell the agent the rule directly: "One hypothesis at a time. Do not change behaviour until a probe confirms the cause."
Before and after
Weak exchange:
User: The cache returns old prices. Fix it.
Agent: I lowered the TTL, added a flush on write and rewrote the key function.
Better:
User: List 3 likely causes for stale prices. Test only the first with a log line.
Agent: H1: the key ignores currency. Probe: log the key on each get and set.
Output: get key=price:42, set key=price:42:EUR. The keys differ.
H1 is confirmed in one run. The fix is one line, and nothing else moved.
What goes wrong
- Shotgun edits. Many changes at once, with no way to tell which mattered.
- Probes that change behaviour. A log that forces a flush or a sleep can hide a race.
- Confirmation bias. The agent reads the output as support for its first idea. Ask: "What in this output would contradict H1?"
- Leftover debug code. Check the diff for stray prints.
Examples
A single-probe prompt
Hypothesis: the retry runs twice because the handler is registered twice.
Probe: add one log line in registerHandler() printing the event name
and a counter. Run the failing test once. Do not change anything else.
The agent has a narrow task, and you get one clean result.
Reading the result
The log shows registerHandler(order.paid) #1 and #2. Hypothesis confirmed. The fix is to register once at startup, and the next step is to rerun the original reproduction.
Key points
- List several causes and say what each would look like if true before testing any.
- Test the cheapest or likeliest hypothesis with a probe that does not change behaviour.
- Read the probe output and say which hypotheses it rules out.
- Make the smallest fix only after the cause is confirmed.
- Remove probes before finishing, and check the diff for leftovers.
Common mistakes
- Letting the agent edit several things at once.
- Starting from a fix instead of a list of causes.
- Accepting the first plausible story without asking what would contradict it.
- Forgetting to remove debug output.
Check yourself · 2 of 3
What is a probe in debugging?
Check yourself · 3 of 3
After a probe contradicts your top hypothesis, what is the best next step?
Practice items, not counted towards your mastery.
Start here
- Git Tools: Debugging with Git (Pro Git) (opens in a new tab)BookFree
File annotation and binary search with git bisect, explained step by step.
- git-bisect documentation (opens in a new tab)DocsFree
The full reference for bisect run, exit codes, skip and reset.
- Common workflows (Claude Code docs) (opens in a new tab)DocsFree
Shows how to hand an agent an error and ask for a fix with a verified result.
More for this topic
- pdb, the Python debugger (opens in a new tab)DocsFree
Breakpoints and stepping, the tools an agent can run instead of guessing.
- Best practices for Claude Code (Claude Code docs) (opens in a new tab)DocsFree
Advice on giving agents a way to verify work, and on clearing context after repeated failed corrections.