Skip to main content

Python SDK for AgentCube Code Interpreter

Project description

AgentCube Python SDK

The official Python SDK for AgentCube, enabling programmatic interaction with secure, isolated Code Interpreter environments.

This SDK creates a seamless bridge between your application and the AgentCube runtime, handling the complexity of:

  • Session Management: Automatically creating and destroying isolated environments.
  • Execution: Running shell commands and code (Python, Bash) remotely.
  • File Management: Uploading and downloading files to/from the sandbox.

Features

  • Simple API: Pythonic context managers (with statement) for automatic resource cleanup.
  • Flexible: Supports both short-lived (ephemeral) and long-running sessions.
  • Kubernetes Native: Automatically authenticates using Service Account tokens when running in-cluster.

Installation

git clone https://github.com/volcano-sh/agentcube.git
cd agentcube/sdk-python
pip install .

Quick Start

from agentcube import CodeInterpreterClient

with CodeInterpreterClient() as client:
    output = client.run_code("python", "print('Hello, AgentCube!')")
    print(output)
# Session automatically deleted on exit

Manual Lifecycle Management

For long-running applications, you can manually control the lifecycle:

# Create a session with a 1-hour timeout
client = CodeInterpreterClient(ttl=3600)

try:
    client.run_code("python", "print('Session started')")
    # ... perform operations ...
    # You can continue using the same client for multiple operations
    client.run_code("python", "x = 42")
    client.run_code("python", "print(f'x = {x}')")  # x persists within session
finally:
    client.stop()  # CRITICAL: Ensure resources are released

File Operations

with CodeInterpreterClient() as client:
    client.upload_file("./data.csv", "/workspace/data.csv")
    client.run_code("python", """
import pandas as pd
df = pd.read_csv('/workspace/data.csv')
df.describe().to_csv('/workspace/summary.csv')
""")
    client.download_file("/workspace/summary.csv", "./summary.csv")

API Reference

Method Description
start() Explicitly start the session (optional, auto-starts on first API call)
execute_command(cmd) Execute shell command
run_code(language, code) Execute code (python/bash)
upload_file(local, remote) Upload file to sandbox
download_file(remote, local) Download file from sandbox
write_file(content, path) Write string to file
list_files(path) List directory contents
stop() Delete session and release resources

Configuration

CodeInterpreterClient(
    name="custom-template",        # CodeInterpreter CRD template name
    namespace="agentcube",         # Kubernetes namespace
    ttl=7200,                      # Session TTL (seconds)
    session_id="existing-id",      # Optional: reuse existing session
    verbose=True                   # Enable debug logging
)

Environment Variables:

  • WORKLOAD_MANAGER_URL: Control Plane URL
  • ROUTER_URL: Data Plane URL
  • API_TOKEN: Authentication token

Advanced: Session Reuse

For workflows requiring state persistence across multiple client instances:

# Step 1: Create session
client1 = CodeInterpreterClient()
client1.run_code("python", "x = 42")
session_id = client1.session_id  # Save for reuse
# Don't call stop() - let session persist

# Step 2: Reuse session with new client
client2 = CodeInterpreterClient(session_id=session_id)
client2.run_code("python", "print(x)")  # x still exists
client2.stop()  # Cleanup when done

Development

make build-python-sdk
python3 -m pytest sdk-python/tests/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agentcube_sdk-0.0.10.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agentcube_sdk-0.0.10-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file agentcube_sdk-0.0.10.tar.gz.

File metadata

  • Download URL: agentcube_sdk-0.0.10.tar.gz
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for agentcube_sdk-0.0.10.tar.gz
Algorithm Hash digest
SHA256 9a2d50dc3fba5e191e559df061dbdafaa85c889011efe5d13fdb36747ba7e118
MD5 31c88b8d69896214db411c51e7e2cb32
BLAKE2b-256 2c6da4290383512565aaf061741912e8f7c80a1c15abf4042e3d7c570027c800

See more details on using hashes here.

File details

Details for the file agentcube_sdk-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: agentcube_sdk-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for agentcube_sdk-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 ae7a01af767b1f8fa8f6776062289d3b116e0587b55b4d8c191bbe9cbd7bea45
MD5 7c7e9a32208d1d715d31ab5f3c32aebe
BLAKE2b-256 f88ca8af4af74b807e533658cc7b3e847d3ba08297f2172385824b08eb0ed427

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page