Skip to main content

Secure WASM runtime to isolate and manage AI agent tasks

Project description

capsule-run

A secure, durable runtime for agentic workflows

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

Installation

pip install capsule-run

Quick Start

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

Use --verbose to display real-time task execution details.

How It Works

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."""
    return {"processed": len(dataset), "status": "complete"}

The runtime requires a task named "main" as the entry point. Python can define the main task itself, but it's recommended to set it manually.

When you run capsule run main.py, your code is compiled into a WebAssembly module and executed in a dedicated sandbox.

Response Format

Every task returns a structured JSON envelope containing both the result and execution metadata:

{
  "success": true,
  "result": { "processed": 5, "status": "complete" },
  "error": null,
  "execution": {
    "task_name": "data_processor",
    "duration_ms": 1523,
    "retries": 0,
    "fuel_consumed": 45000
  }
}

Response fields:

  • success — Boolean indicating whether the task completed successfully
  • result — The actual return value from your task (json, string, null on failure etc..)
  • error — Error details if the task failed ({ error_type: string, message: string })
  • execution — Performance metrics:
    • task_name — Name of the executed task
    • duration_ms — Execution time in milliseconds
    • retries — Number of retry attempts that occurred
    • fuel_consumed — CPU resources used (see Compute Levels)

Documentation

Task Configuration Options

Parameter Description Type Default Example
name Task identifier str function name "process_data"
compute CPU level: "LOW", "MEDIUM", "HIGH" str "MEDIUM" "HIGH"
ram Memory limit str unlimited "512MB", "2GB"
timeout Maximum execution time str unlimited "30s", "5m"
max_retries Retry attempts on failure int 0 3
allowed_files Folders accessible in the sandbox list [] ["./data", "./output"]
env_variables Environment variables accessible in the sandbox list [] ["API_KEY"]

Compute Levels

  • LOW: Minimal allocation for lightweight tasks
  • MEDIUM: Balanced resources for typical workloads
  • HIGH: Maximum fuel for compute-intensive operations
  • CUSTOM: Specify exact fuel value (e.g., compute="1000000")

Project Configuration (Optional)

Create a capsule.toml file in your project root to set default options:

[workflow]
name = "My AI Workflow"
version = "1.0.0"
entrypoint = "src/main.py"  # Run `capsule run` without specifying a file

[tasks]
default_compute = "MEDIUM"
default_ram = "256MB"
default_timeout = "30s"

Task-level options always override these defaults.

HTTP Client

Standard requests library isn't compatible with WASM. Use Capsule's HTTP client:

from capsule import task
from capsule.http import get, post

@task(name="fetch", compute="MEDIUM", timeout="30s")
def main() -> dict:
    response = get("https://api.example.com/data")
    return {"status": response.status_code, "ok": response.ok()}

File Access

Tasks can read and write files within directories specified in allowed_files. Any attempt to access files outside these directories is not possible.

from capsule import task

@task(name="restricted_writer", allowed_files=["./output"])
def restricted_writer() -> None:
    with open("./output/result.txt", "w") as f:
        f.write("result")

@task(name="main")
def main() -> str:
    restricted_writer()

Environment Variables

Tasks can access environment variables to read configuration, API keys, or other runtime settings. Use Python's standard os.environ to access environment variables:

from capsule import task
import os

@task(name="main", env_variables=["API_KEY"])
def main() -> dict:
    api_key = os.environ.get("API_KEY")
    return {"api_key": api_key}

Compatibility

Supported:

  • Pure Python packages and standard library
  • json, math, re, datetime, collections, etc.

⚠️ Not yet supported:

  • Packages with C extensions (e.g numpy, pandas)

Links

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

capsule_run-0.5.1.tar.gz (105.9 kB view details)

Uploaded Source

Built Distributions

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

capsule_run-0.5.1-py3-none-win_amd64.whl (10.1 MB view details)

Uploaded Python 3Windows x86-64

capsule_run-0.5.1-py3-none-manylinux_2_39_x86_64.whl (10.5 MB view details)

Uploaded Python 3manylinux: glibc 2.39+ x86-64

capsule_run-0.5.1-py3-none-macosx_11_0_arm64.whl (9.5 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

File details

Details for the file capsule_run-0.5.1.tar.gz.

File metadata

  • Download URL: capsule_run-0.5.1.tar.gz
  • Upload date:
  • Size: 105.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.11.5

File hashes

Hashes for capsule_run-0.5.1.tar.gz
Algorithm Hash digest
SHA256 06cf0b0d86d57f4cdc0aaaba4d3b7ea79a4f8f20d52b8a48bb1b2cf792b29d51
MD5 b477c6ad66527883537dec88e799b138
BLAKE2b-256 8d1c9e0acd7f25c032642e1acec90ec5838dfce1d959b0956f32d4234b6f50d0

See more details on using hashes here.

File details

Details for the file capsule_run-0.5.1-py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for capsule_run-0.5.1-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 b9fe86c85b33f91f92e84c8eddfc5a6f4e6d5d8d410abef974b2c5c7f25e6e37
MD5 ab885eec0316e282ff273e775aa8472d
BLAKE2b-256 ba38f93eaaf157790480f6b4d1712f5f04218bf2934fe4ae28cc96e047b063a0

See more details on using hashes here.

File details

Details for the file capsule_run-0.5.1-py3-none-manylinux_2_39_x86_64.whl.

File metadata

File hashes

Hashes for capsule_run-0.5.1-py3-none-manylinux_2_39_x86_64.whl
Algorithm Hash digest
SHA256 3b422895d06aed9d126692bbd612f2970569ac9c97cb5b8e195e7d118346416a
MD5 6a01e5c36aa1cda3b1fbeb9528d1ba22
BLAKE2b-256 0c172348c9391c044c8cf84ceeb4e77b1fd4a80a9f634a96b45c9118588f5e59

See more details on using hashes here.

File details

Details for the file capsule_run-0.5.1-py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for capsule_run-0.5.1-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dc79cc6cd6dcabddea4aafda058ba634af757f6213ddd965f9c5ce01f7b250dc
MD5 92b17b184eb3518ba0f3fbc5138a6928
BLAKE2b-256 5e0be297125182bc5cdcc8a855848ddcb44439132585becba582b3688a08a48f

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