Debugging prompts work best when they include the full error, relevant code, expected behavior, and what you have tried. This library contains 35+ battle-tested prompts for error analysis, stack traces, performance issues, and systematic bug hunting - ready to copy into any AI coding assistant.

Debug Library

Debugging Prompts: 35+ Templates for Fixing Bugs with AI

Stop pasting error messages and hoping for the best. These debugging prompts are structured to give AI the context it needs to actually fix your bugs - not just explain them. Each template guides you to include the right information: error messages, code context, expected behavior, and what you have tried.

35+
Prompts
6
Categories
Fix-First
Approach
100%
Copy-Paste Ready
๐Ÿ”ด Error Analysis 8 prompts
๐Ÿ“š Stack Traces 5 prompts
โšก Performance 6 prompts
๐Ÿ“ Type Errors 5 prompts
๐Ÿƒ Runtime Issues 6 prompts
๐Ÿงช Testing 5 prompts

Error Analysis Prompts

Prompts for understanding and fixing error messages.

Quick Error Fix

Fast Fix Any Error
Prompt
Fix this error:

## Error
```
[paste full error message here]
```

## Code That Caused It
```[language]
[paste the relevant code - function or file section]
```

## Expected Behavior
[what should happen instead]

## Provide
1. Root cause explanation (1-2 sentences)
2. The fixed code (complete, runnable)
3. How to prevent this in the future

Comprehensive Error Analysis

Deep Analysis Complex Bugs
Prompt
Analyze and fix this error comprehensively:

## Error Message
```
[paste full error with stack trace]
```

## Code Context
```[language]
[paste relevant code - 50-150 lines max]
```

## Environment
- Framework: [Next.js 15 / React 19 / Node 20 / etc.]
- Language: [TypeScript / JavaScript / Python]
- Running in: [development / production / test]

## When It Happens
- Frequency: [always / sometimes / once]
- Trigger: [specific action that causes it]
- Started after: [recent change if known]

## What I've Tried
1. [attempt 1 and result]
2. [attempt 2 and result]

## Analysis Required
1. What is the root cause?
2. Why did my attempted fixes not work?
3. What is the correct fix?
4. Are there related issues I should check?

Provide the fixed code with comments explaining the changes.

Cryptic Error Message

Unclear Errors Interpretation
Prompt
Help me understand this cryptic error message:

## Error
```
[paste the confusing error]
```

## Context
- Where: [file/function where it occurs]
- When: [what action triggered it]
- Framework: [relevant framework]

## My Code
```[language]
[paste relevant section]
```

## Questions
1. What does this error actually mean in plain English?
2. What are the common causes?
3. How do I fix it in my specific case?

Be specific to my code, not generic documentation.

Build/Compile Error

Build Compile
Prompt
Fix this build/compile error:

## Build Output
```
[paste complete build error output]
```

## Build Command
[npm run build / tsc / vite build / etc.]

## Relevant Files
```[language]
// [filename]
[paste the file mentioned in error]
```

## Config Files (if relevant)
```json
// tsconfig.json or similar
[paste config]
```

## Recent Changes
[what changed before build started failing]

Provide:
1. What's causing the build to fail
2. The fix (code changes or config changes)
3. Build command to verify the fix

Multiple Errors Cascade

Multiple Errors Root Cause
Prompt
I'm getting multiple errors. Help me find and fix the root cause.

## All Errors
```
[paste all error messages]
```

## My Hypothesis
[what I think might be wrong, if any]

## Code
```[language]
[paste relevant code]
```

## Analysis Needed
1. Which error is the root cause (likely first)?
2. Which errors are cascading from the root cause?
3. What single fix will resolve all of them?

Focus on the root cause - don't fix symptoms.

Stack Trace Prompts

Prompts for analyzing stack traces and call chains.

Stack Trace Analysis

Stack Trace Deep Dive
Prompt
Analyze this stack trace and help me fix the bug:

## Stack Trace
```
[paste complete stack trace]
```

## Code at Error Location
```[language]
// [file and line from stack trace]
[paste code around the error location]
```

## Code That Called It
```[language]
// [calling function from stack trace]
[paste the calling code]
```

## Analysis Required
1. Walk through the call chain
2. Identify where things went wrong
3. Explain why the error occurred
4. Provide the fix

Show me exactly which function to change and how.

Async Stack Trace

Async Promises
Prompt
Debug this async error - the stack trace is incomplete:

## Error
```
[paste error and partial stack trace]
```

## Async Code
```[language]
[paste async functions involved]
```

## Call Flow (what I know)
1. [function A] calls
2. [function B] which awaits
3. [function C] where error might originate

## Context
- Using: [async/await / Promises / callbacks]
- Framework: [Next.js server actions / API routes / etc.]

## Help Needed
1. Where in the async chain is the error originating?
2. What's causing it (missing await, unhandled rejection, etc.)?
3. How do I add proper error handling?
4. Fixed code with proper async error handling

React/Next.js Component Stack

React Component Tree
Prompt
Debug this React component error:

## Error Message
```
[paste error - often shows component stack]
```

## Component Tree (from error or React DevTools)
```
[paste component hierarchy]
```

## Component Code
```tsx
// [Component that errored]
[paste component code]
```

## Parent Component
```tsx
// [Parent that renders this component]
[paste parent code]
```

## Error Occurs
- On: [mount / update / unmount / interaction]
- After: [specific user action]

Identify which component in the tree has the bug and provide the fix.

Performance Debugging Prompts

Prompts for diagnosing and fixing performance issues.

Slow Function/Query

Performance Optimization
Prompt
This code is slow. Help me optimize it:

## Slow Code
```[language]
[paste the slow function/query]
```

## Performance Data
- Current execution time: [X ms/seconds]
- Target execution time: [Y ms/seconds]
- Data size: [N records/items]

## Where It Runs
[API route / database query / client-side / etc.]

## Analysis Needed
1. What's making it slow? (N+1, missing index, algorithm, etc.)
2. What optimizations are possible?
3. Which optimization gives biggest improvement?
4. Provide optimized code

Prioritize fixes by impact. Show before/after code.

Memory Leak Investigation

Memory Leak
Prompt
I suspect a memory leak in this code:

## Symptoms
- Memory usage: [grows over time / spikes / never freed]
- When it happens: [specific action / continuous]
- App behavior: [slows down / crashes / OOM errors]

## Suspicious Code
```[language]
[paste code you suspect is leaking]
```

## Environment
- Runtime: [Node.js / Browser / React Native]
- Framework: [React / Next.js / Express / etc.]

## Common Leak Patterns to Check
- [ ] Event listeners not removed
- [ ] setInterval/setTimeout not cleared
- [ ] Closures holding references
- [ ] Caches growing unbounded
- [ ] Subscriptions not unsubscribed

## Provide
1. Identify the leak source
2. Explain why memory isn't being freed
3. Fixed code with proper cleanup
4. How to verify the fix worked

React Re-render Debugging

React Re-renders
Prompt
This React component re-renders too often:

## Component
```tsx
[paste component code]
```

## Parent Component
```tsx
[paste parent that passes props]
```

## Symptoms
- Re-renders on: [every keystroke / parent update / unrelated state change]
- React DevTools shows: [highlight updates info]

## State/Context Used
- Local state: [list useState hooks]
- Context: [which contexts consumed]
- Props: [what props received]

## Analyze and Fix
1. Why is it re-rendering unnecessarily?
2. Which props/state changes are triggering it?
3. What's the optimal solution? (useMemo, useCallback, memo, restructure)
4. Provide optimized code

Don't over-optimize - focus on the actual performance problem.

Database Query Performance

Database Query
Prompt
This database query is slow:

## Query
```sql
[paste query - SQL or ORM code]
```

## Explain/Execution Plan (if available)
```
[paste EXPLAIN output]
```

## Schema (relevant tables)
```sql
[paste table definitions and existing indexes]
```

## Performance
- Current: [X seconds]
- Table sizes: [N rows]
- Called: [X times per minute/hour]

## Optimize For
1. Query rewrite opportunities
2. Missing indexes
3. N+1 query issues (if ORM)
4. Caching opportunities

Provide optimized query AND any schema changes (indexes).

TypeScript Type Error Prompts

Prompts for fixing TypeScript and type-related errors.

Type Mismatch Error

TypeScript Types
Prompt
Fix this TypeScript type error:

## Error
```
[paste full TypeScript error message]
```

## Code
```typescript
[paste code with error]
```

## Type Definitions
```typescript
[paste relevant type/interface definitions]
```

## What I'm Trying to Do
[explain the intended behavior]

## Provide
1. Explain the type mismatch in plain terms
2. The correct fix (don't use 'any' unless necessary)
3. If types need to change, show the new types
4. Explain why the fix is correct

Generic Type Error

Generics Advanced
Prompt
Fix this generic type error:

## Error
```
[paste TypeScript error - often mentions type parameters]
```

## Generic Function/Type
```typescript
[paste the generic code]
```

## How I'm Using It
```typescript
[paste the usage that errors]
```

## Intent
I want this generic to: [describe desired behavior]

## Provide
1. Explain what's wrong with the generic constraints
2. Fixed generic definition
3. Correct usage example
4. Explain the type flow

Third-Party Library Types

Library @types
Prompt
Fix this type error with a third-party library:

## Error
```
[paste TypeScript error]
```

## My Code
```typescript
[paste code using the library]
```

## Library
- Package: [package name]
- Version: [version number]
- Types: [built-in / @types/package / none]

## What I'm Trying to Do
[explain intended usage]

## Options to Consider
1. Am I using the library API correctly?
2. Are the types outdated?
3. Do I need to extend/override types?
4. Should I create a declaration file?

Provide the least hacky solution first.

Runtime Issues Prompts

Prompts for debugging runtime behavior issues.

Undefined/Null Error

Common Null Safety
Prompt
Fix this undefined/null error:

## Error
```
[TypeError: Cannot read property 'X' of undefined]
[or similar null/undefined error]
```

## Code
```[language]
[paste the code with error]
```

## Data Source
The variable comes from: [API call / prop / state / database / etc.]

## When It Fails
- Works when: [describe working case]
- Fails when: [describe failing case]

## Fix Requirements
1. Identify where null/undefined enters
2. Proper null handling (not just || defaultValue)
3. Consider: optional chaining, nullish coalescing, guards
4. TypeScript: proper type narrowing

Provide defensive code that handles the edge case properly.

Race Condition / Timing Bug

Async Race Condition
Prompt Template text
I have a race condition or timing bug:

## Symptoms
- Fails: [X% of the time / under load / randomly]
- Behavior: [describe inconsistent behavior]
- Related to: [concurrent operations / async code / state updates]

## Code
```[language]
[paste async/concurrent code]
```

## Operations Involved
1. [Operation A - what it does]
2. [Operation B - what it does]
These can happen [simultaneously / in either order]

## What Should Happen
[describe correct ordering/behavior]

## Debug This
1. Identify the race condition
2. What operations are competing?
3. How to ensure correct ordering?
4. Provide fixed code with proper synchronization

Consider: locks, queues, state machines, or restructuring.

Infinite Loop / Recursion

Loop Hang
Prompt Template text
My code is stuck in an infinite loop or recursion:

## Symptoms
- Browser tab: [freezes / crashes]
- Console shows: [maximum call stack / no output]
- Happens when: [specific trigger]

## Suspicious Code
```[language]
[paste loop or recursive function]
```

## React Specific (if applicable)
```tsx
// useEffect or component code
[paste the component]
```

## Analyze
1. What condition should stop the loop/recursion?
2. Why is that condition never met?
3. Is there a dependency loop (useEffect deps)?
4. Fixed code with proper termination

Unexpected Behavior (No Error)

Logic Bug Silent Failure
Prompt Template text
This code runs without errors but produces wrong results:

## Code
```[language]
[paste the code]
```

## Input
```
[example input data]
```

## Expected Output
```
[what I expect to get]
```

## Actual Output
```
[what I actually get]
```

## My Debugging So Far
- I've checked: [list what you verified]
- I suspect: [your hypothesis if any]

## Analysis Needed
1. Walk through the code step by step with my input
2. Where does the logic diverge from expectation?
3. What's the bug?
4. Fixed code

Test Debugging Prompts

Prompts for fixing failing tests.

Failing Unit Test

Unit Test Jest/Vitest
Prompt Template text
Help me fix this failing test:

## Test Output
```
[paste test failure output - expected vs received]
```

## Test Code
```typescript
[paste the failing test]
```

## Implementation
```typescript
[paste the code being tested]
```

## Question
Is the test wrong or the implementation wrong?

## Provide
1. Which one is incorrect and why
2. The corrected code
3. If test was wrong: what was it actually testing vs. what it should test
4. If implementation was wrong: what was the bug

Flaky Test

Flaky Intermittent
Prompt Template text
This test passes sometimes and fails sometimes:

## Test
```typescript
[paste the flaky test]
```

## Pass Rate
Passes: [X%] of runs

## Failure Output (when it fails)
```
[paste failure message]
```

## Common Flakiness Causes to Check
- [ ] Timing/race conditions
- [ ] Order-dependent tests
- [ ] Shared state between tests
- [ ] External service dependencies
- [ ] Date/time dependent
- [ ] Random data without seeding

## Fix Requirements
1. Identify the source of flakiness
2. Make the test deterministic
3. No arbitrary sleeps/timeouts as solution
4. Provide stabilized test code

Mock Not Working

Mocking Jest
Prompt Template text
My mock isn't working as expected:

## Test with Mock
```typescript
[paste test code with mock setup]
```

## Module Being Mocked
```typescript
[paste the actual module/function]
```

## Problem
- Mock should return: [expected mock behavior]
- Actually does: [actual behavior - calling real implementation?]

## Mock Setup
- Using: [jest.mock / vi.mock / manual mock]
- Mock location: [same file / __mocks__ / etc.]

## Diagnose
1. Is the mock being applied correctly?
2. Is it a module/hoisting issue?
3. Is the import path correct?
4. Provide working mock setup

Systematic Debugging Process

When AI can't immediately solve your bug, follow this systematic approach:

1

Isolate the problem

Create the smallest code sample that reproduces the bug. Remove unrelated code until only the bug remains.

2

Add logging

Ask AI: "Add console.log statements to trace the data flow through this function." Follow the data to find where it goes wrong.

3

Test assumptions

Ask AI: "What assumptions does this code make? How can I verify each one?" Often bugs are broken assumptions.

4

Binary search

If you know it worked before, use git bisect or ask AI to help identify what change introduced the bug.

Frequently Asked Questions

How do I get AI to fix my bug instead of just explaining it?

Be explicit: "Provide the fixed code, not just an explanation." Include the full error, relevant code, and what you have tried. Ask for the complete corrected code block with file path. The prompts in this library include these instructions by default.

What information should I include when asking AI to debug?

Four things: (1) Full error message and stack trace, (2) Relevant code that triggered it, (3) What you expected to happen, (4) What you have already tried. More context means faster, more accurate fixes.

Can AI debug production issues?

Yes, but sanitize data first. Remove real user data, API keys, and internal URLs from logs before pasting. Use the production debugging prompts in this library which remind you to sanitize and focus on patterns rather than specific data.

How do I debug intermittent or race condition bugs with AI?

Describe the conditions: "This fails about 30% of the time, usually under load." Include timing information, concurrent operations, and any patterns you have noticed. AI can identify race conditions, deadlocks, and timing issues from descriptions.

Should I paste my entire file when debugging?

No, AI loses focus on long contexts. Paste: (1) The specific function with the bug, (2) Related functions it calls, (3) Type definitions if relevant. Keep it under 200 lines. Use the focused debugging prompts in this library.

How do I debug TypeScript type errors with AI?

Include: (1) The exact type error message, (2) The code causing it, (3) The type definitions involved. TypeScript errors often need the surrounding type context. Ask AI to explain the type mismatch and provide the corrected types.

Related Content