# Debug Code With AI: 7 Copy-and-Paste Prompts That Work

AI assistants can be excellent debugging partners, but only if you ask well. "Fix my code" gets you a rewrite you don't understand. The prompts below get you an explanation, a small fix and a way to make sure the bug stays gone.

Each prompt has blanks in [square brackets]. Fill them in, paste, and send. They work in any language and with any modern chat assistant or AI coding tool.

## Before you start: give the AI context

Start the chat with one short block so every answer fits your setup:

```text
Context: I'm using [language + version] on [OS/framework].
My level: [beginner/intermediate]. I want you to explain your
reasoning briefly, make the smallest change that works, and say
so when you're unsure instead of guessing. Never invent functions
or libraries.
```

That last sentence matters. It reduces the chance of the assistant referencing a function that doesn't exist.

## 1. The error decoder

```text
Here is the full error, including the traceback:
[paste everything]
Here is the code around it: [paste 20-40 lines]
In plain English: what is this error saying, which line is the
real cause (not just the last line shown), and what are the three
most likely reasons? Rank them.
```

**Why it works:** the line where a program crashes often isn't the line that caused the problem. Asking for the *real cause* and a ranked list steers the assistant away from a first guess.

## 2. Expected vs. actual (for bugs with no error)

```text
This code should [expected behavior] but instead [actual behavior].
Input: [input]. Expected output: [output]. Actual output: [output].
Walk through the code step by step with this input and tell me
where the actual behavior first diverges from what I expect.
```

**Why it works:** most "wrong answer, no error" bugs are logic bugs. Making the AI trace a specific input forces it to find where reality and your expectation split.

## 3. The rubber duck

```text
Don't give me the answer yet. Ask me up to five questions, one at
a time, that would help me find this bug myself. Here's the code
and the symptom: [paste]
```

**Why it works:** sometimes you learn more by being guided. This is the best prompt if your goal is to improve, not just to get unstuck.

## 4. The minimal reproducible example

```text
Help me shrink this problem into the smallest program that still
shows the bug. Remove everything unrelated and tell me what you
removed and why. Here's the code: [paste]
```

**Why it works:** half of all debugging is isolating the problem. A minimal example is also what you need if you ask a forum or open an issue.

## 5. "It worked yesterday"

```text
This worked before and now it doesn't. What I changed since then:
[list changes, new packages, updates]. Here's the error or
symptom: [paste]. List the most likely culprits in the order I
should check them, and how to check each one quickly.
```

**Why it works:** listing what changed turns the AI into a detective with clues instead of a guesser.

## 6. The smallest-diff fix

```text
Fix it with the smallest change possible. Show only the changed
lines plus 2 lines of context. Explain in one or two sentences
why this fixes it. Don't refactor or rename anything else.
```

**Why it works:** big rewrites can fix one bug and quietly add three more. Small diffs are easy to review and easy to undo.

## 7. The "safe to run?" check

```text
Before I run this command or script: explain what it does line by
line. Flag anything that deletes or overwrites files, installs
software, needs admin rights (sudo), changes settings, or sends
data over the internet. How can I test it safely first?
```

**Why it works:** AI suggestions sometimes include commands with side effects. For example, previewing what `git clean` would delete before actually running it is a good habit, and this prompt builds it in.

## Bonus habit: turn every bug into a test

After you fix a bug, ask:

```text
Write a test that would have FAILED because of this bug and
passes now. Explain what it checks.
```

That's how the bug stays fixed. We cover this in more detail in [our guide to AI prompts for learning to code](https://agentkitco.com/blog/ai-prompts-for-learning-to-code/).

## A word on trust

AI can be wrong, with total confidence. Always read the suggested change, run your tests, and never run commands you don't understand. If an answer references a function or option you can't find in the official docs, treat it as suspect.

## Get all 40

These seven are a taste. The **[Prompt Pack for Coding](https://burmunster.gumroad.com/l/coding-prompt-pack)** has 40 copy-paste prompts across five sections: learning, debugging, refactoring, testing and shipping. Each has a "when to use" note, the full prompt and a short example, plus a "safe to run?" checklist and a two-page quick reference.

---

*Originally published on [AgentKit Co](https://agentkitco.com/blog/debug-code-with-ai-prompts/).* Want more? Grab 41 copy-and-paste prompts in the [free AgentKit Co prompt library](https://agentkitco.com/free-prompts/), or check out the [full pack](https://agentkitco.com/products/coding-prompt-pack/) if you want the complete set.
