Code interpreter in an LLM sandbox: Files, safety, and cost

Run AI-generated analysis in an LLM sandbox. Choose an interpreter, control files and credentials, handle cancellation, and compare session and infrastructure costs.

β€’

Published on

β€’

Code interpreter in an LLM sandbox: Files, safety, and cost
Do not index
Do not index
A code interpreter lets an AI application execute generated code, inspect results, and return files such as reports or charts. An LLM sandbox supplies the environment around that execution. Agent37 provides standard gVisor sandboxes and full VMs for developers who want to build this workflow with their own model and application.
The useful output is a file your customer can trust and retrieve. Getting there requires choices about dependencies, credentials, cancellation, retention, and cost. A Python tool alone does not make those choices for you.
This guide follows a sales CSV becoming a monthly report. The design is illustrative, not a deployed benchmark. Product documentation and prices were checked on November 15, 2026.

What a code interpreter adds to an LLM sandbox

The model proposes a program. The interpreter runs it. Your application decides which inputs it can read, how long it may run, and which results reach the customer.
For a sales report, Python might parse dates, group amounts by month, and write a summary CSV. Computation makes the arithmetic inspectable, but the program can still choose the wrong columns, ignore refunds, or combine currencies incorrectly. Execution needs independent checks.
Keep the coordinator outside the worker. It owns customer authentication, model calls, job records, and delivery. Give the worker only the selected input files and dependencies needed for the calculation. Store the customer-to-job-to-sandbox mapping where generated code cannot rewrite it.
Component
Responsibility
Allowed input
Application
Authorize and track jobs
Customer and upload IDs
Model
Propose code
Task, schema, sample
Worker
Execute the program
Approved files
Result handler
Validate and deliver
Expected artifacts
The name also appears in the open-source llm-sandbox package, which supports Docker, Kubernetes, and Podman backends. That library is one implementation choice. The responsibilities above apply whether you use it, a hosted interpreter, or a remote execution API.

Choose a built-in tool or a sandbox you control

Start with the amount of runtime control your application needs. These are different integration paths, so choose against a representative job rather than a feature count.
OpenAI Code Interpreter integrates Python execution into the Responses API. You can request an automatically managed container or supply one you created explicitly. Its documented memory choices are 1, 4, 16, and 64 GB. Generated file annotations identify artifacts your application can download. See the Code Interpreter guide.
Treat those containers as temporary: after 20 minutes without use they expire, and their files and in-memory state become unrecoverable. Export required artifacts while the container is active. The API guide recommends keeping durable data in your own systems.
E2B Code Interpreter gives your application an execution SDK. Its quickstart uses Sandbox.create() and run_code() in Python, with shell commands and file operations available through the same sandbox. This fits an application that supplies the generated code itself; the interpreter SDK does not replace your model orchestration or authorization.
Amazon Bedrock AgentCore Code Interpreter provides managed execution for Python, JavaScript, and TypeScript. Its resource management documentation distinguishes Sandbox, Public, and VPC networking. Sandbox mode still permits selected AWS access, including S3. Choose permissions and networking explicitly rather than reading β€œsandbox” as β€œoffline.” See the tool overview.
Agent37 supplies the machine, execution, files, and lifecycle APIs. You build the interpreter interface and model loop on top. This is useful when you want your own dependencies and persistent workspace; it also leaves more integration work in your application. Start with the instance API. For a framework-specific path, our LangChain sandbox guide covers that integration separately.

Move files through an explicit execution contract

Before generating code, decide what one job is allowed to do. For the example report, require a CSV with known date, currency, and amount columns. Set upload and expanded-data limits, an execution deadline, expected outputs, and a retention period.
These are application decisions, not Agent37 defaults. Record them with the job so a retry cannot quietly enlarge its access. Reject a missing currency column as an input error instead of inviting repeated model attempts to invent an exchange rate.
Validate the upload before starting the worker. Check encoding and structure, then remove unrelated columns where possible. Often the model needs a schema and a synthetic sample to write the transformation, while only the worker needs the actual rows. This can reduce unnecessary data exposure and prompt size.
Prepare reviewed dependencies in a template or setup step. Pin the environment you have tested. Letting each generated script install arbitrary packages while holding customer data makes both reproducibility and access control harder.
Upload the input and generated script as separate files. Use backend-controlled paths. Do not splice a customer's filename or generated program into a shell command without deliberate quoting and validation.
Agent37's file API writes raw bytes through PUT /v1/files/content and downloads them through GET /v1/files/content. Keep provisioning and access credentials in your backend. File transfer and command execution are separate operations.
Record an input checksum, script checksum, runtime version, and expected artifact names. After execution, collect only those outputs. Mark the job delivered when the authorized customer can retrieve its validated files, not merely when Python exits successfully.

Restrict credentials and network access before execution

Assume the generated program can inspect files and environment variables available to its process. An API token in an environment variable remains a token the program can use.
For an offline report, let the trusted backend fetch an authorized export from your database. Give the worker that export instead of a production database password. Keep sending email, changing records, and other external actions behind separate application checks.
Uploaded content can influence generated code. Isolation limits where that code runs; it does not turn instructions hidden in data into trustworthy directions. Keep permission decisions in backend code that the model cannot modify.
Agent37 allows outbound internet access by default. For this calculation, prepare dependencies and then set network: "deny-all" before supplying customer data and running the program. The network reference says a successful 200 confirms enforcement. If the update returns 503 try_again, repeat it to confirm enforcement before starting the job.
Under deny-all, the worker cannot reach managed model and tool services either. Keep model calls in the external coordinator for this pattern. The backend can still use authenticated file and execution interfaces. If a task needs outside data, define the narrow access it requires and test that policy.

Choose the runtime around the calculation

A reporting job needs Python, compatible libraries, sufficient memory, and an appropriate execution boundary. It usually does not need a Docker daemon inside the worker.
gVisor interposes an application kernel between the workload and the host. Its architecture differs from an ordinary shared-kernel container and from a full VM. Compatibility should be tested with your dependencies and representative input sizes.
Agent37 standard gVisor instances cannot run a Docker daemon. Its full VMs have their own Linux kernel and support Docker workloads at a separate compute rate. If you host the llm-sandbox package's Docker backend inside an Agent37 instance, choose a VM. See the VM reference.
The Ubuntu VM template includes Docker, but its entrypoint is not a systemd init environment. Put durable files on the billed disk and verify your startup process. Runtime type is fixed for an instance; changing it means creating another instance.
Memory can be the first constraint in analytics. A compressed upload may expand greatly during parsing, joins, and chart generation. Test the largest input you intend to accept, then enforce that limit before execution. Our microVM versus container guide explains the broader isolation decision.

Cancel jobs and validate outputs deliberately

A lost HTTP response does not prove a calculation stopped. Blindly retrying may start another copy, doubling work or producing conflicting files.
Agent37's exec reference distinguishes platform errors from command results. A command can exit nonzero inside an HTTP 200, so inspect exit_code. Long-running commands can outlast the request's waiting limit. Use a supervisor that can terminate the process and its children, plus an external watchdog for abandoned jobs.
Track whether a launch failed, a process is still running, a calculation failed, or a completed response was lost. Reconcile that state before retrying. Bound model repair attempts too: one invalid column should not trigger unlimited model calls and overlapping calculations.
Treat output as untrusted. Validate CSV columns, types, row counts, date ranges, and currency handling. Independently reconcile a control total. A plausible chart can conceal omitted refunds even when the program reports success.
Allow only expected artifact paths and sizes. Check actual file types and handle links and traversal deliberately. Never accept an arbitrary path printed by generated code as permission to download it.
For charts, a validated PNG is easier to deliver safely than active HTML. Agent37's file guidance recommends isolated rendering or attachment downloads for untrusted active content. Do not insert generated HTML or SVG into your application's origin as trusted markup.
Reuse a worker only within a deliberate ownership boundary. For unrelated customer jobs, a fresh environment is a useful starting point. A new directory in an existing process is not independent isolation. Preserve evidence needed for diagnosis, export approved artifacts, and clean up the job environment.

Budget sessions, compute, storage, and model calls

Interpreter pricing uses different meters. Compare your session pattern and retention needs, then add model tokens, repair attempts, and external tools separately.
OpenAI's published container rates are $0.03 for 1 GB, $0.12 for 4 GB, $0.48 for 16 GB, and $1.92 for 64 GB, per 20-minute session per container. For example, 100 separate 4 GB sessions, each consuming one billed session, cost $12 in container charges before model usage. The billing interval and inactivity expiry are separate concepts.
Agent37 bills allocated compute while running and disk while retained. Standard rates are $0.80 per vCPU and $0.70 per GB RAM per 730 running hours; disk is $0.09 per GB per 730 hours. Full VM compute is 2.5 times standard compute. See pricing and billing.
For one instance with 2 vCPU, 4 GB RAM, and 4 GB disk, those rates give:
Usage and retention
Standard gVisor
Full VM
60 hours, then delete
$0.39
$0.93
60 hours, disk kept 730 hours
$0.72
$1.26
Runs for all 730 hours
$4.76
$11.36
The middle row includes 60 hours of compute plus $0.36 for retaining 4 GB of disk all month. These are rounded infrastructure calculations, not subscriptions, throughput benchmarks, or a claim that different providers supply identical hardware. The Ubuntu VM default uses 12 GB disk, making that default 2 vCPU/4 GB configuration $12.08 for 730 running hours.
Awake idle time still bills compute. Stop or sleep pauses compute, while retained disk continues billing; deletion ends the instance meter. Auto-sleep must be enabled. It can restore memory but can also cold-boot, so save important state to disk and review auto-sleep behavior for long jobs.
Choose with a measured workload: successful reports, elapsed runtime, peak memory, failures, and retained storage. Low infrastructure rates help only if the selected runtime handles your actual jobs.

Code interpreter questions

Does the language model run in the sandbox?

It can, but this design keeps model requests in the application and executes generated programs in the worker. That keeps model credentials outside the untrusted process and permits offline calculation.

Can a code interpreter guarantee a correct answer?

No. It executes the program. Your application must validate inputs, assumptions, and artifacts, and reject incomplete results. Successful execution is not proof that a chart answers the customer's question.

Should every analysis keep its environment?

Keep a workspace only when continuity is useful and ownership is clear. Export durable artifacts before temporary sessions expire. For ongoing workers, choose a retention policy and account for disk charges while stopped.

What should we build first?

Start with one input format and one output type. Test authorization, cancellation, validation, and cleanup with representative data. Then explore Agent37 sandboxes or create a sandbox for the runtime your job needs.
Vishnu

Written by

Vishnu

Founder at Agent37, which runs managed hosting for OpenClaw and Hermes agents for 1,000+ users. Writes about what actually breaks when you leave an AI agent running.