●MODEL — Claude Opus 4.8 lands, improving coding, agentic, and reasoning over 4.7 at the same price●CODE — Opus 4.8's Fast mode runs at 2.5x speed and is now three times cheaper than earlier models●CODE — Auto-mode command classification expands, with denial tracking and live bash path autocomplete●ENTERPRISE — Connector permissions in custom roles let admins control which tools each role can use●TEAM — Tag Claude directly in Slack and hand off tasks while you focus elsewhere●MCP — MCP servers now show startup auth notices, making connection status easier to track●MODEL — Claude Opus 4.8 lands, improving coding, agentic, and reasoning over 4.7 at the same price●CODE — Opus 4.8's Fast mode runs at 2.5x speed and is now three times cheaper than earlier models●CODE — Auto-mode command classification expands, with denial tracking and live bash path autocomplete●ENTERPRISE — Connector permissions in custom roles let admins control which tools each role can use●TEAM — Tag Claude directly in Slack and hand off tasks while you focus elsewhere●MCP — MCP servers now show startup auth notices, making connection status easier to track
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.
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 anthropicclient = anthropic.Anthropic() # The API key is read from the ANTHROPIC_API_KEY environment variable# Request 1: create a file and save it under /tmpresponse1 = 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 responsecontainer_id = response1.container.id# Request 2: reuse the same container and read the file we just wroteresponse2 = 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.
Item
Carried over when reusing the same container?
Files written to the workspace (/tmp, etc.)
Yes
Input files mounted via the Files API
Remain 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.
The billing reality: execution-time charges and the "attached files" trap
Before talking about reuse, it helps to internalize that Code Execution is billed separately from tokens. When you pair it with web search or web fetch there is no extra charge, but used on its own it is billed by execution time (the container's running time). There are a few traps here that cost you if you don't know them.
Aspect
Detail
Billing unit
Container execution time (separate from tokens)
Minimum charge
At least 5 minutes per request
Free allowance
1,550 hours per month per organization
Overage
$0.05 per hour, per container
Easy to miss
If a request includes files, execution time is billed even when the tool is never invoked, because files are preloaded onto the container
That last row matters most. The moment you attach an input file, execution time accrues for loading it onto the container, even if Claude never runs any code. A habit of "let me attach the CSV every time just in case" keeps generating cost even on lightweight questions where no code runs.
This is where container reuse pays off. Instead of re-attaching a large input file on every request, mount it once, leave the intermediate results in the workspace, and pass the container ID afterward to skip the attachment. Given that the minimum charge is in 5-minute units, finishing a sequence in one container beats standing up a fresh container for each tiny step. Once I limited input attachment to the first call, the execution-time line in my aggregation job's bill dropped noticeably.
Running a multi-step pipeline in one container
Abstract talk is hard to hold onto, so let me run "extract -> clean -> aggregate -> output" through a single container. Each stage leaves its artifact as a file, and the next stage reads it.
import anthropicclient = anthropic.Anthropic()TOOLS = [{"type": "code_execution_20250825", "name": "code_execution"}]MODEL = "claude-opus-4-8"def step(container_id, instruction): """Run one stage while reusing the same container; return the container ID.""" kwargs = dict(model=MODEL, max_tokens=4096, tools=TOOLS, messages=[{"role": "user", "content": instruction}]) if container_id: kwargs["container"] = container_id resp = client.messages.create(**kwargs) return resp.container.id, resp# Stage 1: load input, save the extracted result to an intermediate file (attach input only here)cid, _ = step(None, "Load the raw sales data, extract only the needed columns, and save to '/tmp/extracted.parquet'")# Stage 2: clean (read the previous intermediate file; do not re-attach the input)cid, _ = step(cid, "Read '/tmp/extracted.parquet', impute missing values and fix types, save to '/tmp/clean.parquet'")# Stage 3: aggregatecid, _ = step(cid, "Aggregate '/tmp/clean.parquet' by month and save to '/tmp/summary.csv'")# Stage 4: output (generate a chart)cid, final = step(cid, "From '/tmp/summary.csv', build a monthly line chart and save it as 'report.png'")print("pipeline container:", cid)
step() is a thin wrapper that takes a container_id and, if present, passes it into container=. From stage 2 on, no input data is attached, because extracted.parquet already lives inside the container. With this setup, each stage's instruction can focus only on "which file to read, which file to write," and Claude is less likely to get confused too.
The design tip is to always separate stage boundaries with files. If the intermediate result persists as a file, you can rebuild just one stage from its input file when you need to. Cram everything into a single request and you tend to restart from scratch whenever you hit the 90-second cell limit (more on that below).
Don't swallow container_expired
Reuse is convenient, but containers don't live forever. Containers expire 30 days after creation. Specify an expired container and the tool result returns the container_expired error code. For jobs left idle for a while, or batches you resume the following week, you have to plan for this.
What you must not do is swallow container_expired and proceed as if it "succeeded." Continue aggregating in a container that has no intermediate files, and an empty report quietly takes shape. In an unattended pipeline, the pattern I fear most is exactly this: no error raised, yet the wrong result emerges.
The defensive baseline is to never treat the container ID as an absolute precondition. Treat intermediate files as a cache, and make sure you can rebuild from source (the original input) if they have expired.
import anthropicclient = anthropic.Anthropic()TOOLS = [{"type": "code_execution_20250825", "name": "code_execution"}]MODEL = "claude-opus-4-8"def has_container_expired(resp): """Check whether the response contains a container_expired error.""" for block in resp.content: content = getattr(block, "content", None) err = getattr(content, "error_code", None) if err == "container_expired": return True return Falsedef run_summary(container_id, source_instruction, summary_instruction): # First, try to continue on the existing container if container_id: resp = client.messages.create( container=container_id, model=MODEL, max_tokens=4096, tools=TOOLS, messages=[{"role": "user", "content": summary_instruction}], ) if not has_container_expired(resp): return resp.container.id, resp # If expired, don't swallow it; fall through to a rebuild print("Detected container_expired. Rebuilding from source.") # No container, or it expired: rebuild from the original input resp = client.messages.create( model=MODEL, max_tokens=4096, tools=TOOLS, messages=[{"role": "user", "content": source_instruction}], ) return resp.container.id, resp
With this shape, expiry becomes a "cache miss" rather than an error. Thirty days sounds generous, but month-crossing operations and jobs that only run occasionally hit it routinely. The small effort of explicitly catching container_expired and branching is what prevents silent degradation.
Combine the 90-second cell limit with reuse
One more thing worth knowing: each cell has a 90-second wall-clock limit (exceeding it returns a detection_timeout-style result). Pack heavy work into one cell and you get cut off here.
Container reuse pairs nicely with this limit. Split heavy work into "fits in 90 seconds" units, write each step's result to a file, and keep going in the same container. For aggregating a large number of records, for example, you can save per-chunk partial aggregates as /tmp/partial_0001.parquet and merge them at the end. That kind of split works precisely because state persists as files inside the container.
# Conceptual example: split heavy aggregation into 90-second-safe units in the same containercid, _ = step(None, "Split a huge log into chunks of 500k rows, partially aggregate each chunk, and save to " "'/tmp/partial_<seq>.parquet'. You may proceed one chunk at a time")cid, _ = step(cid, "Load all of '/tmp/partial_*.parquet', merge them, and save the final result to '/tmp/final.parquet'")
Because the split partial aggregates remain inside the container, if one chunk stalls midway you can resume from the remaining partial_*.parquet. Not forcing everything into one request is, in the end, what makes the whole thing robust.
To try this, pick one job where you currently attach the input file on every request, and switch it to mount once and pass container.id thereafter. The surest check is the billing number: see how much the execution-time line changes. For me, the confidence of running an unattended pipeline shifted a notch once I added reuse and expiry handling. I hope it helps anyone automating multi-stage processing the same way.
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.