Debug code with AI

Intermediate · 5 min read

AI can help investigate a bug, but a convincing explanation is not evidence that a fix works. Treat the assistant as a reviewer that proposes hypotheses and tests. Keep the reproduction small, preserve the original behavior and verify every change locally before deploying.

1. Capture the failure

Record the exact error, the input that triggers it and the expected behavior. Include the runtime version and relevant dependency versions when they affect reproduction. For a browser issue, distinguish console errors from network failures and layout problems. A screenshot may show a symptom but not its cause. Remove access tokens, session cookies and personal information from logs before sharing them.

2. Build a minimal reproduction

Reduce the failing program to the smallest example that still exhibits the problem. If a search filter fails, isolate the filter function and a small data array instead of sending an entire application. Confirm that the reduced example fails in the same way. If removing a dependency makes the error disappear, that is useful evidence. Preserve a copy of the original project so you can compare behavior after changes.

3. Ask for ranked hypotheses

Request likely causes with evidence for and against each one. For instance, an undefined variable may result from a typo, a missing import or code executing before initialization. Ask for a diagnostic step that distinguishes these possibilities. Do not accept an explanation that assumes an unseen file or framework configuration. When the assistant lacks context, provide the relevant snippet rather than letting it invent the missing code.

4. Request a minimal patch

Specify which behavior must remain unchanged and ask for a small diff or replacement function. Avoid a broad rewrite unless the evidence supports it. A patch should explain why it addresses the observed failure and identify any side effects. Review changes to authentication, permissions, database queries and file handling particularly carefully. Do not run commands that delete data or change production configuration merely because an assistant suggests them.

5. Test normal and edge cases

Write a test that fails before the patch and passes afterward. Add cases for empty input, missing fields and unexpected values where relevant. For a UI bug, test keyboard navigation, mobile widths and slow network conditions. Check the console after the interaction, not just at initial page load. A successful build is not enough if the feature still behaves incorrectly. Record the actual results rather than assuming the proposed tests passed.

6. Review and deploy safely

Inspect the final diff for unrelated changes, secrets and new dependencies. Run the project’s existing test suite and lint checks. Use a staging environment when the change could affect real users. Keep a rollback path and monitor errors after deployment. If the assistant recommends a dependency upgrade, read the release notes and verify compatibility. The developer remains responsible for the final change.

Bad prompt vs. good prompt

Weak prompt

My JavaScript is broken. Fix everything.

Improved prompt

The function filterItems throws a TypeError when query is null. Expected behavior: return all items for an empty query. Review the supplied function and reproduction. Give two hypotheses, a minimal patch and tests for null, empty and normal input. Preserve existing sorting. Do not invent files.

Try it yourself

Use the improved example with your own task. Replace the sample details with real inputs, compare the answer against the stated requirements and record what needs revision. Repeat with a second example before adopting the template for regular work.

Open related prompt builder →

Frequently asked questions

Should I paste the whole repository?

Start with the smallest relevant reproduction and sanitized logs.

Can AI confirm my fix works?

Only actual execution and test results can establish that for your environment.

What if the suggested patch introduces another bug?

Revert the patch, isolate the regression and add a test before trying another fix.

Related guides

Write clearer AI prompts →

Verify AI-generated output →