Bug Diagnosis Prompt
Give the model the error, the code and what you expected. It ranks likely causes by evidence, tells you how to confirm each, and proposes a minimal fix.
- ChatGPT
- Claude
- Gemini
- Cursor
Prompts for writing, debugging, refactoring and testing code with an AI model.
These prompts cover the everyday loop of programming work: writing a function or script to a spec, finding out why something broke, restructuring code that has become hard to change, and adding tests so it stays working. Each one is built around what a capable colleague would ask for first (the language version, the runtime, the exact error text, what the code is supposed to do) because generated code follows the quality of the spec far more than the wording of the request.
Pick by the stage you are at, not by language. If nothing exists yet, start with the Python or JavaScript prompt. If something exists and fails, use the debugging prompt, which works across languages and is the broadest of the set. If it works but is hard to live with, refactor. Before or after either, write tests. The prompts hand over to one another: the behaviors restated by the code-writing prompts become test cases, and a diagnosed bug becomes a regression test.
AI models are useful for drafting implementations, reading stack traces, proposing hypotheses and listing edge cases you had not considered. They do not run your code unless your tool has an execution environment, they cannot see files you have not shown them, and they can recall a library function that does not exist. Every prompt here therefore asks the model to separate what it knows from what it assumes, and you should treat the output as a pull request from a fast, well-read contributor: run it, test it and review it before it ships.
Give the model the error, the code and what you expected. It ranks likely causes by evidence, tells you how to confirm each, and proposes a minimal fix.
| If you need to… | Use |
|---|---|
| I have an error or a wrong result and need to find the cause | Bug Diagnosis Prompt |
| I need a Python script or module written from a description | Python Script and Module Prompt |
| I need a JavaScript or TypeScript function with a clear, typed contract | JavaScript and TypeScript Function Prompt |
| The code works but is hard to read, change or test | Behavior-Preserving Refactoring Prompt |
| I want tests for existing code, or tests first for code I am about to write | Unit Test Writing Prompt |
Give the model the error, the code and what you expected. It ranks likely causes by evidence, tells you how to confirm each, and proposes a minimal fix.
Restructures code in small, named steps while holding its observable behavior fixed, and reports anything it could not prove equivalent.
A contract-first prompt: the model fixes the signature, types and error behavior of a function or module before it writes the implementation.
Turns a plain-language spec into a complete, typed Python script or module, with assumptions, run commands and edge cases listed separately.
Produces a case inventory first, then readable tests that check behavior at the boundaries, with suspected bugs flagged instead of encoded as passing.
Guide
The five parts of a complete prompt (goal, context, task, constraints, output format), with a worked rewrite and a pre-send checklist.
Technical guide
How to split a large task into a sequence of focused prompts, design the handoffs between them, and debug the chain when a step goes wrong.
Technical guide
A seven-criterion rubric and a nine-field test record for evaluating prompts, plus why this site describes results in words and publishes no scores.