The code-interpreter-v1 template provides a securely isolated code execution sandbox environment. It supports secure execution of Python, JavaScript, and other languages in the cloud, with context persistence across calls (variables, imports, and functions can be referenced across calls).
The code-interpreter-v1 template aligns with E2B Code Interpreter's code context and code execution capabilities, and can be accessed directly through the E2B Code Interpreter SDK.
Features
| Feature | Description |
| Multi-language code execution | Supports Python and JavaScript through run_code/runCode |
| Context persistence | Variables, imports, and functions are retained across calls in the default context |
| Code context management | Supports creating, restarting, and deleting independent code contexts, each with isolated variable state |
| File system operations | Supports uploading, downloading, reading, and writing files, creating directories, moving, and deleting, covering both text and binary files |
| Terminal command execution | Supports synchronous command execution and interactive terminals (PTY) |
| Secure isolation | Based on function instance isolation; each sandbox instance has its own independent file system and process space |
Use cases
| Use case | Description |
| AI Agent code sandbox | Provides a secure code execution environment for AI Agents, preventing untrusted code from accessing or tampering with host system resources |
| Data analysis | Runs Python data analysis scripts in the sandbox, using libraries such as pandas and numpy |
| File processing | Uploads files to the sandbox for format conversion, data cleaning, and other operations, then downloads the results |
| Script execution and automation | Executes Shell commands, installs dependencies, and runs automation scripts |
Default configuration
The default configuration for the code-interpreter-v1 template is as follows:
| Configuration | Default | Description |
| Default port | 5000 | Sandbox service listening port |
| CPU | 2 vCPU | Minimum requirement |
| Memory | 2048 MB | Minimum requirement |
| Disk size | 10240 MB | — |
SDK usage
When using the code-interpreter-v1 template, whether you need to explicitly specify template depends on the SDK:
| SDK | template parameter | Description |
e2b_code_interpreter SDK | Not required | The dedicated SDK creates code-interpreter-v1 sandboxes by default |
e2b SDK | Specify code-interpreter-v1 | The general SDK creates base sandboxes by default; you need to explicitly select the code-interpreter-v1 template |
Create a sandbox and execute code
Use the e2b_code_interpreter SDK to create a sandbox and execute code through run_code:
from e2b_code_interpreter import Sandbox
sbx = Sandbox.create()
try:
execution = sbx.run_code("print('hello from code interpreter')")
print("".join(execution.logs.stdout))
finally:
sbx.kill()TypeScript example:
npm init -y
npm install @e2b/code-interpreter@^2.6.1 tsximport { Sandbox } from "@e2b/code-interpreter";
const sbx = await Sandbox.create();
try {
const execution = await sbx.runCode("print('hello from code interpreter')");
console.log(execution.logs.stdout.join(""));
} finally {
await sbx.kill();
}logs.stdout and logs.stderr are both lists of strings, and need to be joined with "".join(...) (Python) or .join("") (TypeScript) to form complete text.
Main parameters of run_code / runCode:
| Parameter | Description |
code | The code to execute |
language | Execution language; supports python and javascript; defaults to python if not specified; mutually exclusive with context |
context | Specifies which code context to execute in; mutually exclusive with language |
timeout / timeoutMs | Code execution timeout (Python in seconds, default 300 seconds; TypeScript in milliseconds, default 60000 milliseconds) |
envs | Custom environment variables |
on_stdout / onStdout, etc. | Streaming callbacks, receiving stdout/stderr/results/errors line by line |
The Execution result contains logs (stdout/stderr lists), results (final expression results with text representation), error (execution exceptions), and an execution count (execution_count in Python and executionCount in TypeScript).
Context persistence
In the default context of the same sandbox, variables, imports, and functions are retained across calls:
sbx = Sandbox.create(**kwargs)
try:
sbx.run_code("x = 42")
execution = sbx.run_code("print(x)")
print("".join(execution.logs.stdout)) # 42
finally:
sbx.kill()Code context management
Each code context (Context) has independent variable state. Route code execution to a specified context through the context parameter; when context is not specified, the default context is used. Context management is currently provided by the Python SDK:
| Operation | Python SDK method |
| Create context | create_code_context(cwd="/home/user", language="python") |
| Restart context | restart_code_context(context) |
| Delete context | remove_code_context(context) |
| Execute in specified context | run_code(code, context=ctx) |
The context parameter of run_code must pass a Context object; restart_code_context / remove_code_context accept a Context object or a context ID string. create_code_context requires language (python or javascript), and cwd defaults to /home/user.
from e2b_code_interpreter import Sandbox
sbx = Sandbox.create(**kwargs)
try:
# Create an independent context
ctx = sbx.create_code_context(language="python", cwd="/home/user")
sbx.run_code("y = 100", context=ctx)
execution = sbx.run_code("print(y)", context=ctx)
print("".join(execution.logs.stdout)) # 100
# Variables in ctx are not accessible in the default context, execution.error contains NameError
default_execution = sbx.run_code("print(y)")
# Variables are cleared after restarting the context, execution.error contains NameError
sbx.restart_code_context(ctx)
restarted_execution = sbx.run_code("print(y)", context=ctx)
# Delete the context
sbx.remove_code_context(ctx)
finally:
sbx.kill()The runCode method of the TypeScript SDK executes code normally, but its context management methods (createCodeContext / listCodeContexts / restartCodeContext / removeCodeContext) are currently unavailable on this platform; use the Python SDK to manage independent contexts.
Multi-language execution
Specify the execution language through the language parameter, defaulting to python:
sbx = Sandbox.create(**kwargs)
try:
execution = sbx.run_code("console.log('hello js')", language="javascript")
print("".join(execution.logs.stdout))
finally:
sbx.kill()const sbx = await Sandbox.create();
try {
const execution = await sbx.runCode("console.log('hello js')", { language: "javascript" });
console.log(execution.logs.stdout.join(""));
} finally {
await sbx.kill();
}Using the general e2b SDK
When using the e2b SDK, you need to explicitly specify the template code-interpreter-v1. At this point, you can use the sandbox's basic capabilities (files, commands, processes), but it does not include run_code:
from e2b import Sandbox
sbx = Sandbox.create(template="code-interpreter-v1")
try:
result = sbx.commands.run("python --version")
print(result.stdout)
finally:
sbx.kill()Usage workflow
Create a sandbox instance: Use the
e2b_code_interpreterSDK to callSandbox.create(), which automatically selects thecode-interpreter-v1template.Execute code: Execute code through
run_code/runCode; the default context automatically maintains variable state.Isolated execution: If you need to isolate variable state, use
create_code_contextto create an independent context, and route execution through thecontextparameter.Clean up resources: Call
kill()to release the sandbox when finished; contexts that are no longer needed can be deleted withremove_code_context.
Sandbox instance states
A sandbox instance goes through the following states during its lifecycle:
| State | Description |
running | Ready and available for use |
paused | Paused (deep sleep), can be resumed |
terminated | Terminated |
Usage limits
| Limit | Constraint |
| Sandbox lifecycle | Single sandbox instance maximum lifecycle is 24 hours (the timeout parameter upper limit is 86400 seconds) |
| Idle timeout | Can be set through the sandboxIdleTimeoutSeconds parameter, with a lower bound of 60 seconds |
| Code execution timeout | Single run_code / runCode synchronous execution default timeout is Python 300 seconds; TS 60000 milliseconds, adjustable through timeout / timeoutMs |