Retext
Claude CodeIDEFor DevelopersBug Fix

Claude Code Bug Fix Prompt for Developers

You get the root cause named in a sentence or two, the smallest code change that fixes it, and a check that proves the bug is gone. This prompt is tuned for developers working in Claude Code: it carries your role context, and the steps below cover what changes in Claude Code. Check that every function, method and package the AI uses actually exists in your installed versions, and run the tests before you commit.

Installs as a snippet — type ;bugfix to paste this prompt anywhere on your Mac.
Prompt Template
I have a bug I need fixed. Find the root cause before you suggest any change.

## Expected behavior
[what should happen]

## Actual behavior
[what happens instead, including the full error message and stack trace]

## Code
Language and version: [e.g. Python 3.12]
[paste the smallest piece of code that reproduces the bug]

## Already ruled out
[what you tried and what it showed]

## Instructions
- Name the root cause and point to the exact line.
- Give the smallest change that fixes it. Do not rewrite or reformat unrelated code.
- If the information above is not enough to be sure, ask me one question instead of guessing.
- Tell me if the same mistake likely appears elsewhere.

## Output format
1. Root cause (1 to 2 sentences)
2. Fixed code (only the changed function or block)
3. Why the fix works
4. One test or manual check that proves it is fixed

About my work
- Language, framework and versions: [e.g. TypeScript 5, Next.js 15, Node 20]
- Relevant files or functions: [e.g. lib/billing.ts, the createInvoice function]
- Conventions to follow: [e.g. no classes, Zod for validation, Vitest for tests]

Context
- Files involved: [list the files, or reference them in the chat]
- How I build and test: [e.g. npm test]

How to use this prompt

  1. 1

    Open Claude Code

    Run claude in your project directory and paste the prompt.

  2. 2

    Give it context

    Name the files, commands and tests involved. Claude Code can read files and run commands in your project, asking permission as it goes.

  3. 3

    Review and iterate

    Approve or decline each edit and command, then reply in the same session to adjust.

  4. 4

    Save what you repeat

    Put project conventions (how to run tests, style rules, folders to avoid) in a CLAUDE.md file so every session starts with them.

Working in Claude Code

  • •Claude Code works in your terminal, so tell it the exact commands for building and testing.
  • •Ask it to explain its plan before large edits; you can stop it if the plan is wrong.
  • •Start a fresh session for an unrelated task so earlier context doesn't steer the new one.

Worked example

What you give it

Expected: average([]) returns 0.
Actual: ZeroDivisionError: division by zero
Language: Python 3.12

def average(nums):
    total = 0
    for n in nums:
        total += n
    return total / len(nums)

Already ruled out: nums is always a list, never None. It only fails when a user has no orders yet.

What a good answer looks like (shortened)

1. Root cause: when nums is empty, len(nums) is 0, so the return line divides by zero.

2. Fixed code:
def average(nums):
    if not nums:
        return 0
    return sum(nums) / len(nums)

3. Why it works: the guard returns before any division happens. sum() replaces the manual loop and gives the same result for non-empty lists.

4. Check:
assert average([]) == 0
assert average([2, 4]) == 3.0

Tips for better results

  • •Paste the actual error output and the exact versions you run. Most wrong answers come from the AI assuming a different version of a library or framework.
  • •Fill in Expected behavior and Actual behavior with real values, not descriptions like "it breaks." A concrete input and output usually points straight at the faulty line.
  • •Start a fresh session for an unrelated task so earlier context doesn't steer the new one.
  • •Avoid this: Pasting only the error message without the code that raised it, so the AI has to guess where the problem is.

Check before you use it: Check that every function, method and package the AI uses actually exists in your installed versions, and run the tests before you commit.

Get the free prompt pack

Our best coding prompts in one Markdown file, delivered by email. The templates here stay free either way — the pack just saves you the copying.

Variations

Fix with a regression test

Add this line under Output format: 5. A unit test that fails on the old code and passes on the fixed code.

Production incident

Add this section: ## Constraints: This is live. Prefer a safe, minimal patch now and list a better follow-up fix separately.

Explain for a junior dev

Add this line under Instructions: Explain the root cause in plain terms, as if teaching someone new to [language].

Save it as a Retext snippet

Retext is a free text expander for Mac. Save this prompt once, then type ;bugfix in any app (ChatGPT, Claude, your editor, a browser tab) and the full prompt is pasted in.

  1. Click Add to Retext. The prompt installs as a snippet with the trigger ;bugfix.
  2. Change the trigger in Retext if you prefer a different one.
  3. Type the trigger wherever you want the prompt, then fill in the brackets.

Frequently asked questions

How do developers keep AI-generated code consistent with their codebase?

Include one or two existing files as examples of your style and state your conventions in the prompt. Ask for changes as small diffs to existing code rather than whole new files.

How much code should I paste when asking AI to fix a bug?

Paste the function that fails plus anything it calls that could affect the result. If the snippet is over a couple hundred lines, cut it down to a minimal reproduction first, since extra code hides the signal.

Will Claude Code run commands without asking?

By default it asks for permission before editing files or running commands. You approve each step, which is a good moment to check its plan against the prompt.

How do I reuse this prompt without copying it every time?

Click Add to Retext to save it as a snippet. Then type ;bugfix in any Mac app, including your AI tool, and the full prompt is pasted in.

Related Templates

Get the free prompt pack

Our best coding prompts in one Markdown file, delivered by email. The templates here stay free either way — the pack just saves you the copying.