You need to enable JavaScript to run this app.
Volcengine Kubernetes Engine

Volcengine Kubernetes Engine

Copy page
Download PDF
E2B-based management
Getting started
Copy page
Download PDF
Getting started
This topic demonstrates the complete workflow for creating an AI Agent instance and persisting data by using the Python SDK based on the minimum viable product (MVP) principle. This example workflow includes common operations, such as creating templates, creating Sandboxes, reading/writing files, running code and commands, pausing/resuming Sandboxes, and querying status.​
Preparation​
  1. Configure E2B-related parameters in the YAML file for the VCI Agent Sandbox controller.​
  • ​
    VCI Agent Sandbox not installed
    VCI Agent Sandbox installed
    ​
    Follow the steps in the installation guide to install VCI Agent Sandbox. During the installation process, set enableE2BServer to true in the values.yaml file. Configure the remaining parameters as needed.​
    warning​
    When installing VCI Agent Sandbox, select v0.1.1-vke.3 or later for the Chart version of the agent-sandbox application template. Earlier versions do not have built-in E2B parameters.​
    ​
  1. Install the E2B Python SDK.​
  • ​
    pip install e2b-code-interpreter==2.4.1 e2b==2.10.2 anthropic python-dotenv​
    ​
  1. Ensure that you have an available E2B API Key and configure the address and key (such as API_BASE_URL and ADMIN_API_KEY) required to access the E2B API in your environment. By default, the E2B API Key is saved in the e2b-key-store Secret in the agent-sandbox-system namespace. For details, see the installation guide. You can also obtain the API Key by calling the API via the SDK. For details, see Managing API Keys.​
  1. Expose the E2B service externally via an ALB Ingress. For details, see Exposing the E2B service via an Ingress.​
Procedure​
Step 1: Create a template​
A template defines Sandbox startup parameters. You can create a template in your cluster via the Kubernetes API. Alternatively, you can create a template via the VCI SDK. For details, see Managing templates.​
tip​
  • In the sample code below, replace the image, resource, and annotation settings with your actual information. For other parameters in the code, you can keep the existing settings.​
  • After the template is created, obtain the template ID for use in subsequent Sandbox instance creation.​
​
apiVersion: extensions.agents.x-k8s.io/v1alpha1​
kind: SandboxTemplate​
metadata:​
name: code-interpreter-template​
namespace: default​
spec:​
podTemplate:​
metadata:​
annotations:​
agent.sandbox.io/enable-envd-injection: "true" # Inject envd into the primary container.​
vci.vke.volcengine.com/enable-auto-create-imc: "true" # Enable automatic image caching.​
vci.vke.volcengine.com/preferred-instance-family: vci.u1,vci.n3i # The VCI specification.​
vci.volcengine.com/persistent-container: "true" # Specify whether to enable persistence for the container writable layer data. Valid values: true (enable) and false (disable).​
vci.volcengine.com/tls-enable: "false" # Enable log collection to collect logs to TLS.​
vci.volcengine.com/usable-disk-capacity: "40" # Specify the available space of the system volume (writable layer space).​
vke.volcengine.com/burst-to-vci: enforce # Enforce VCI-based deployment.​
spec:​
containers:​
- image: e2bdev/code-interpreter:latest # Use the executable code image provided by E2B.​
name: main​
resources:​
limits:​
cpu: "2"​
memory: 4Gi​
requests:​
cpu: "2"​
memory: 4Gi​
terminationGracePeriodSeconds: 1​
​
Step 2: Create a Sandbox​
Use the Python SDK to create a Sandbox instance with the template ID obtained in the previous step.​
​
from e2b_code_interpreter import Sandbox​
​
sbx = Sandbox.create(​
template="default--code-interpreter-template",​
secure=True,​
timeout=600,​
metadata={"scene": "quickstart"}​
)​
​
print(f"sandbox id: {sbx.sandbox_id}")​
​
The following table describes the parameters.​
​
Parameter​
Description​
template​
The template ID of the SandboxTemplate created in the previous step.​
Description​
  • When you create a SandboxTemplate through the Kubernetes API, the Template ID is <metadata.namespace value--metadata.name value>, such as default--code-interpreter-template.​
  • When you create a SandboxTemplate through the VCI SDK, the Template ID is the returned value of the template_id parameter.​
secure​
Specifies whether to enable envd authentication:​
  • True: Enable this option. We recommend enabling authentication in production.​
  • False: Disable this option.​
timeout​
The lifetime of the Sandbox. When the timeout expires, the Sandbox is automatically reclaimed.​
  • Unit: seconds​
  • Default value: 300​
metadata​
Optional information that can be added to help distinguish Sandboxes during subsequent queries or O&M. The data type is Dict[str, str].​
​
Step 3: Write and read files​
After you create the Sandbox, read and write files in the Sandbox. The following example writes the local ./test.txt file to the Sandbox, reads it from the Sandbox, and prints the file content.​
​
def write_and_read_file(sbx):​
# Write a local file to /test.txt inside the Sandbox.​
with open("./test.txt", "rb") as file:​
sbx.files.write("/test.txt", file)​
​
# Read file content from the Sandbox.​
file_content = sbx.files.read("/test.txt")​
print("content in sandbox:", file_content)​
​
The following table describes the parameters.​
​
Parameter​
Description​
files.write(path, file)​
Writes a file to the Sandbox. path specifies the file path inside the Sandbox.​
files.read(path)​
Reads and returns the file content from the Sandbox.​
​
Step 4: Run code and commands​
After the file is ready, run Python code and shell commands inside the Sandbox.​
​
def run_code_and_command(sbx):​
# Run Python code.​
sbx.run_code("print('hello world')")​
​
# Run a shell command.​
sbx.commands.run("echo hello")​
​
The following table describes the parameters.​
​
Parameter​
Description​
run_code(code: str)​
Runs Python code in the Sandbox. It is suitable for scenarios such as quick calculations and data processing.​
commands.run(cmd: str)​
Run command-line instructions in the Sandbox. It is suitable for invoking system tools or running scripts.​
​
Subsequent operations​
Pause and resume Sandboxes​
When you need to temporarily stop using a Sandbox or release computing power (stop billing), while retaining the current status of the data generated by the Sandbox, you can use the Sandbox pause/resume feature.​
​
def pause_and_resume(sbx):​
# Pause the Sandbox. The underlying system will asynchronously create a snapshot and reclaim the Pod.​
sbx.beta_pause()​
sandbox_id = sbx.sandbox_id​
​
# When resumption is needed, call the connection method to get it automatically resumed based on the last snapshot.​
sbx = Sandbox.connect(sandbox_id=sandbox_id)​
return sbx​
​
The following table describes the parameters.​
​
Parameter​
Description​
beta_pause()​
Pauses the current Sandbox. This API is asynchronous, meaning that upon successful invocation, the corresponding Pod will be immediately reclaimed while the snapshot is created in the background.​
Sandbox.connect(sandbox_id=...)​
During subsequent connections to the same sandbox (identified by sandbox_id), the Sandbox data will be automatically restored using the writable layer snapshot generated during the last pause.​
​
View the information and list​
When a Sandbox is running, you can view its status.​
​
def print_sandbox_info(sandbox_id: str) -> None:​
sbx = Sandbox.connect(sandbox_id=sandbox_id)​
info = sbx.get_info()​
​
print("sandbox_id =", info.sandbox_id)​
print("template_id =", info.template_id)​
print("state =", info.state)​
print("started_at =", info.started_at)​
print("end_at =", info.end_at)​
print("metadata =", info.metadata)​
​
The following table describes the parameters.​
​
Parameter​
Description​
get_info()​
Returns the Sandbox details, such as the associated bound template, status, start time, end time, and metadata.​
​
Last updated: 2026.09.21 17:08:51