A secure, durable runtime for AI agents
```Capsule```
A secure, durable runtime for AI agents
Getting Started • Documentation • Contributing
Overview
Capsule is a runtime for coordinating AI agent tasks in isolated environments. It is designed to handle untrusted code execution, long-running workflows, large-scale processing, or even multi-agent systems.
Each task runs inside its own WebAssembly sandbox, providing:
- Isolated execution: Each task runs isolated from your host system
- Resource limits: Set CPU, memory, and timeout limits per task
- Automatic retries: Handle failures without manual intervention
- Lifecycle tracking: Monitor which tasks are running, completed, or failed
This enables safe task-level execution of untrusted code within AI agent systems.
How It Works
With Python
Simply annotate your Python functions with the @task decorator:
from capsule import task
@task(name="analyze_data", compute="MEDIUM", ram="512MB", timeout="30s", max_retries=1)
def analyze_data(dataset: list) -> dict:
"""Process data in an isolated, resource-controlled environment."""
# Your code runs safely in a Wasm sandbox
return {"processed": len(dataset), "status": "complete"}
With TypeScript / JavaScript
Use the task() wrapper function with full access to the npm ecosystem:
import { task } from "@capsule-run/sdk";
export const analyzeData = task({
name: "analyze_data",
compute: "MEDIUM",
ram: "512MB",
timeout: "30s",
maxRetries: 1
}, (dataset: number[]): object => {
// Your code runs safely in a Wasm sandbox
return { processed: dataset.length, status: "complete" };
});
// The "main" task is required as the entrypoint
export const main = task({
name: "main",
compute: "HIGH"
}, () => {
return analyzeData([1, 2, 3, 4, 5]);
});
[!NOTE] The runtime requires a task named
"main"as the entry point. Python will create one automatically if none is defined, but it's recommended to set it explicitly.
When you run capsule run main.py (or main.ts), your code is compiled into a WebAssembly module and executed in isolated sandboxes.
Each task operates within its own sandbox with configurable resource limits, ensuring that failures are contained and don't cascade to other parts of your workflow. The host system controls every aspect of execution, from CPU allocation via Wasm fuel metering to memory constraints and timeout enforcement.
Getting Started
Python
pip install capsule-run
Create hello.py:
from capsule import task
@task(name="main", compute="LOW", ram="64MB")
def main() -> str:
return "Hello from Capsule!"
Run it:
capsule run hello.py
TypeScript / JavaScript
npm install -g @capsule-run/cli
npm install @capsule-run/sdk
Create hello.ts:
import { task } from "@capsule-run/sdk";
export const main = task({
name: "main",
compute: "LOW",
ram: "64MB"
}, (): string => {
return "Hello from Capsule!";
});
Run it:
capsule run hello.ts
[!TIP] Add
--verboseto see real-time task execution details.
Run From Your Code
The run() function lets you execute tasks programmatically from your code instead of using the CLI. The args are automatically forwarded as parameters to the main task.
Python
from capsule import run
result = await run(
file="./sandbox.py",
args=["code to execute"]
)
Create sandbox.py:
from capsule import task
@task(name="main", compute="LOW", ram="64MB")
def main(code: str) -> str:
return eval(code)
TypeScript / JavaScript
[!IMPORTANT] You need
@capsule-run/cliin your dependencies to use the runner functions in TypeScript.
import { run } from '@capsule-run/sdk/runner';
const result = await run({
file: './sandbox.ts',
args: ['code to execute']
});
Create sandbox.ts:
import { task } from "@capsule-run/sdk";
export const main = task({
name: "main",
compute: "LOW",
ram: "64MB"
}, (code: string): string => {
return eval(code);
});
[!TIP] If you're looking for a pre-configured, ready-to-use solution, check out the Python adapter or TypeScript adapter.
Documentation
Task Configuration Options
Configure your tasks with these parameters:
| Parameter | Description | Type | Default | Example |
|---|---|---|---|---|
name |
Task identifier | str |
function name (Python) / required (TS) | "process_data" |
compute |
CPU allocation level: "LOW", |
Configuration
{"mcpServers": {"capsule": {"command": "capsule", "args": ["run"]}}}