Skip to content

AIExplore

Narrow Bugs With ChatGPT and a Tiny Failing Example

Cut the failing code down to the smallest example that still breaks, then ask ChatGPT for help so the answer targets the real problem.

When a bug hides inside a large codebase, pasting the whole file into ChatGPT rarely helps. The answer comes back vague because ChatGPT has to guess which part matters. A minimal reproduction flips that. You strip the code down to the smallest version that still fails, and suddenly ChatGPT can see the bug clearly.

Building a minimal repro also helps you. Half the time you find the bug yourself while removing lines. The other half, you hand ChatGPT a clean puzzle with one moving part, and the answer arrives in seconds. Either way, you save time.

What a minimal reproduction is

A minimal reproduction is the shortest code that shows the bug. It removes every line that does not change the failure. No extra imports, no unrelated state, no database calls when the bug is in a math function. The goal is to make the bug impossible to miss.

When to build a minimal repro before asking ChatGPT

  • The file is longer than about fifty lines and the bug could be anywhere
  • The error message is generic and does not point to a specific line
  • You have tried pasting the full file and the answer was not useful
  • Multiple systems interact and you need to isolate which one is broken
  • You want to post the question in a forum or share it with a teammate later

Prompt with a stripped down state bug

Minimal reproduction:
let count = 0;

function increment() {
  setTimeout(() => { count = count + 1; }, 0);
}

increment();
increment();
console.log(count);

Expected: 2
Actual: 0

Task: explain why count is 0 after calling increment twice.
Walk through the event loop timing.

Why this prompt works

The code is nine lines. There is nothing to skip and nothing to guess. ChatGPT can focus entirely on the setTimeout timing issue. If this same bug lived inside a 200 line React component, the answer would have to sort through props, state, and effects before reaching the real cause.

Prompt with a data transformation bug

Minimal reproduction:
const items = [
  { name: "A", price: 10 },
  { name: "B", price: 20 },
  { name: "C", price: 30 },
];

const filtered = items.filter(i => i.price > 15);
const names = items.map(i => i.name);

Expected names after filter: ["B", "C"]
Actual names: ["A", "B", "C"]

Task: explain the bug. Why does names include all items?

Prompt with a network request issue

Minimal reproduction:
async function fetchUser(id) {
  const res = await fetch("/api/user/" + id);
  const data = res.json();
  return data.name;
}

const name = await fetchUser(1);
console.log(name); // undefined

Task: explain why name is undefined even though the API returns { "name": "Alice" }.

How to build the minimal version

  • Start with the failing code in its original file
  • Remove imports and modules that do not affect the failure
  • Replace database calls or API calls with hard coded values
  • Delete every function the failing path does not call
  • Run the stripped version and confirm it still fails the same way

How to refine the answer

If ChatGPT gives the right diagnosis but you want more detail, ask it to trace the values line by line. For the state bug example, you could say walk through what count equals after each line executes including the event loop. A line by line trace catches subtleties that a summary might skip.

Common mistakes

  • Pasting the full file when only ten lines matter
  • Removing a line that actually contributes to the bug, which changes the failure
  • Forgetting to include the expected versus actual values
  • Asking for a fix before confirming the diagnosis matches the real bug
  • Skipping the step where you run the minimal version to confirm it still fails

How to check the diagnosis

Take the explanation and predict what would happen if you changed one thing. If ChatGPT says the bug is a missing await, add the await in your minimal repro and run it. If the output changes to the expected value, the diagnosis is right. If not, share the updated result and ask again.

Takeaway

A minimal repro is the fastest way to get a sharp answer from ChatGPT. Strip the code, confirm the failure, and let ChatGPT focus on the one thing that is actually broken.

Related articles