◉CLAUDE LABJP
●2.1.283 — No Claude Code release over the weekend; 2.1.283 from September 25 is still the latest. Its new auto-mode default deserves a check against your allow rules●PLANS — Pro and Team Standard now default to Opus instead of Sonnet, a change shipped in Claude Code 2.1.280 that shifts how you compare plans●10/07 — Nine days left until the old spellings of the Claude Desktop / Cowork managed config keys stop being accepted; after that they fail closed●AUTO — Users are asking why commands covered by allow rules get rejected in auto mode. The fix is shaping the invoked command to match the rule●NEW — The week Opus 5.5 became the default: three places an alias was still pointing at the old model●CURSOR — Cursor credits burn in proportion to model cost, so routing Claude through Cursor gets expensive fast. Above ~50 requests a day, flat-rate Claude Code Pro is the steadier choice●2.1.283 — No Claude Code release over the weekend; 2.1.283 from September 25 is still the latest. Its new auto-mode default deserves a check against your allow rules●PLANS — Pro and Team Standard now default to Opus instead of Sonnet, a change shipped in Claude Code 2.1.280 that shifts how you compare plans●10/07 — Nine days left until the old spellings of the Claude Desktop / Cowork managed config keys stop being accepted; after that they fail closed●AUTO — Users are asking why commands covered by allow rules get rejected in auto mode. The fix is shaping the invoked command to match the rule●NEW — The week Opus 5.5 became the default: three places an alias was still pointing at the old model●CURSOR — Cursor credits burn in proportion to model cost, so routing Claude through Cursor gets expensive fast. Above ~50 requests a day, flat-rate Claude Code Pro is the steadier choice
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 API123Code ExecutionContainersPipeline2Cost 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 $15 for lifetime access
View Membership →

Related Articles

⬡ API & SDK2026-06-20
Routing the effort Parameter Per Stage to Balance Claude's Output Cost and Latency
Claude's effort parameter governs all output tokens — thinking, prose, and tool calls. This guide replaces a blanket high setting with per-stage tiers and a dynamic router, grounded in measurements from a solo developer's automation pipeline.
⬡ API & SDK2026-09-28
The Night Claude Returned 529, My Fallback Failed Too — Strip cache_control at the Provider Boundary
My Claude API fallback threw a TypeError because content blocks still carried cache_control when they reached another vendor. Here is the stripping function, the pre-send guard, and the weekly forced-fallback routine that finally made the fallback a real safety net.
⬡ API & SDK2026-09-17
I Send Images Twenty at a Time — The Day the 21st Image Changed the Rules for the Other 61
Sending 62 images in one request failed with invalid_request_error. The cause was the image count, not the payload size. Here is how I recounted visual tokens as 28px patches and built batches that respect count, dimensions, and payload at once.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links