How Do You Debug a Bug With No Error Message?
Have Claude add a few temporary, labeled logs along the suspect path, run the code, and find the first value that's wrong. In Claude Code, it can run the code and read the output itself, or watch a log file while you reproduce the bug.
[Feature] gives the wrong result but no error. Input: [example]. Expected: [X]. Actual: [Y]. Don't fix anything yet. Add temporary logging along the path from [entry point] to [output], at each point where the value changes. Start every log line with `DBG:`, keep it to [10] lines or fewer, and don't log secrets or personal data. Run [command] and read the output. Tell me the first line where the value is wrong, and what that rules out. Then make the smallest fix and remove every `DBG:` line. Delete any temporary files or scripts you made. Show me `git diff` so I can see none are left.
- 1subtotal=90 (number)correct
- 2fee="10" (string)first wrong value, so fix here
- 3total="9010" (string)the symptom, not the cause
Try it now
- 1Send the prompt in Claude Code with a real input and the value you expected.
- 2If the bug needs a running server, add:
Start the dev server in the background, tail its log, and flag lines containing DBG:. Then trigger the bug yourself./taskslists background commands. - 3Read the first wrong value before accepting any fix.
- 4Check the result with
git diff | grep DBG:. It worked if that prints nothing and the fix touches the line the logs pointed to.
Use it when
Wrong numbers, silent failures, and state that's right alone but wrong in combination. Also anything you can run but can't yet capture in a test. In the Claude app, you add the logs Claude suggests, run the code, and paste the output back.
Skip it when
When a stack trace already points to the failure, or when you can already write a failing test, which leaves behind something more useful than logs. Avoid it too in timing-sensitive code, since logging can change the timing enough to hide the bug. A debugger or test is cleaner there.
Why it works
With no stack trace, both you and Claude are guessing at hidden state, and logs replace that guess with real values. Claude Code closes the loop because it can run the command and read the result. There's a practical reason to log little: Anthropic's tools reference says Claude receives Bash output inline up to roughly 30,000 characters by default. Past that, it gets a saved file's path and a preview of up to the first 2,000 characters, and has to read or search the file for the rest. A few labeled lines reach Claude whole. For a server that's already running, the Monitor tool lets Claude tail a log and flag lines as they appear, with a deadline of 5 minutes by default and 30 at most.
Example
A cart shows a total of 9010 for a subtotal of 90 and a fee of 10. The logs read DBG: subtotal=90 (number), then DBG: fee="10" (string), then DBG: total="9010" (string). The fee line is the first value that's wrong, because the API returns it as a string and + joins the two values instead of adding them. So the fix belongs where the fee is parsed, not where the total is calculated.
Going further
Stop DBG: lines from being committed. Add a PreToolUse hook to .claude/settings.json with the matcher Bash, the filter "if": "Bash(git commit *)", and a command that checks git diff --cached for DBG: and exits with code 2 if it finds any. Exit 2 blocks the action and sends your stderr message to Claude. The script is an idea built on the documented hook, not an Anthropic example, and the docs call the if filter best-effort, so it won't catch a commit you run yourself. You can also ask for temporary assertions instead of logs, so that the first one to fire names the boundary where reality diverges from the theory.
Common mistakes
- Logging tokens or customer data. Everything Claude reads goes to the model, and Anthropic's data page says transcripts stay in plaintext under
~/.claude/projects/for 30 days by default. - Logging so much that the output passes the inline limit, so Claude gets a preview and a file instead of the whole result at once.
- Assuming a Monitor watch runs forever. Each has a deadline, 5 minutes by default, so ask Claude to restart it if your reproduction takes longer.
- Accepting a fix before the instrumented run. Then you've added logs and still guessed.
Sources
- Tools reference, Claude Code Docs
- Automate actions with hooks, Claude Code Docs
- Data usage, Claude Code Docs
- Prompting best practices, Claude Platform Docs
Checked 6 November 2026. Products change often, so confirm details with the vendor.