Fix Errors with Claude Code: Reproduce → Cause → Minimal Fix → Rerun
Instead of telling AI only that 'there is an error,' reproduce the problem with the same input, narrow down the cause from the actual exception message, and request only the minimum necessary fix. Using a CSV where an amount contains a comma, reproduce a ValueError in Python and rerun the corrected code to verify the fix.
Content checked 2026.09.22Copyable prompts
Show contents
Who this is forBeginners who want to control the debugging process using error-message evidence when a small Python tool made with Claude Code fails
What you need
Understand the summarize.py and expenses.csv workflow from the previous articles
Be able to run a command in the form python summarize.py filename in a terminal
Preserve the original file and the error message first when an error occurs
Use only synthetic CSV data rather than real personal information
01Reproduce the error with the same input before fixing anything
When an error occurs, the first step is reproduction, not guessing. In this example, we use a synthetic CSV in which one amount value is the string "1,450". Because a comma appears inside the value, the CSV field is quoted; after the csv module reads it, row['amount'] contains the string '1,450'. If the existing code calls int(row['amount']) directly, Python cannot convert that string to an integer.
import csv
import sys
from collections import defaultdict
path = sys.argv[1]
monthly = defaultdict(int)
with open(path, encoding="utf-8", newline="") as f:
reader = csv.DictReader(f)
for row in reader:
month = row["date"][:7]
monthly[month] += int(row["amount"])
for month in sorted(monthly):
print(f"{month} = {monthly[month]}")
Running this code with `python summarize_buggy.py expenses_bad.csv` stops at the conversion step before printing the correct totals. The same code was actually run with the Python tool in GPT chat, and the exception string was confirmed to match the text below exactly.
Prompt
ValueError: invalid literal for int() with base 10: '1,450'
02Separate the failing value and operation from the error message
The message already contains important clues. The failed function is int(), and the failed value is '1,450'. There is no need to first assume that 'the entire CSV is wrong' or that 'the monthly aggregation logic is incorrect.' The problem scope can be narrowed to the point where the amount string is converted to an integer.
Observation
Confirmed fact
What not to assume yet
Exception type
ValueError
Do not conclude that the entire file is corrupted
Failed value
'1,450'
Do not assume every other row is also wrong
Failed operation
int() conversion
Do not conclude that the aggregation formula itself is wrong
Change scope
Prioritize the amount parsing point
Do not rewrite the entire program
When asking Claude Code for help, provide the error message, the reproduction command, and the input condition that triggered the problem. This makes it possible to check whether the AI's explanation is connected to the actual evidence and makes it easier to narrow the scope again if an unrelated large change is proposed.
03Ask for the cause first, then request only the minimal fix
The required fix is simple. Remove commas from a numeric string before converting it to an integer, and if the value is still not numeric after comma removal, raise a clear error that identifies the row and the original value. Do not change the monthly aggregation method or output format.
Prompt
Running python summarize_buggy.py expenses_bad.csv produces the error below.
ValueError: invalid literal for int() with base 10: '1,450'
Explain the cause first.
Then make the smallest possible change only to the amount conversion logic.
Requirements:
- For numeric strings containing commas, remove the commas before converting to int
- If the value is still not numeric, raise a clear ValueError that includes the row number and original value
- Keep the monthly aggregation and output format unchanged
- Use only the Python standard library
- Also tell me which command to run afterward to verify the fix.
Do not ask to 'rewrite the entire code.' The failure location has already been narrowed to amount conversion, so limit the change to that part. If the AI tries to modify other files or aggregation rules, ask why that is necessary first, and return to the original scope if there is no evidence supporting the expansion.
04Put comma removal and row-number errors into one function
The revision below separates amount parsing into parse_amount(). It first removes commas and tries int(); if conversion fails, it raises a new ValueError that includes the input row number and the original string. Because the header of csv.DictReader is row 1, data row numbers are aligned with enumerate(reader, start=2).
Prompt
import csv
import sys
from collections import defaultdict
def parse_amount(text, line_number):
cleaned = text.replace(",", "")
try:
return int(cleaned)
except ValueError as exc:
raise ValueError(
f"line {line_number}: amount must be an integer, got {text!r}"
) from exc
path = sys.argv[1]
monthly = defaultdict(int)
with open(path, encoding="utf-8", newline="") as f:
reader = csv.DictReader(f)
for line_number, row in enumerate(reader, start=2):
month = row["date"][:7]
monthly[month] += parse_amount(row["amount"], line_number)
for month in sorted(monthly):
print(f"{month} = {monthly[month]}")
This change is not intended to be a general-purpose parser for every money format such as currency symbols, decimals, or parenthesized negatives. It solves only the thousands-separator comma issue that was actually reproduced here. To support broader input rules, first define the allowed formats and add them as a separate requirement.
05Rerun with the same file and compare against the original expected values
After the fix, rerun the code with the exact file that caused the problem. The revised code above was actually run against `expenses_bad.csv` with the Python tool in GPT chat, and the monthly totals were 2026-09 = 26600 and 2026-10 = 9800. These exactly match the expected values defined from the beginning of the series.
Do not stop merely because the error disappeared. The key point of the rerun is to verify numerically that both 1450 without a comma and "1,450" with a comma are interpreted as the same amount and that the original correct result is preserved.
06Do not silently skip non-numeric values; fail with their location
Adding comma removal does not mean invalid values should be replaced with 0 or ignored. For example, if the amount in row 3 is abc, the program should fail so the data error is not hidden. The same revised code was actually run on a separate synthetic input, and a ValueError containing `line 3` and the original value `'abc'` was confirmed.
ValueError: line 3: amount must be an integer, got 'abc'
First confirm that the error can be reproduced with the same input.
Read the exception type and failed value to narrow the change scope.
Ask AI to explain the cause, then request only the minimum fix.
Rerun with the input that caused the problem and compare against the original expected values.
Do not silently ignore invalid input; leave an error that makes the problem locatable.
What to check yourself
Python code actually run with the Python tool in GPT chat · Only synthetic CSV data used · Claude Code was not actually run
Check that the existing int(row['amount']) code actually reproduces a ValueError for input '1,450'
Check that the exception string exactly matches "invalid literal for int() with base 10: '1,450'"
Check that the revised code runs on expenses_bad.csv without an exception
Check that stdout after the fix is exactly 2026-09 = 26600 and 2026-10 = 9800
Check that amount=abc actually raises the ValueError "line 3: amount must be an integer, got 'abc'"
Check that only the Python standard library is used
Verification limits
Python execution verification was performed with the Python tool in GPT chat and is not claimed to have been tested in Claude Code. The example parser handles only thousands-separator commas and does not generalize to currency symbols, decimals, or locale-specific numeric formats.
Instead of treating vibe coding as simply 'tell AI what you want and it builds everything for you,' this article frames it as a workflow in which a person defines the requirements and verification criteria, then checks the result produced by AI. Using a fictional household-expense CSV summation tool, it organizes the input, output, things not to do, and verification method into a one-page note.
Check the Claude Code installation command and account requirements for your operating system, then start a first session in a fictional practice folder. Instead of generating code immediately, this article focuses on asking only three questions in plan mode to inspect the folder and CSV, then exiting safely.
Use the fictional expenses.csv and requirements note from the previous articles to practice building a first small Python tool. Put the input example, expected output, and constraints in the request, then run the completed summarize.py separately in the Python tool in GPT chat—not in Claude Code—to verify the result.
Put the run command, data rules, prohibitions, and verification method that were being repeated in every request into the project's CLAUDE.md in a concise form. Distinguish the `/init` function that creates a draft from the purposes of different file locations, and complete a 35-line rules file for the expense CSV example.
Using the example of adding a `--by-category` option to an already working monthly expense summary tool, learn how to review a change plan first in Claude Code's plan mode. Fix the scope of edits and output format before implementation, then run the completed code after approval to verify both the existing monthly totals and the category totals.
Create a git baseline before Claude Code edits files, then read the actual changes with `git status` and `git diff`. You can ask AI to explain the diff, but do not rely on the explanation alone: review the diff and execution results yourself, then stage and commit the changes directly.
Assuming Claude Code can read and modify files and run commands, organize the safety checks beginners should make before, during, and after work. Connect the differences between permission modes, changes checkpoints cannot restore, the separate role of git, and the rule of not exposing secrets into one practical checklist.