Product case study · November 30, 2026
Ask Claude Code to find the shortages Counter subtraction hides
Build a stock reconciliation bug, ask Claude Code for a scoped fix, and test that Python Counter preserves shortages as well as surpluses.

A reconciliation report should keep the bad news. Define the sign convention before asking an agent to repair its arithmetic.
The stock report finds two extra cables and says nothing about the three missing adapters. Its arithmetic looks reasonable: counted stock minus expected stock. The surprise is that Python’s Counter subtraction deliberately discards negative results.
That makes a useful coding-agent exercise. The fix is small, but “make the tests pass” is a poor brief until you decide what a negative number means and whether unchanged products belong in the report. Here, positive means surplus, negative means shortage, and zero means omit the product.
Use Python 3.10 or newer, Bash on Linux, macOS or WSL, and an installed, signed-in Claude Code. The fixture and reference repair below were tested with Python 3.14.4. The Claude prompt is an authored exercise, not a recorded agent run. Official sources were checked November 30, 2026.
Make the missing stock disappear
Create a disposable folder containing synthetic data only:
counter_lab=$(mktemp -d /tmp/claude-counter.XXXXXX)
cd "$counter_lab"
cat > reconcile.py <<'PY'
from collections import Counter
def stock_delta(expected, counted):
return dict(Counter(counted) - Counter(expected))
PY
python3 - <<'PY'
from reconcile import stock_delta
print(stock_delta(
{"cable": 4, "adapter": 5},
{"cable": 6, "adapter": 2},
))
PY
The output is {'cable': 2}. Our report needs {'cable': 2, 'adapter': -3}. Both inputs are mappings from product names to quantities; they are not lists of individual stock items.
Python’s Counter documentation describes subtraction as a multiset operation: the result keeps only positive counts. That behavior suits a question like “which items remain?” It loses information when the question is “what changed in either direction?”
I would settle that distinction before letting an agent edit the function. Otherwise, it can produce perfectly tidy code for the wrong report.
Give the report six concrete obligations
Save this as test_reconcile.py in the same folder:
import unittest
from reconcile import stock_delta
class StockDeltaTests(unittest.TestCase):
def test_mixed_surplus_and_shortage(self):
self.assertEqual(
stock_delta({"cable": 4, "adapter": 5},
{"cable": 6, "adapter": 2}),
{"cable": 2, "adapter": -3},
)
def test_expected_only_product_is_a_shortage(self):
self.assertEqual(stock_delta({"adapter": 5}, {}),
{"adapter": -5})
def test_counted_only_product_is_a_surplus(self):
self.assertEqual(stock_delta({}, {"cable": 2}),
{"cable": 2})
def test_equal_and_explicit_zero_counts_are_omitted(self):
self.assertEqual(
stock_delta({"cable": 4, "adapter": 0}, {"cable": 4}),
{},
)
def test_empty_inputs(self):
self.assertEqual(stock_delta({}, {}), {})
def test_inputs_are_unchanged(self):
expected = {"cable": 4, "adapter": 5}
counted = {"cable": 6, "adapter": 2}
before_expected = expected.copy()
before_counted = counted.copy()
stock_delta(expected, counted)
self.assertEqual(expected, before_expected)
self.assertEqual(counted, before_counted)
Run the tests:
python3 -m unittest -v test_reconcile
The baseline has two failures and four passes. The mixed-stock case loses the adapter deficit; the expected-only case returns an empty dictionary. Keep those failures visible. They explain more than a vague instruction to “fix inventory.”
The other tests protect useful behavior during the repair: a newly counted product remains a surplus, zeros stay absent, empty inputs work, and the caller’s dictionaries stay unchanged. Python’s unittest guide documents the command-line runner and assertEqual used here. Comparing ordinary dictionaries also checks exactly which keys the report returns.
Ask Claude Code to preserve the bad news
Start claude from the practice folder. The Claude Code project lesson covers setting up a scoped session if you need it.
Paste this task:
Read reconcile.py and test_reconcile.py. Run the six tests and report
which fail before editing anything.
Repair stock_delta. Inputs are dictionaries mapping product names to
nonnegative integer quantities. Return an ordinary dictionary of
counted minus expected for every product present in either input.
Keep positive and negative differences; omit exactly zero differences.
Treat a missing product as quantity zero. Do not mutate either input.
Change only reconcile.py. Keep the function name and arguments.
Do not edit tests, install packages, access inventory services,
commit files or delegate. Use the Python standard library.
Run the unchanged tests after editing. Explain why the original
Counter subtraction lost shortages and show the final function.
This uses the investigate, repair and verify pattern in Anthropic’s common workflows. Review the session’s tool permissions as usual; a sentence limiting file edits is a task instruction, not an operating-system access boundary.
The input contract is deliberately narrow. This lab does not ask the agent to invent a policy for fractional quantities, invalid strings or negative stock snapshots. Those would need separate requirements.
Use subtraction that retains signed counts
A reference repair is:
from collections import Counter
def stock_delta(expected, counted):
delta = Counter(counted)
delta.subtract(expected)
return {product: amount for product, amount in delta.items()
if amount != 0}
subtract() updates the new counter while retaining zero and negative values. The final dictionary comprehension removes only zeros. Constructing delta from the counted mapping leaves the original mapping untouched. These behaviors follow the documented Counter methods.
Do not “clean up” the intermediate result with +delta or replace the method call with delta -= Counter(expected): Counter’s multiset operations discard nonpositive results. The negative values are the point of this report.
An explicit loop over the union of the input keys is also a reasonable repair. Prefer whichever version your team finds easiest to read; the contract and tests should survive that choice.
Check the patch outside the conversation
Exit Claude and rerun:
python3 -m unittest -v test_reconcile
The reference implementation passed all six tests locally. Open both files to confirm that the tests still contain their original expectations and that only the implementation changed. A green run is much less persuasive if the agent has removed the shortage assertion.
If shortages still vanish, look for a positive-only filter or another Counter arithmetic operation after subtract(). If the signs are reversed, check the operand order: counted minus expected. If zero entries remain, inspect the final filter. If the input-preservation test fails, check whether the patch modifies a caller-owned dictionary.
There is one business assumption to revisit before adapting this to real stock data: missing means zero only because this exercise says so. An incomplete warehouse export may mean “not observed.” Decide whether both snapshots are complete before turning absent rows into shortages. The function can preserve every negative number and still answer the wrong business question if the input contract is wrong.
Sources
Official documentation checked November 30, 2026:
- Python: collections.Counter — signed counts, subtraction,
subtract()and multiset filtering. - Python: unittest — command-line test execution and equality assertions.
- Anthropic: Common workflows — focused debugging tasks and verification in Claude Code.
Put this to work
Why Counter subtraction and Counter.subtract serve different purposes.
Try
Repair a two-file Python inventory lab using fixed acceptance tests.
Prove it worked
Run six unchanged tests before and after the repair and inspect the returned dictionaries.
Keep in view
- Reproduce a stock report that drops negative differences without raising an error.
- Ask Claude Code to preserve signed differences without changing the inputs.
- Verify shortages, surpluses, unchanged stock and products present on only one side.