All Products
Search
Document Center

Function Compute:Code interpreter v1 template

Last Updated:Sep 14, 2026

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

FeatureDescription
Multi-language code executionSupports Python and JavaScript through run_code/runCode
Context persistenceVariables, imports, and functions are retained across calls in the default context
Code context managementSupports creating, restarting, and deleting independent code contexts, each with isolated variable state
File system operationsSupports uploading, downloading, reading, and writing files, creating directories, moving, and deleting, covering both text and binary files
Terminal command executionSupports synchronous command execution and interactive terminals (PTY)
Secure isolationBased on function instance isolation; each sandbox instance has its own independent file system and process space

Use cases

Use caseDescription
AI Agent code sandboxProvides a secure code execution environment for AI Agents, preventing untrusted code from accessing or tampering with host system resources
Data analysisRuns Python data analysis scripts in the sandbox, using libraries such as pandas and numpy
File processingUploads files to the sandbox for format conversion, data cleaning, and other operations, then downloads the results
Script execution and automationExecutes Shell commands, installs dependencies, and runs automation scripts

Default configuration

The default configuration for the code-interpreter-v1 template is as follows:

ConfigurationDefaultDescription
Default port5000Sandbox service listening port
CPU2 vCPUMinimum requirement
Memory2048 MBMinimum requirement
Disk size10240 MB—

SDK usage

When using the code-interpreter-v1 template, whether you need to explicitly specify template depends on the SDK:

SDKtemplate parameterDescription
e2b_code_interpreter SDKNot requiredThe dedicated SDK creates code-interpreter-v1 sandboxes by default
e2b SDKSpecify code-interpreter-v1The 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 tsx
import { 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:

ParameterDescription
codeThe code to execute
languageExecution language; supports python and javascript; defaults to python if not specified; mutually exclusive with context
contextSpecifies which code context to execute in; mutually exclusive with language
timeout / timeoutMsCode execution timeout (Python in seconds, default 300 seconds; TypeScript in milliseconds, default 60000 milliseconds)
envsCustom 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:

OperationPython SDK method
Create contextcreate_code_context(cwd="/home/user", language="python")
Restart contextrestart_code_context(context)
Delete contextremove_code_context(context)
Execute in specified contextrun_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

  1. Create a sandbox instance: Use the e2b_code_interpreter SDK to call Sandbox.create(), which automatically selects the code-interpreter-v1 template.

  2. Execute code: Execute code through run_code / runCode; the default context automatically maintains variable state.

  3. Isolated execution: If you need to isolate variable state, use create_code_context to create an independent context, and route execution through the context parameter.

  4. Clean up resources: Call kill() to release the sandbox when finished; contexts that are no longer needed can be deleted with remove_code_context.

Sandbox instance states

A sandbox instance goes through the following states during its lifecycle:

StateDescription
runningReady and available for use
pausedPaused (deep sleep), can be resumed
terminatedTerminated

Usage limits

LimitConstraint
Sandbox lifecycleSingle sandbox instance maximum lifecycle is 24 hours (the timeout parameter upper limit is 86400 seconds)
Idle timeoutCan be set through the sandboxIdleTimeoutSeconds parameter, with a lower bound of 60 seconds
Code execution timeoutSingle run_code / runCode synchronous execution default timeout is Python 300 seconds; TS 60000 milliseconds, adjustable through timeout / timeoutMs