CLAUDE LABJP
MODEL — Claude Opus 4.8 lands, improving coding, agentic, and reasoning over 4.7 at the same priceCODE — Opus 4.8's Fast mode runs at 2.5x speed and is now three times cheaper than earlier modelsCODE — Auto-mode command classification expands, with denial tracking and live bash path autocompleteENTERPRISE — Connector permissions in custom roles let admins control which tools each role can useTEAM — Tag Claude directly in Slack and hand off tasks while you focus elsewhereMCP — MCP servers now show startup auth notices, making connection status easier to trackMODEL — Claude Opus 4.8 lands, improving coding, agentic, and reasoning over 4.7 at the same priceCODE — Opus 4.8's Fast mode runs at 2.5x speed and is now three times cheaper than earlier modelsCODE — Auto-mode command classification expands, with denial tracking and live bash path autocompleteENTERPRISE — Connector permissions in custom roles let admins control which tools each role can useTEAM — Tag Claude directly in Slack and hand off tasks while you focus elsewhereMCP — MCP servers now show startup auth notices, making connection status easier to track
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 API93Code 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 $10 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-06-29
Let Claude Actually See the Images Your Tools Return — Use Image Blocks in tool_result and Cut Tokens by Roughly 10x
Stuffing a base64 string into a tool_result makes the same image cost roughly 10–20x more tokens. Here is how to return it as an image content block instead, with SDK code, a token-cost estimate, and the gotchas I hit in production.
API & SDK2026-06-28
Did That Post Actually Go Through? Safely Retrying an Interrupted MCP Write Without Double-Executing
When an MCP write tool call is interrupted by a dropped connection, you can't tell whether the server ran it. Here's why naive retries cause double-execution, and a working wrapper that uses idempotency keys and a reconcile read to retry safely — with examples from an unattended pipeline.
📚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
See all →