Debugging With AI

Stop spending hours on bugs that AI can fix in 30 seconds. The right debugging prompt structure gets you from error to fix on the first attempt.

Beginner 11 min read Lesson 8 of 20

The Debugging Formula

The quality of a debug session depends almost entirely on the information you provide. Use this four-part formula:

Debug prompt formula
1. THE CODE:
   Paste the function/section where the error is (not the entire 500-line file)

2. THE ERROR:
   Exact error message, including file name and line number

3. WHAT I DID:
   Exact steps to reproduce ("I submitted the form with email='test@test.com'")

4. WHAT I EXPECTED:
   What should have happened ("it should redirect to /dashboard")

Template:
"Here is my code: [paste code]
Error: [exact error message with line number]
I trigger this by: [steps to reproduce]
Expected behavior: [what should happen]
Fix it."

Different Error Types

Type 1: Fatal error with message
> My PHP code throws this error when I try to login:
  Fatal error: Uncaught PDOException: SQLSTATE[42S22]:
  Column not found: 1054 Unknown column 'email_address' in 'where clause'
  in /app/models/UserModel.php:47

  Here is the login function:
  [paste UserModel::login() method]

  Fix the query to use the correct column name.
Type 2: Wrong behavior (no error message)
> My login form submits with no error, but the user is not logged in.
  The page just reloads. Here is the login() method:
  [paste the method]

  When I submit: email=test@example.com, password=Test1234!
  Expected: redirect to /dashboard and session $_SESSION['user_id'] set
  Actual: page reloads, session not set

  Find the bug and fix it.
Type 3: JavaScript / console error
> My JavaScript throws this error in the browser console:
  TypeError: Cannot read properties of null (reading 'addEventListener')
  at initForm (app.js:23:35)

  Here is the relevant code:
  [paste initForm function]

  This happens when I open the page. Fix it.
Type 4: Test failing
> This PHPUnit test is failing:

  Test: UserTest::testCreateWithDuplicateEmail
  Failure: Expected exception NotUniqueException to be thrown but it was not.

  Here is the test:
  [paste test method]

  Here is the create() method it tests:
  [paste UserModel::create()]

  The test expects an exception when inserting a duplicate email.
  Fix the create() method to throw NotUniqueException when email already exists.

Debug Prompt Templates

20 debug prompt starters - copy what fits
# Error messages:
"I get this error: [error]. Here is the relevant code: [code]. Fix it."
"PHP Warning: [message]. Which line causes it and how do I fix it?"
"Uncaught exception: [message] at [file:line]. Here is that code: [code]."

# Wrong output:
"This function returns [wrong value] but should return [correct value]. Here is the code: [code]."
"This SQL query returns 0 rows but there is data in the table. Query: [sql]. Fix it."
"The page shows a blank white screen. Error logs show: [log line]. Here is index.php: [code]."

# Behavior bugs:
"Form submits but nothing is saved to the database. Handler: [code]. Database table: [schema]."
"Login works but session expires immediately. Here is my session handling: [code]."
"The file upload works on localhost but fails on the live server. Error: [message]. Code: [code]."

# Database bugs:
"This query is very slow (takes 10 seconds). Explain why and add indexes if needed. Query: [sql]. Table has 500,000 rows."
"My JOIN is returning duplicate rows. Query: [sql]. Expected one row per user."

# CSS/HTML bugs:
"My Bootstrap card layout breaks on mobile screens. HTML: [code]. CSS: [code]. Fix it."
"The sticky navbar overlaps my page content. CSS: [code]."

# API/network bugs:
"My API call returns 200 but the response body is empty. PHP endpoint: [code]. JS fetch: [code]."
"CORS error: Access-Control-Allow-Origin header missing. My PHP endpoint: [code]. Fix headers."

When It Is Still Broken

If the first fix attempt does not work, do not start over. Add more information and continue in the same session:

Follow-up prompts when still broken
# After applying the fix and still getting an error:
"I applied the fix but now I get a different error:
 [new error message]
 The code now looks like: [updated code]"

# If the behavior is still wrong:
"The error is gone but the behavior is still wrong.
 I am getting [what I see] instead of [what I expect].
 Here is the updated code:"

# To add a debug trace:
"Add var_dump() / console.log() statements to help me trace
 what value each variable has at each step."

# To isolate the problem:
"Create a minimal test script that reproduces this bug in isolation
 (no framework dependencies)."

# Nuclear option - start fresh:
"Forget the previous attempts. Here is the original requirement:
 [describe what you need from scratch]. Write it again correctly."

Harder Bugs - Adding Context

For bugs that do not yield to the basic formula, give the AI more environmental context:

Rich context debug prompt
"I have a bug I cannot track down. Here is all the context:

Environment: PHP 8.2, MySQL 8, Apache on Ubuntu 22.04
The bug: Users report that sometimes their cart is empty after checkout
  - Happens about 10% of the time
  - Cannot reproduce locally
  - Only happens on the live server
  - Server is under high load when it happens

Relevant code:
[paste cart.php checkout section]

Database tables involved:
[paste CREATE TABLE for carts and orders]

What I have tried already:
- Added session_write_close() before redirect
- Checked that DB transactions are committed

What is the most likely cause and how do I add logging to confirm?"

When AI Cannot Find the Bug

Some bugs require information the AI does not have:

SituationWhat to do
Bug depends on actual database dataExport a sample of the problematic data and paste it into the prompt
Bug only appears with specific user actionsRecord the exact HTTP requests (browser Network tab) and paste headers/body
Bug is in server configurationPaste your Apache/nginx config and PHP.ini relevant sections
Race condition / timing bugAsk the AI to add mutex/locking and explain the race condition
Bug in third-party libraryPaste the error trace and the library version. Ask for a workaround.
Ask AI to add logging first, not fix first

For stubborn bugs, resist the urge to ask "fix it" immediately. Instead: "Add detailed logging to this code so I can see exactly what values each variable has." Run the logged version, collect the output, then bring it back: "Here is the log output: [logs]. Now fix the bug." This approach is 10x more effective than blind fix attempts.

Frequently Asked Questions

For small files (under 100 lines), yes. For large files, give the AI: (1) the function/section where the error occurs (20-40 lines), (2) the exact error message, (3) what inputs cause the error. The AI does not need the entire file to fix a function-level bug.

This is normal and expected. Report the new error immediately in the same session: "The previous error is fixed but now I get: [new error]." The AI has the full context of what it changed and can correct it. Do not start a new session for cascading bugs - continue in the same one.

Yes - describe the conditions: "This error happens only when a user has admin role AND uploads a file larger than 5MB". AI can analyze the code path that would be triggered by those conditions and find the likely cause even without running the code.