Debug an Error

Diagnose an error by giving the AI the message, the code, what you tried and what you expected.

debuggingerrorstroubleshooting All models

Prompt

Prompt
Help me debug an error. Think like a careful engineer: find the cause before proposing a fix.

<environment>
{environment}
</environment>

<error>
{error_message}
</error>

<code>
{code}
</code>

<behavior>
Expected: {expected_behavior}
Actual: {actual_behavior}
Already tried: {already_tried}
</behavior>

Answer in this order:
1. What the error message means, in plain words.
2. The most likely cause, pointing to the exact line or condition. Give your confidence (high, medium, low).
3. Two other possible causes, and a quick check that would rule each in or out.
4. The fix: a corrected code snippet, and a sentence on why it works.
5. How to confirm it is fixed: the exact steps or test to run.
6. How to prevent the same kind of error in future.

If the information is not enough to find the cause, do not guess. Tell me what extra information you need, such as logs, a variable value or the calling code, and how to get it. Do not suggest changes unrelated to the error.

Variables

VariableWhat to put inExample
{environment}Language, framework, versions and operating system.Python 3.12, FastAPI, macOS.
{error_message}The full error message and stack trace.Paste the exact text from your terminal.
{code}The code related to the error.Paste the relevant function and the line where it fails.
{expected_behavior}What should happen.The endpoint should return a list of users.
{actual_behavior}What happens instead.It returns a server error when the list is empty.
{already_tried}What you already tried. Write "nothing yet" if nothing.Restarted the server; checked that the database is running.

How to use

  1. Copy the full error text, not a summary. The first line and the last lines of a stack trace both matter.
  2. Include only the relevant code, plus the line where it fails.
  3. Run the suggested check before applying the fix, so you know the cause was correct.

Example result

Illustrative example

Meaning: the code reads the first item of a list that is empty, which raises an IndexError. Likely cause (high confidence): get_users() returns [] when there are no users, and line 14 reads users[0] without checking. Fix: return an empty list when users is empty. Confirm: call the endpoint with an empty table and check that it returns [] with a 200 status.

Model notes

Claude

Claude tends to follow the cause-first order well. Keep the instruction not to guess so it asks for missing logs instead.

ChatGPT

Works as-is. If the bug is spread across files, paste the call path rather than one function.

Gemini

If you can attach log files or a code export, name them and point to the lines that matter.

Grok

Include exact versions. Ask it to say when a fix depends on the library version, and confirm against the library's documentation.

Refine the result

  • Prompt
    Explain the cause as if I were new to this language.
  • Prompt
    Add a regression test that would have caught this error.
  • Prompt
    Show a second, more defensive version of the fix.

Related templates

  • Code Review

    Get a prioritized code review that covers correctness, security, readability and tests.

    code reviewqualitysecurity All models
  • Write Unit Tests

    Generate unit tests that cover normal cases, edge cases and failures, with the reasoning for each.

    testingunit testsquality All models
  • Refactor for Readability

    Improve the structure and naming of code without changing what it does, with a list of changes.

    refactoringreadabilityclean code All models

Related guide