◉CLAUDE LABJP
●WATERMARK — Since 11/16, text from Opus 4.6, Sonnet 4.5 and other models carries a watermark; non-default temperature and similar settings may drop it●CC 2.1.325 — Claude Code is still on 2.1.325 (11/13), with no newer release yet●SONNET 4.5 — Retires on the Claude API on 11/30, 13 days left. Sweep model IDs in code and saved settings in one pass●AUTOMATION — A user reports recurring AI jobs set up without run counts or a spending cap, burning through a weekly allowance●CONNECTOR — If connectors vanish only in new sessions, compare the /mcp list against an older session first●NEW — Cowork Feels Worse? Run the Same Request in Three Fresh Sessions●WATERMARK — Since 11/16, text from Opus 4.6, Sonnet 4.5 and other models carries a watermark; non-default temperature and similar settings may drop it●CC 2.1.325 — Claude Code is still on 2.1.325 (11/13), with no newer release yet●SONNET 4.5 — Retires on the Claude API on 11/30, 13 days left. Sweep model IDs in code and saved settings in one pass●AUTOMATION — A user reports recurring AI jobs set up without run counts or a spending cap, burning through a weekly allowance●CONNECTOR — If connectors vanish only in new sessions, compare the /mcp list against an older session first●NEW — Cowork Feels Worse? Run the Same Request in Three Fresh Sessions
Articles/API & SDK
⬡ API & SDK/2026-06-29Advanced

Stop rebuilding intermediate files every request: reuse the Code Execution container to carry pipeline state

How to reuse the Code Execution container across requests by passing its container ID, so generated files and intermediate results carry over to the next step. Includes the execution-time billing trap and how to handle container_expired safely, with working code.

Claude API147Code ExecutionContainersPipelineCost optimization3

✦ Premium Article

The first time I handed a data aggregation job to the Code Execution tool, I was re-uploading the same CSV on every request. Extract, clean, aggregate, chart. Each time I split the work across requests, the intermediate file I had built in the previous call was simply gone, and I was starting over. As an indie developer who automates the numbers across several sites, I only noticed how much execution time this "start from zero every time" was quietly eating when I read the billing breakdown.

The cause was simple: a separate container was spinning up for each request. Code Execution containers do not share state across requests unless you explicitly reuse one via the container parameter. The flip side is encouraging: pass the container ID that a previous response returned into your next request, and the files you created and the data you already loaded all carry over. Today I want to walk through this "carry state across the container" design, the billing reality behind it, and the expiry handling you can't skip, all with working code.

Passing the container ID keeps your files alive into the next request

The mechanism itself is refreshingly plain. The response includes a container object, and if you pass its id into the next request's container parameter, the same workspace is reused.

import anthropic
 
client = anthropic.Anthropic()  # The API key is read from the ANTHROPIC_API_KEY environment variable
 
# Request 1: create a file and save it under /tmp
response1 = client.messages.create(
    model="claude-opus-4-8",
    max_tokens=4096,
    messages=[
        {
            "role": "user",
            "content": "Generate a random number and write it to '/tmp/seed.txt'",
        }
    ],
    tools=[{"type": "code_execution_20250825", "name": "code_execution"}],
)
 
# Pull the container ID out of the response
container_id = response1.container.id
 
# Request 2: reuse the same container and read the file we just wrote
response2 = client.messages.create(
    container=container_id,  # <- this alone carries the state forward
    model="claude-opus-4-8",
    max_tokens=4096,
    messages=[
        {
            "role": "user",
            "content": "Read the value from '/tmp/seed.txt' and compute its square",
        }
    ],
    tools=[{"type": "code_execution_20250825", "name": "code_execution"}],
)
 
print(response2)

The only meaningful part is holding onto response1.container.id and passing it into the second call's container=. Forget it, and the second request spins up a fresh container where /tmp/seed.txt becomes "no such file." That is exactly where I tripped the first time.

What carries over, and what disappears

"Reuse" sounds like "everything continues from where it left off," but it pays to separate what actually carries over from what doesn't.

Files in the workspace persist. Anything written to /tmp or the working directory stays visible as long as you specify the same container ID. That is the heart of reuse. The conversation context (the messages array), however, is a different thing. Reusing the container does not mean Claude remembers the previous exchange. The state only persists as files, so on the second request you have to restate which file holds what.

One more thing to watch is Python REPL variables (in-memory state). These are not carried over on the base tool version. If you need in-memory variables preserved, you need the newer tool version with REPL state persistence (code_execution_20260120 and later, available on Opus 4.5+ / Sonnet 4.5+). In practice, though, writing each step's result out to a file rather than hoarding variables makes for a design that survives expiry and re-runs far better. I deliberately drop every stage's output to a file.

ItemCarried over when reusing the same container?
Files written to the workspace (/tmp, etc.)Yes
Input files mounted via the Files APIRemain on the container (no remount needed)
Python REPL variables (in-memory state)Not on the base version (supported from 20260120)
Conversation context (the messages content)Not carried over (restate each time)
✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦A concrete implementation that reuses the container via container.id so generated files and preprocessed data carry into the next request
✦The easy-to-miss billing trap where attaching files bills execution time even when the tool isn't invoked, and how reuse trims that time
✦A defensive pattern that catches container_expired instead of swallowing it, rebuilding from source (accounting for the 30-day expiry and 90-second cell limit)
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

⬡ API & SDK2026-11-17
If You Rely on Claude's Text Watermark, Check temperature in the Request Body, Not Your Code
Since November 16, text from Opus 4.6, Opus 4.5, Sonnet 4.5, and Haiku 4.5 carries Anthropic's text watermark, but non-default temperature, top_p, or top_k may drop it. Here is how I log the request body once and decide.
⬡ API & SDK2026-11-09
Does an Undated claude-sonnet-4-5 Escape the November 30 Retirement?
The retirement table lists only the dated Sonnet 4.5 ID. How I read the docs on the undated alias, found the lines that really send requests, chose between Sonnet 4.6 and 5.5, and sorted out the 400s after the swap.
⬡ API & SDK2026-11-07
Your Key Is Valid but You Get a 400 or 401 — Check the Workspace Header and Browser Calls Before Reissuing
When a Claude API key is valid but requests still fail with 400 or 401, two causes are worth ruling out before you create a new key: a missing workspace ID header and a direct call from the browser. A triage table and a small script you can run are included.