Coding
AI Prompts for Debugging Code
Use 10 copy-ready AI prompts to debug code, review diffs, write tests, check security and prevent unsafe or invented fixes.
The prompts
Copy a prompt, then replace the parts in [BRACKETS] with your own details.
Act as a senior [LANGUAGE] engineer. Diagnose the error below without guessing. Context: Language and version: [LANGUAGE AND VERSION] Framework and version: [FRAMEWORK AND VERSION] Runtime and operating system: [RUNTIME AND OS] Relevant package versions: [PACKAGES] Evidence: Full error message and stack trace: [PASTE ERROR AND STACK TRACE] Smallest reproducible code sample: [PASTE CODE] Expected behavior: [EXPECTED RESULT] Actual behavior: [ACTUAL RESULT] Recent changes: [RECENT CHANGES] What I tried: [ATTEMPTS AND RESULTS] Process: 1. Separate observed facts from hypotheses. 2. List ranked likely causes with evidence and confidence. 3. Ask only essential clarification questions. 4. Propose the smallest safe fix. 5. Provide a regression test and the exact command to run it. Output headings: Observed facts Ranked causes Clarifying questions Minimal patch Regression test Verification command Remaining risks Do not invent APIs, packages or test results. Mark unverified claims as UNKNOWN. I will run the tests myself.
Tip: Include the complete stack trace and a small reproduction, not only the final error line.
Act as a senior [LANGUAGE] engineer investigating a logic bug. Find the root cause, not just a line that looks unusual. Code: [PASTE RELEVANT CODE] Language and version: [VERSION] Inputs that fail: [FAILING INPUT] Expected output: [EXPECTED OUTPUT] Actual output: [ACTUAL OUTPUT] Inputs that pass: [WORKING INPUT] Recent changes: [CHANGES] Constraints: [CONSTRAINTS] First, trace the failing input through the code. Then identify the earliest point where the actual state differs from the expected state. Separate evidence from hypotheses. Return: 1. Root cause with the relevant file and line. 2. Step-by-step trace. 3. Minimal patch. 4. A regression test covering the failure. 5. Two edge cases to check. 6. Exact test or verification commands. Preserve the public API and unrelated behavior. Do not rewrite the file or add dependencies. Do not claim anything was run. Mark missing information as UNKNOWN.
Tip: Provide one failing input and one working input so the model can compare the paths.
Act as a test engineer for [LANGUAGE] and [FRAMEWORK]. Turn this bug report into a failing test that reproduces the problem. Project details: Language and version: [VERSION] Test framework and version: [TEST FRAMEWORK] Relevant module or function: [NAME] Bug report: [REPORT] Expected behavior: [EXPECTED] Current behavior: [ACTUAL] Code or interface under test: [PASTE CODE] Existing test style: [PASTE ONE EXAMPLE] Write a test first. Explain why it should fail before the fix. Use realistic, minimal data. Then describe the smallest implementation change that should make it pass, without writing that change unless requested. Output only these sections: Test file Why it should fail Expected result Likely fix area Additional edge cases Command to run Do not invent imports or APIs. Follow the existing test style. If required details are missing, ask targeted questions instead of guessing.
Tip: Paste one existing test so the generated test matches your project conventions.
Act as a careful code reviewer for [LANGUAGE] and [FRAMEWORK]. Review the supplied pull-request diff against the task and acceptance criteria. Task: [TASK] Acceptance criteria: [CRITERIA] Language, runtime and versions: [VERSIONS] Changed files and diff: [PASTE DIFF] Relevant surrounding code: [PASTE CODE] Test output: [PASTE RESULTS] Constraints: [CONSTRAINTS] Check correctness, regressions, error handling, security, performance, tests, scope creep and compatibility. Review the diff, not the whole project. Do not suggest a rewrite unless the current change cannot meet the criteria. Return a table with these columns: Severity: blocker, high, medium, low or note File and line Issue Evidence Why it matters Smallest suggested change Verification step Then provide: accepted strengths, missing tests and questions for the author. Report no issue when the evidence does not support one. Do not claim tests ran.
Tip: Include acceptance criteria and test output, because a diff without its intended behavior is hard to judge.
Act as a secure code reviewer for [LANGUAGE] and [FRAMEWORK]. Review this code for one focused security area: [SECURITY AREA]. Versions and environment: [VERSIONS AND ENVIRONMENT] Threat model: [WHO CAN ATTACK AND WHAT THEY CAN ACCESS] Relevant code or diff: [PASTE CODE OR DIFF] Input sources: [USER INPUT, API, FILE, DATABASE OR OTHER] Sensitive assets: [ASSETS] Existing controls: [CONTROLS] Check validation, authorization, authentication, injection, secrets, data exposure, unsafe file or network access and error handling, but focus on the named security area first. Return: 1. Findings ranked by severity. 2. File and line for each finding. 3. Evidence and attack path. 4. Smallest safe remediation. 5. A security regression test. 6. Verification steps. 7. Unknowns requiring review. Do not request or repeat real secrets. Do not invent an exploit, library function or test result. Preserve behavior outside the stated scope.
Tip: Name the threat model and sensitive asset, because security advice depends on what an attacker can reach.
Act as a senior [LANGUAGE] maintainer. Refactor the code below for [GOAL], while preserving behavior and the public API. Language and version: [VERSION] Framework and package versions: [VERSIONS] Goal: [READABILITY, DUPLICATION, PERFORMANCE OR OTHER] Code: [PASTE CODE] Public interfaces to preserve: [INTERFACES] Behavior that must not change: [BEHAVIOR] Performance target: [TARGET] Allowed dependencies: [RULES] Existing tests: [PASTE TESTS OR RESULTS] First list the proposed changes and risks. Then provide a minimal unified diff. Keep the diff limited to the stated goal. Do not alter schemas, dependencies, error contracts or public names without approval. Return: Plan Assumptions Minimal diff Behavior-preservation checks Tests to add or run Remaining risks Do not claim the refactor was tested. Mark any uncertain behavior as UNKNOWN.
Tip: State the exact behavior and interfaces that must stay unchanged before asking for a diff.
Act as a test engineer for [LANGUAGE] and [TEST FRAMEWORK]. Create edge-case and regression tests for this function or module. Code or interface: [PASTE CODE] Purpose: [WHAT IT SHOULD DO] Language and version: [VERSION] Inputs and outputs: [DETAILS] Known bug or risk: [BUG OR RISK] Existing tests: [PASTE EXAMPLES] Constraints: [NO MOCKS, NO NEW DEPENDENCIES OR OTHER RULES] Identify input partitions, boundaries, invalid values, empty values, duplicate values, large values, time or concurrency cases and failure paths relevant to this code. Do not add irrelevant cases. Return: 1. A short test matrix. 2. Test file only, using the existing style. 3. The bug or boundary each test covers. 4. Expected results. 5. The command to run the tests. Use only APIs shown in the supplied code or official [FRAMEWORK] conventions. Mark assumptions as UNKNOWN. Do not claim the tests ran.
Tip: Share existing tests and constraints so the output fits your runner instead of using generic examples.
Act as a senior test engineer investigating an intermittent failure in [LANGUAGE] and [TEST FRAMEWORK]. Find likely causes without treating one guess as fact. Test code: [PASTE TEST] Application code involved: [PASTE RELEVANT CODE] Failure message and stack trace: [PASTE DETAILS] Pass rate or failure pattern: [OBSERVATION] Environment and versions: [DETAILS] Parallelism, timing or order settings: [DETAILS] Recent changes: [CHANGES] Separate evidence from hypotheses. Rank causes such as shared state, timing, order dependence, network or database reliance, cleanup, random data and environment differences. Return: Evidence summary Ranked hypotheses with confidence Instrumentation to add Smallest stabilizing change Regression test or reproduction loop Commands to verify Risks of hiding a real defect Do not recommend arbitrary sleeps unless you explain why they address a measured race. Do not claim reproduction or test execution.
Tip: Include how often the test fails and whether it fails only in parallel or in a specific environment.
Act as a dependency and API verification reviewer. Check this proposed code for invented functions, wrong package names, removed APIs and version mismatches. Language and version: [LANGUAGE AND VERSION] Runtime: [RUNTIME] Framework and package lock details: [VERSIONS] Documentation or API target: [OFFICIAL DOCUMENTATION LINK OR NAME] Proposed code: [PASTE CODE] Error message, if any: [PASTE ERROR] Required compatibility: [SUPPORTED VERSIONS] For every external API, package or configuration key, classify it as CONFIRMED, LIKELY or UNKNOWN. Explain what evidence supports the classification. Identify incompatible syntax and suggest the smallest correction. Return a table with: Item Status Evidence Version concern Suggested check Then provide a corrected minimal snippet only if the evidence supports it. Do not invent documentation, package names, functions or test results. Tell me what to verify in official documentation.
Tip: Provide the lock file or exact dependency versions, not only the package names.
Act as a senior engineer reviewing AI-generated code before merge. Be skeptical but evidence-based. Task the code should complete: [TASK] Acceptance criteria: [CRITERIA] Language, framework and versions: [VERSIONS] Generated code or diff: [PASTE CODE OR DIFF] Relevant existing code: [PASTE CONTEXT] Tests and output: [PASTE RESULTS] Constraints: [SECURITY, PERFORMANCE, API OR COMPATIBILITY RULES] Check correctness, hidden assumptions, error handling, security, data validation, concurrency, performance, maintainability, dependency changes and scope. Look for invented APIs and code paths without tests. Return: 1. A severity-ranked review table with file, line, issue, evidence and fix. 2. Missing tests, including edge cases. 3. Claims that require documentation or local verification. 4. A merge recommendation: do not merge, revise, or ready after checks. 5. The smallest required changes. Do not rewrite the code unless a specific finding requires it. Do not claim tests ran. Mark uncertain findings as UNKNOWN.
Tip: Add the acceptance criteria and generated diff together, so the review can test intent against implementation.
Use these prompts when you need help debugging, reviewing or testing code with ChatGPT, Claude or Gemini. They ask for evidence, version details, small changes and runnable checks instead of a blind rewrite.
How to use this pack
Choose one prompt for one job. Replace every [BRACKETED] field before you send it. Include the smallest useful code sample, the full error message and the expected behavior. Add the language version, framework, runtime, operating system and relevant package versions.
Remove API keys, passwords, access tokens, customer data and private business logic. If a file contains sensitive code, create a small reproduction first. Do not ask an AI tool to change production files, databases or dependencies without approval.
Ask the model to label assumptions and unknowns. It should separate observed evidence from likely causes. It should not claim that tests ran unless you provide the results or use a tool that can run them.
How to adapt the prompts
For Python, include the Python version, package manager and test command. For JavaScript or TypeScript, include the Node.js version, package manager, compiler settings and test runner. For PHP, Java, Go or another language, name the runtime and framework versions.
Keep the requested output narrow. Ask for a diagnosis, a minimal patch, a diff, a test file or review comments. Do not combine debugging, architecture, deployment and a full rewrite in one request.
For code review, provide the task description, acceptance criteria, changed files, diff and test output. For agent workflows, define allowed files, forbidden files, permitted commands, network access and rollback rules.
Mistakes to avoid
A prompt such as Fix this code gives the model too little context. Pasting only the final stack trace line also hides useful evidence. Do not accept new packages, invented APIs or changed public interfaces without checking them against official documentation.
Run the suggested tests yourself, inspect every diff and check edge cases. Treat generated code as a proposal until it passes your normal lint, type-check, security and test steps.
Frequently asked questions
What is the best prompt for debugging code?
Give the AI your language and version, framework, full error, stack trace, smallest reproduction, expected behavior and actual behavior. Ask for ranked causes, evidence for each cause, a minimal patch and a regression test. This structure reduces guessing and makes the answer easier to check.
Can ChatGPT fix code from only a stack trace?
It can suggest likely causes, but a stack trace alone often lacks the failing input, source code and version details. Provide the relevant function, recent changes and expected result. Ask the model to mark uncertain claims and request missing information before proposing a fix.
Should I paste the whole file into an AI tool?
Usually, no. Start with the smallest reproduction that still shows the problem. Include nearby functions, configuration and data needed to reproduce it. Redact secrets, personal data and private logic. A smaller example often produces a more focused diagnosis.
How do I stop AI from inventing APIs or packages?
Name the exact language, runtime, framework and package versions. Ask the model to mark unverified APIs as UNKNOWN and use only documented functions. Check suggestions against official package documentation, lock files and your compiler or test output before using them.
How can I verify an AI-generated code fix?
Ask for a runnable regression test, the exact command to run and the expected result. Then run tests, linting and type checks yourself. Inspect the diff for changed behavior, new dependencies, security issues and edge cases before merging the fix.
More in Coding
Help keep every tool free
FreeToolr stays free because people help it grow. Share a tool you like, tell us what to build next, or support us on Ko-fi.