📦 Capsule
A secure, durable runtime for agentic workflows
Getting Started • Documentation • Contributing
Overview
Capsule is a runtime for coordinating AI agent tasks in isolated environments. It is designed to handle, long-running workflows, large-scale processing, autonomous decision-making securely, 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
Capsule leverages Wasm to create secure, isolated execution environments.
With Python
Simply annotate your Python functions with the @task decorator:
"""Process data in an isolated, resource-controlled environment."""
# Your code runs safely in a Wasm sandbox
return
With TypeScript / JavaScript
Capsule now supports TypeScript and JavaScript with the task() wrapper function. This offers compatibility with the entire JavaScript 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] TypeScript/JavaScript projects require a task named
"main"as the entrypoint.
When you run capsule run main.py (or main.ts), your code is compiled into a WebAssembly module and executed in a dedicated sandbox to isolate tasks.
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.
Quick Start
Python
Create hello.py:
return
Run it:
TypeScript / JavaScript
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:
[!TIP] Use
--verboseto display real-time task execution details.
Documentation
Task Configuration Options
Configure your tasks with these parameters:
| Parameter | Type | Description | Example |
|---|---|---|---|
name |
str |
Task identifier (defaults to function name) | "process_data" |
compute |
str |
CPU allocation level: "LOW", "MEDIUM", or "HIGH" |
"MEDIUM" |
ram |
str |
Memory limit for the task | "512MB", "2GB" |
timeout |
str |
Maximum execution time | "30s", "5m", "1h" |
max_retries |
int |
Number of retry attempts on failure (default: 1) | 3 |
Compute Levels
Capsule controls CPU usage through WebAssembly's fuel mechanism, which meters instruction execution. The compute level determines how much fuel your task receives.
- LOW provides minimal allocation for lightweight tasks
- MEDIUM offers balanced resources for typical workloads
- HIGH grants maximum fuel for compute-intensive operations
- CUSTOM to specify an exact fuel value (e.g.,
compute="1000000") for precise control over execution limits.
HTTP Client API
Python
The standard Python requests library and socket-based networking aren't natively compatible with WebAssembly's sandboxed I/O model. Capsule provides its own HTTP client that works within the Wasm environment:
"""Example demonstrating HTTP client usage within a task."""
# GET request
=
# POST with JSON body
=
# Response methods
= # Returns True if status code is 2xx
= # Get the HTTP status code
= # Parse response as JSON
= # Get response as text
return
TypeScript / JavaScript
Capsule also provides an HTTP client for TypeScript/JavaScript via @capsule-run/sdk. However, standard libraries like fetch already compatible, so you can use whichever approach you prefer.
Compatibility
[!NOTE] TypeScript/JavaScript has broader compatibility than Python since it doesn't rely on native bindings.
Python: Pure Python packages and standard library modules work. Packages with C extensions (numpy, pandas) are not yet supported.
TypeScript/JavaScript: npm packages and ES modules work. Node.js built-ins (fs, path, os) are not available in the sandbox.
Contributing
Contributions are welcome!
Development setup
Prerequisites: Rust (latest stable), Python 3.13+, Node.js 22+
# Build and install CLI
# Python SDK (editable install)
# TypeScript SDK (link for local dev)
&& &&
# Then in your project: npm link @capsule-run/sdk
How to contribute
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Run tests:
cargo test - Open a Pull Request
Need help? Open an issue
Credits
Capsule builds on these open source projects:
- componentize-py – Python to WebAssembly Component compilation
- jco – JavaScript toolchain for WebAssembly Components
- wasmtime – WebAssembly runtime
- WASI – WebAssembly System Interface
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.