Sub topic 13
Know when to stop prompting and debug by hand
This chapter assumes you can read a stack trace, run tests from a terminal, and use git. It also assumes you have worked through the earlier chapters on writing debugging prompts and checking a diagnosis. An assistant is a useful tool for debugging, but it is not always the fastest one. Knowing when to put it down is part of the workflow.
Signs that prompting has stopped helping
Each follow-up prompt should add information or narrow the problem. When it does neither, you are no longer making progress. Watch for these signals:
- Circular answers. The assistant proposes a fix you already tried, or alternates between two fixes that each undo the other.
- Growing patches. Each suggestion touches more files or adds more code, while the original failure remains.
- Shifting diagnoses. The stated cause changes every turn and none of the explanations match the traceback.
- Invented details. The assistant keeps citing functions, flags, or settings that do not exist in your version of the library.
- No new evidence. You have nothing new to paste, such as a different error, a new log line, or a changed test result.
A practical rule is a budget. If three focused attempts have not moved the failure, such as a changed error message or a narrower failing case, stop and switch methods.
What to do by hand
Debugging by hand does not mean starting over. You keep what the earlier steps gave you: the reproduction, the minimal example, and the failing test. Then work directly.
- Revert to a known state. Use
git stashorgit checkoutto discard the assistant's speculative edits. Stacked guesses make the real cause harder to see. - Confirm the failure still reproduces. Run the minimal example once to be sure you are debugging the original problem.
- Narrow the location. Add a
printor logging call, or use a debugger such aspdb, to inspect values at the failing line. - Bisect. Halve the search space. Comment out half the code path, or use
git bisectto find the commit that introduced the bug. - Read the source and docs. Check the library's documentation for your installed version rather than relying on a summary.
Here is a small example using the failing function from earlier chapters. Instead of asking again, inspect the value that triggers the error.
import pdb
def average(values):
pdb.set_trace()
return sum(values) / len(values)
print(average([]))At the (Pdb) prompt, type p values to print the list and p len(values) to see the length. Both confirm that the list is empty before the division runs. Type c to continue. Remove the pdb lines once you have your answer.
Return to the assistant with better questions
Hand debugging often ends with a sharper question, and the assistant can be useful again. After you have learned something, start a fresh conversation rather than continuing a long, confused one. Include only what you now know:
- the confirmed location of the failure,
- the values you observed,
- what you ruled out and how.
A short prompt such as "values is empty at line 2 when called from report(). Where in report() could an empty list originate?" gives the assistant a precise task.
Common mistakes
| Mistake | Better approach |
|---|---|
| Keeping every failed patch applied | Revert to a clean commit before debugging by hand |
| Continuing one very long conversation | Start a new one with a short summary of confirmed facts |
| Abandoning the reproduction | Keep the minimal example and failing test as your reference |
| Treating manual debugging as a failure | Treat it as the next step in the same workflow |
Exercise
You asked an assistant to fix a failing test. After three attempts, the error message is identical each time and the assistant's patches have touched four files. List the first two things you should do.
Show answer
First, revert the assistant's edits (for example with git stash or git checkout) so you return to a known state. Second, rerun the minimal reproduction or failing test to confirm the original failure, then debug by hand using prints, a debugger, or bisecting. Afterward, you can start a fresh prompt with the facts you confirmed.