Product case study · November 17, 2026

Ask Claude Code why the second request changes the first

Reproduce a Python mutable-default bug across two calls, give Claude Code an ownership contract, and test that request tags stay independent.

Reading time
6 min
Checked
Nov 17, 2026
Superbash Learn graphic reading Practical AI lessons without the hype fog on a warm cream background
Two calls should produce two independent lists of tags.
Bottom line

When a bug appears only on the second call, test the lifetime of the data before adding locks or resets.

The first request looks fine. Make a second one, then look back: the first request has somehow acquired the second request’s tag. You do not need threads, a database or a cache to create this bug. One empty list in a Python function signature is enough.

This is a good job for Claude Code because the fix is tiny but the requirement is easy to underspecify. “Stop sharing the default” solves only half the exercise. The function must also leave a caller’s own list alone.

You need Bash on macOS, Linux or WSL, Python 3, and an installed, signed-in Claude Code. The requests below are fictional dictionaries, not network traffic. The Python reproduction and reference repair were tested locally; the Claude prompt is an authored exercise, not a recorded agent run. Sources were checked November 17, 2026.

Make the second call betray the first

Create a disposable practice folder and a deliberately broken request builder:

request_lab=$(mktemp -d /tmp/claude-request-tags.XXXXXX)
cd "$request_lab"
cat > requests_demo.py <<'PY'
def make_request(name, tags=[]):
    tags.append(name)
    return {"name": name, "tags": tags}
PY
python3 - <<'PY'
from requests_demo import make_request
first = make_request("alpha")
print("Before:", first["tags"])
second = make_request("beta")
print("After:", first["tags"])
print("Same list:", first["tags"] is second["tags"])
PY

The local reproduction printed:

Before: ['alpha']
After: ['alpha', 'beta']
Same list: True

Python evaluates a default argument once when the function definition executes. Calls that omit tags reuse that default list. The two returned dictionaries are separate, but their tags entries point to the same object. The Python tutorial on default arguments documents this behavior and the usual None pattern.

I would investigate object lifetime before adding a lock here. These two calls already run one after the other. Making them take turns more carefully cannot give them separate lists.

Write down who owns the tags

Save this as SPEC.md in the practice folder:

make_request(name, tags=None) returns a dictionary with name and tags.
name is a string. tags is either None or a list of strings.
Omitting tags and passing None both mean no initial tags.
Return a fresh list containing the supplied tags, followed by name.
Preserve their order and any duplicates.
Do not change the supplied list.
Later calls must not alter earlier results.
Changing a returned list must not alter another result or the input list.
Use only the Python standard library.
Input validation, nested objects and HTTP requests are outside this lab.

The list-of-strings boundary keeps the copying requirement small. If tags later become dictionaries containing other lists, decide which nested objects must be independent before choosing a deeper copy.

Test a sequence, not just a single answer

Save this as test_requests_demo.py:

import unittest
from requests_demo import make_request


class RequestTests(unittest.TestCase):
    def test_repeated_omitted_tags(self):
        first = make_request("alpha")
        second = make_request("beta")
        self.assertEqual(first["tags"], ["alpha"])
        self.assertEqual(second["tags"], ["beta"])
        self.assertIsNot(first["tags"], second["tags"])

    def test_explicit_none(self):
        self.assertEqual(
            make_request("alpha", None),
            {"name": "alpha", "tags": ["alpha"]},
        )

    def test_supplied_list_is_unchanged(self):
        supplied = ["urgent", "urgent"]
        result = make_request("alpha", supplied)
        self.assertEqual(result["tags"], ["urgent", "urgent", "alpha"])
        self.assertEqual(supplied, ["urgent", "urgent"])
        self.assertIsNot(result["tags"], supplied)

    def test_empty_list_is_unchanged(self):
        supplied = []
        result = make_request("alpha", supplied)
        self.assertEqual(result["tags"], ["alpha"])
        self.assertEqual(supplied, [])
        self.assertIsNot(result["tags"], supplied)

    def test_result_mutation_stays_local(self):
        supplied = ["urgent"]
        first = make_request("alpha", supplied)
        second = make_request("beta", supplied)
        first["tags"].append("edited")
        self.assertEqual(second["tags"], ["urgent", "beta"])
        self.assertEqual(supplied, ["urgent"])

Run the checks before asking for a repair:

python3 -m unittest -v test_requests_demo

The baseline produced four failures and one error locally. Explicit None raises an error because the broken function tries to append to it; the other tests expose shared or modified lists. Python’s unittest reference documents the named-module command and the value and identity assertions used here.

Keep both calls inside the first test. Launching a fresh Python process for each request would recreate the function each time and hide the default-list leak.

Give Claude the contract and keep the tests fixed

Start claude in the practice folder and paste:

Read SPEC.md, requests_demo.py and test_requests_demo.py.
Run python3 -m unittest -v test_requests_demo and report the baseline.
Explain why the second call can change the first returned dictionary's tags.
Change only requests_demo.py to satisfy SPEC.md.
Do not change tests or expected values, install packages, commit or delegate.
Run the same test command after the repair and report its actual result.
Explain separately how your fix handles omitted tags and caller-owned tags.

Anthropic’s common workflows describes debugging with a specific reproduction and verifying the fix. Review the requested tool permissions for this practice folder; the prompt defines the job but does not create a filesystem sandbox.

A tempting partial repair is to change tags=[] to tags=None, create a list only when the argument is None, then keep appending directly to tags. That separates omitted arguments but still modifies any list the caller supplies. The last three tests are there to catch exactly that shortcut.

Inspect the ownership boundary

Compare the agent’s proposal with this reference repair:

def make_request(name, tags=None):
    result_tags = [] if tags is None else list(tags)
    result_tags.append(name)
    return {"name": name, "tags": result_tags}

The reference passed all five tests locally. The first line creates the list that this call owns, either empty or copied from the supplied strings. Only that new list receives the appended name. Nothing needs clearing after the function returns.

Exit Claude, read requests_demo.py, then rerun the tests yourself. If the omitted-argument test still fails, inspect the signature and any module-level list. If supplied-list tests fail, look for an append before the copy. If the agent proposes clearing the shared list, reject that repair: an earlier returned dictionary still points at it, so clearing can erase an earlier result.

The array-mutation lab covers a related caller-ownership mistake in a JavaScript sort. This exercise adds a different diagnostic habit: when an answer depends on what ran before it, keep multiple calls in the same test and inspect the earlier result again. A single successful call is a poor test of independence.

Sources

Checked November 17, 2026:

Put this to work

How a default argument can keep mutable state between function calls.

Try

Give Claude Code a small request builder and a fixed five-test contract.

Prove it worked

Run the same tests against the broken function and the repaired version.

Keep in view

  • Expose a shared default list by making two requests in one Python process.
  • Separate a fresh default from ownership of a caller-supplied list.
  • Check both returned values and whether earlier results change later.
Learn the workflow: Start a scoped Claude Code project session