Skip to main content
CodingMarble

AI prompts

AI Prompts for Debugging Code: 7 Templates That Work

A debugging prompt works when it gives the assistant the error, the code involved, what you expected and what you tried, and then asks for a specific kind of help. Use the templates below as starting points, treat every answer as a hypothesis to test, and never paste secrets into a prompt.

By CodingMarble · Published · 4 min read

The letters AI on a blurred background
Photo: Zach M on Unsplash

How to use these templates

An AI assistant cannot run your program unless you connect it to one, and it cannot see your machine, your data or your deployment. It sees only the text you give it. So most bad debugging answers come from missing context: the stack trace was trimmed, the versions were not mentioned, or the question was "why is this broken?" with no sign of what working looks like. The templates below force you to supply the facts that matter. Replace everything in square brackets, delete lines that do not apply, and paste only the smallest slice of code that still shows the problem.

Treat the reply as a suggestion from a well-read colleague who has not seen your project. It can be right, plausible and wrong, or confidently wrong in a way that costs an hour. Each template is followed by how to use it, what a good response looks like and how to check it.

Order matters too. Start with reproduction and explanation when you do not yet understand the symptom, move to root cause once you can trigger the bug on demand, and use the comparison and review templates when you already have a candidate fix. Jumping straight to a fix request is how people end up with a patch that hides the symptom while the underlying fault stays in place. Change one thing at a time and rerun after each change, so you always know which edit made the difference.

Templates 1 and 2: reproduce and explain the error

Template 1: write a reproduction checklist

Prompt· Turn a vague report into steps you can run
I have a bug I can only describe loosely.
Symptom: [what the user sees]
Expected: [what should happen]
Environment: [language/framework and versions, OS, browser]
What changed recently: [deploy, dependency update, config change, or unknown]

Ask me up to 5 questions that would help reproduce this, then give a
numbered checklist of steps to reproduce it reliably. Do not guess at the fix yet.

Use this when the bug is intermittent or reported by someone else. A good answer asks about inputs, timing, data state and environment, and ends with steps you can follow. Verify by following the checklist yourself; if you cannot reproduce the symptom, the questions are still a map of what to investigate. It fails when the true cause is something nobody mentioned, so answer the questions honestly instead of saying "not sure".

Template 2: explain an error message

Prompt· Plain-language meaning first, causes second
Explain this error in plain language, then list the most likely causes
in order of probability for my setup.

Error (full text, including the first stack frames):
[paste error]

Stack: [language, framework and versions]
Code at the failing line, with 10 lines of context:
[paste code]

For each cause, tell me one quick check that would confirm or rule it out.

Good output separates what the message literally says from what usually triggers it, and gives each cause a check you can run in a minute, such as logging a value or printing a version. Verify by running those checks rather than accepting the top-ranked cause. It fails when the real error is hidden further up the stack, so include the first error in the log, not only the last line.

Templates 3 and 4: root cause and minimal reproduction

Template 3: find the root cause

Prompt· Forces reasoning over a quick patch
Help me find the root cause, not just a patch.
Behaviour: [what happens]
Expected: [what should happen]
Relevant code: [paste only the functions involved]
Already tried: [list what you changed and the result]

Trace the data through the code step by step and point to the first line
where the value or state stops matching what I expect. State any assumption
you are making about code you cannot see.

The instruction to name assumptions is the important part, since it exposes where the assistant is guessing. A good answer walks through the code with example values and identifies a specific line. Verify by adding a log or breakpoint at that line and comparing the actual value with the one claimed. It fails on bugs that depend on state outside the pasted code, such as a database row, an environment variable or a race between requests.

Template 4: shrink to a minimal reproduction

Prompt· Cut a large failing example down to the smallest one
Here is code that shows the bug: [paste code]
The bug: [description]

Reduce it to the smallest self-contained example that still shows the same
bug. List which parts you removed and why each was safe to remove. If a
removal changes the behaviour, tell me, because that part is involved.

Minimal reproductions are valuable for bug reports and often reveal the cause by themselves. A good result runs on its own with no hidden dependencies and still fails the same way. Verify by actually running it. If the reduced version no longer fails, the assistant removed something relevant, and that gap is your clue.

Templates 5 to 7: compare, rubber-duck and review a fix

Template 5: compare a working and a broken version

Prompt· Spot the difference that matters
Version A works: [paste code or diff]
Version B fails: [paste code or diff]
Failure in B: [error or behaviour]

List every meaningful difference between A and B, ranked by how likely each
is to cause this failure. Ignore formatting and naming changes.

This suits regressions where you have a known good commit. Verify by reverting the top-ranked difference in B alone and rerunning. It fails when the cause sits in a dependency, a data change or configuration that is not part of either snippet.

Template 6: rubber-duck by questions only

Prompt· Make the assistant interview you instead of answering
Act as a rubber duck. I will explain my bug. Do not give solutions.
Ask one question at a time that challenges my assumptions, and wait for my answer.
Start with: what do you believe is happening, and what evidence do you have?

My bug: [description]

The value is in your answers, not the questions. A good session ends with you noticing an assumption you never checked. It fails when you rush through with short answers, so write full sentences.

Template 7: review a proposed fix

Prompt· Check a patch before merging it
Here is my bug and my fix.
Bug: [description]
Fix: [paste diff]

Does the fix address the root cause or only the symptom? List edge cases it
misses, what could break elsewhere, and a test that would fail before the
fix and pass after.

Verify by writing the suggested test, confirming it fails on the old code, and passing on the new. If you cannot make it fail first, the test proves nothing.

Verifying what the assistant tells you

Models sometimes invent function names, options and library behaviour that sound correct. Before applying a fix, search the official documentation for any API it mentions, check that the version you use has it, and run the code in a safe environment. Prefer a failing test or a reproducible command over reading the explanation and nodding. If two attempts in a row do not work, stop iterating in the same thread: start a new conversation with a cleaner summary of the problem and the dead ends you have already ruled out.

Know where this approach fails. Problems involving timing, concurrency, hardware, network conditions or production-only data cannot be diagnosed from a snippet. The assistant also tends to agree with the framing you give it, so if your premise is wrong, say "my theory is X" only after asking for alternatives first.

Never paste secrets into a prompt

Logs, config files and environment dumps routinely contain API keys, database URLs with passwords, tokens and customer data. Before pasting, search your text for key, secret, token, password and connection strings, and replace each value with a placeholder such as [REDACTED]. Prefer a reduced example with fake data over a real record. If a secret does reach a prompt, treat it as exposed and rotate it. The article on ai-debugging-without-leaking-secrets covers a safer routine in more detail.

Practical tip: keep a short personal template file with your stack, versions and conventions already filled in. Pasting it at the top of every debugging prompt saves time and removes the most common reason for generic answers.

Need this built properly for your business?

CodingMarble is a remote studio in India that builds websites, apps and games on fixed packages with the scope written down before payment. Try the free tools we run, or start a project when you are ready.