Prompts for Coding: Debug, Explain, Refactor
Copy-ready ChatGPT and Claude prompts for debugging errors, explaining unfamiliar code and refactoring safely, plus what to include so answers fit your code.
By Orgwyse
Published
Last updated
8 min read
The best coding prompts give the AI what a colleague would need: the exact code, the full error, what you expected to happen, and what you've already tried. Then ask for one job at a time (find the bug, explain the code, or refactor it) and say what must not change. The prompts below are ready to copy; replace each [bracket] with your own details.
If you want the general method behind these, see how to write a good prompt. This post sticks to code.
What every coding prompt needs
Most weak answers come from missing context, not a weak model. Before you send anything, check that your prompt has:
- Language, version and framework. "Python 3.12 with Django 5" gets a different answer than "Python."
- The real code. Paste the function or file involved, not a paraphrase of it. If it calls other code, mention what that code does or paste it too.
- The full error and stack trace. Copy it exactly. The line numbers matter.
- Expected vs. actual behavior. "It should return 3 items; it returns an empty list."
- What you've tried. This stops the AI from suggesting the fix you already ruled out.
- Constraints. No new dependencies, keep the public API, must run on Node 20, and so on.
Put your instructions first and separate them from the code. OpenAI's prompting advice is to place instructions at the start of the prompt and use ### or """ to mark off the material you paste below them (OpenAI Help Center). In a chat, a Markdown code fence (three backticks) is another clear way to mark off pasted code.
Prompts for debugging
1. Fix a specific error
I'm getting an error in [language/framework + version].
What I expected: [expected behavior]
What happens instead: [actual behavior]
What I've already tried: [attempts]
Error and stack trace:
"""
[paste the full error]
"""
Code:
"""
[paste the relevant code]
"""
Explain the cause in two or three sentences, then show the smallest change that fixes it. If you need to see another file to be sure, tell me which one instead of guessing what it contains.That last line matters. Anthropic's sample instruction for agentic coding tells the model never to speculate about code it hasn't opened (Anthropic: prompting best practices). In a chat, the equivalent is asking it to request the file rather than invent it.
2. A bug with no error message
This [function/component] runs without errors but gives the wrong result.
Input: [example input]
Expected output: [expected]
Actual output: [actual]
Code:
"""
[paste]
"""
List the three most likely causes, most likely first. For each, tell me one line I can add (a print, log or assert) to confirm or rule it out. Don't rewrite the code yet.Asking for hypotheses and checks before a fix keeps you in control. You run the checks, paste the output back, and the next answer is based on facts instead of guesses.
3. "It works on my machine"
This code works locally but fails in [CI / production / a colleague's machine].
Local: [OS, runtime version, how I run it]
Failing environment: [OS, runtime version, how it runs there]
Error there: [paste]
Code and config:
"""
[paste the code plus relevant config: package.json, requirements.txt, Dockerfile, env var names without values]
"""
What differences between the two environments could cause this? Rank them, and tell me how to check each one.4. Read a stack trace with me
Walk me through this stack trace from top to bottom. For each frame, say in one line what was happening. Then point to the frame where my code (not a library) went wrong and explain why.
"""
[paste trace]
"""Prompts for explaining code
5. Explain unfamiliar code
Explain what this code does for someone who knows [language] but has never seen this codebase.
1. One-paragraph summary of its purpose.
2. A walk-through of the main steps, in order.
3. Anything surprising: side effects, global state, hidden I/O, or performance traps.
4. Questions I should ask the original author.
"""
[paste]
"""6. Explain one confusing line
In the code below, explain line [number] (or: the part that starts with [snippet]). What does it do, why might someone have written it this way, and what's a clearer way to write the same thing?
"""
[paste]
"""7. Explain an error message in plain words
Explain this error message as if I'm new to [language/tool]. What does it mean, what usually causes it, and what should I look at first in my code?
"""
[paste error]
"""8. Learn a concept from your own code
I don't really understand [concept: closures, async/await, SQL joins, generics]. Explain it using the code below as the example, not a made-up one. Then give me one small exercise based on this code so I can check I understood it.
"""
[paste]
"""Prompts for refactoring
Refactoring goes wrong when the AI "improves" things you didn't ask about. Be clear about what can change and what can't. Anthropic notes that some of its models (it names Claude Opus 4.5 and Opus 4.6) can over-engineer by adding extra files, abstractions or flexibility nobody asked for, and suggests telling the model to make only the changes that were requested (Anthropic: prompting best practices).
9. Refactor without changing behavior
Refactor the code below to make it easier to read and maintain.
Rules:
- Behavior must stay exactly the same, including edge cases and error messages.
- Keep the function names and signatures that other code calls: [list them].
- No new dependencies.
- Don't add features, comments or type hints to parts you don't change.
First list the changes you plan to make and why, in one line each. Then show the refactored code. Finally, list anything you weren't sure was safe to change.
"""
[paste]
"""10. Break up a long function
This function is [number] lines long and does too much. Suggest how to split it into smaller functions. For each new function give a name, a one-line purpose, its inputs and outputs. Show the new version of the original function calling them. Don't change what it returns.
"""
[paste]
"""11. Modernize old code
This code was written for [old version, e.g. Python 2.7 / jQuery / class components]. Update it to [target version/style]. Keep the behavior the same. After the code, give a short list of each change and the reason for it, so I can review them one by one.
"""
[paste]
"""12. Make it faster, with proof
This code is too slow when [situation, e.g. the list has 100,000 items]. Find the slowest part and explain why it's slow. Suggest one change, show it, and tell me how to measure the before and after so I can confirm it helped.
"""
[paste]
"""Prompts for tests and reviews
13. Write tests for existing code
Write [test framework] tests for the function below. Cover the normal case, edge cases (empty input, very large input, wrong types) and the error paths. Don't change the function itself. If a test you'd expect to pass would actually fail, point that out instead of adjusting the test to match the code.
"""
[paste the function]
"""The last sentence borrows from Anthropic's advice for agentic coding: ask the model to tell you when tests are wrong rather than working around them (Anthropic: prompting best practices).
14. Review before you merge
Review this change as a careful senior developer. Order your findings by severity: bugs first, then security, then performance, then readability. For each one, quote the lines, explain the problem, and show a fix. Skip style nitpicks unless they hide a real problem. If everything looks fine, say so plainly.
"""
[paste diff or code]
"""For bigger reviews, run them as separate steps (map the change, list risks, then check each risk). The code review chain in our prompt chaining guide shows how.
Small habits that improve every answer
Say "change" when you want changes. Anthropic points out that "Can you suggest some changes?" may get you suggestions, while "Change this function to improve its performance" gets the edit itself (Anthropic: prompting best practices). Pick the wording that matches what you want.
Reply with the exact failure. If the fix doesn't work, paste the new error and say "that changed the error to this." Don't just say "still broken."
Start fresh when a chat goes in circles. A long thread full of failed attempts can pull the answer back toward them. Open a new chat, paste the current code and the latest error, and list what didn't work.
Run everything before you trust it. AI-written code can look right and still be wrong. Run it, run your tests, and read the diff the same way you'd read a teammate's.
Keep secrets out of what you paste
Before you paste a file or a config, remove API keys, passwords, tokens and connection strings. Replace them with placeholders like YOUR_API_KEY. OpenAI's own key-safety advice is to never commit keys to a repository, to keep them in environment variables, and to rotate a key right away if you think it has leaked (OpenAI: Best practices for API key safety). The same habit protects you in a chat window. If your work code is confidential, check your company's rules first. For what ChatGPT keeps and the settings you control, see are ChatGPT chats private?
FAQ
What's the best ChatGPT prompt for debugging code?
One that includes the exact code, the full error message, what you expected, what actually happened and what you've tried. Then ask for the cause first and the smallest fix second, and tell it to ask for missing files instead of guessing.
Should I paste my whole codebase into ChatGPT or Claude?
Usually not. Paste the function or files involved in the problem, plus a short note on what the surrounding code does. Add more only when the AI asks for it. Smaller, focused input tends to get more focused answers.
How do I stop AI from changing too much when refactoring?
List what must not change (behavior, function names, public APIs, dependencies), ask for a plan before the code, and say plainly that it should make only the changes you asked for.
Can I trust code that ChatGPT or Claude writes?
Treat it like code from a new teammate: often good, sometimes wrong in ways that look fine. Run it, test it and read the diff before you use it.
Is it safe to paste code into an AI chat?
Remove API keys, passwords and tokens first, and follow your employer's rules for confidential code. Check your chat app's data settings for how your conversations are stored and used.
Our product
If you reuse prompts like these, Colloquin, a Chrome extension made by Orgwyse, lets you save up to 10 prompts for free and insert them into the message box on ChatGPT and Claude. They're stored locally in your browser. Get Colloquin on the Chrome Web Store.
Last updated 2 November 2026 · Spotted something out of date? [email protected]
Related guides
Prompting
How to Write a Good Prompt: A 5-Part Framework
A simple five-part framework for writing prompts that get better answers from ChatGPT, Claude, or any AI. Includes before and after examples.
Prompting
Prompt Chaining Explained: Break Big AI Tasks Into Steps
Prompt chaining means splitting one big request into a sequence of smaller prompts. Here's how it works, with examples you can run today.
Privacy
Are ChatGPT Chats Private? What's Stored and What You Control
What happens to your ChatGPT chats, which privacy settings you control, and how to keep your own notes off the provider's servers.