Prompts for debugging code
Prompts that walk you through structured debugging — root causes, tests, fixes.
Debugging is detective work, and AI is a patient partner for the part where you're staring at an error message with no idea where to start. The Debug an Error Message Step-by-Step prompt is the anchor here: paste the error and the relevant code and you get the message explained in plain terms, a ranked list of likely root causes, and tests to confirm which one is real before you start changing things. That last bit matters, because the worst debugging happens when you fix the wrong cause confidently.
The collection leans heavily into a specific debugging sink: regular expressions. The Build and Explain a Regular Expression and Explain and Write a Regex From Plain English prompts both turn a pattern into a line-by-line breakdown with passing and failing test cases, which is exactly how you debug a regex that almost works. The Generate a Production-Ready Dockerfile prompt fits because so many "it works on my machine" bugs are really environment bugs. The pitfall AI helps you avoid is guessing: it pushes you to reproduce, isolate, and verify instead of flailing.
What makes a good prompt for debugging code
A good debugging prompt gives the model the full error text, the code around where it fires, and what you expected to happen instead. Vague descriptions get vague theories; a stack trace plus the offending function gets ranked, testable hypotheses. Ask it to explain the error before proposing fixes so you understand the failure rather than just patching it.
The move that separates good from great is asking for a confirmation step. Have the model propose a quick test or log line to prove which root cause is real before you edit anything. For regex specifically, always ask for the test strings that should match and the ones that shouldn't, because a pattern that passes your one example often fails the case you didn't think of.
Get sharper results
- 01Paste the complete error message and stack trace, not a paraphrase, since the exact wording and line numbers are what let the model rank causes accurately.
- 02Ask it to explain the error and list root causes before suggesting any fix, so you can confirm the real cause instead of patching a symptom.
- 03For regex, demand a table of should-match and should-not-match test strings and run them, because the failing cases reveal bugs your happy-path example hides.
- 04When a bug only appears in production, describe the environment differences (versions, OS, container) so the model can consider environment causes, not just code.
Common questions
The AI suggested a fix that didn't work. Now what?
Tell it exactly what happened: the new error, or what changed. Debugging is iterative, and the first hypothesis is often wrong. Each round where you report the real result lets it eliminate a cause and sharpen the next theory, the same way a human would.
Why does AI struggle with my regex?
Regex bugs are usually edge cases, and a model can only reason about cases it's shown. Always give it concrete strings that should match and shouldn't, then test its output against all of them. The mismatch between expectation and result is what surfaces the actual bug.
How much code should I share to get good debugging help?
Enough to make the failure reproducible in the model's head: the function that errors, the data going into it, and anything it calls that's relevant. Too little and it guesses; too much and the signal drowns. Aim for the smallest snippet that still triggers the bug.
Debug an Error Message Step-by-Step
Get a structured debugging plan: error explanation, ranked root causes, tests, and fixes.
Build and Explain a Regular Expression
Get a working regex with a plain-English breakdown and a set of passing/failing test cases.
Generate a Production-Ready Dockerfile and Explain Each Line
Generate a hardened, multi-stage Dockerfile with line-by-line comments and matching .dockerignore.
Explain and Write a Regex From Plain English
Translate a plain-English matching requirement into a regex with line-by-line breakdown and edge cases.
Generate table-driven unit tests for a function with edge cases
Produces a table-driven unit test suite covering happy paths, boundaries, and error conditions for a given function.
Explain a cryptic regex and rewrite it to be readable
Decodes a confusing regex token by token, surfaces edge cases and backtracking risk, then rewrites it readably.
Port a code snippet to another language idiomatically
Translates a snippet into idiomatic code in a target language and flags cross-language correctness pitfalls.
Diagnose and rewrite a slow SQL query using its plan
Reads a query plan to explain why a SQL statement is slow, then rewrites it and recommends indexes.
Decode a stack trace and pinpoint the likely root cause
Reads a stack trace frame by frame to explain the failure and pinpoint the most likely root cause with next steps.
Brainstorm edge-case test scenarios before you write tests
Enumerates grouped edge-case and adversarial test scenarios for a feature so nothing gets shipped untested.