Compute API(Modal 沙盒)
通过 Modal 在安全的云容器中隔离执行代码。创建沙盒、执行任意命令、管理生命周期。支持资源限制、网络隔离和自动清理。专为 AI Agent 代码执行设计的安全运行时。
基础 URL: https://api.jarvisclaw.ai
认证
两种方式均支持 — 所有请求通过 x402 链上结算:
| 方式 | Header | 描述 |
|---|---|---|
| API Key | Authorization: Bearer sk-... | 平台自动从你的 HD 钱包签名 x402 |
| 私钥(x402) | 通过 SDK 自动 | Agent 直接从自身钱包签名 x402 |
端点
POST /sandbox/create
创建新的隔离沙盒。
请求
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| timeout | integer | 否 | 最大存活秒数(默认: 300,最大: 86400) |
| image | string | 否 | Docker 镜像(默认: python:3.11-slim) |
| cpu | float | 否 | CPU 核数(默认: 0.25) |
| memory | integer | 否 | 内存 MB(默认: 256) |
| gpu | string | 否 | GPU 类型(如 T4, A10G) |
响应
json
{
"sandbox_id": "sb-abc123",
"status": "running",
"created_at": "2025-01-15T10:30:00Z",
"timeout": 300
}POST /sandbox/exec
在沙盒中执行命令。
请求
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| sandbox_id | string | 是 | 沙盒 ID |
| command | string | 是 | 要执行的 shell 命令 |
| timeout | integer | 否 | 命令超时秒数(默认: 60) |
| workdir | string | 否 | 工作目录 |
| env | object | 否 | 环境变量 |
响应
json
{
"exit_code": 0,
"stdout": "hello\n",
"stderr": "",
"duration_ms": 45
}POST /sandbox/status
查询沙盒状态。
请求
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| sandbox_id | string | 是 | 沙盒 ID |
响应
json
{
"sandbox_id": "sb-abc123",
"status": "running",
"commands_executed": 5,
"uptime_seconds": 120,
"cpu_usage": 0.15,
"memory_usage_mb": 128
}POST /sandbox/terminate
终止沙盒并释放资源。
请求
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| sandbox_id | string | 是 | 沙盒 ID |
响应
json
{
"sandbox_id": "sb-abc123",
"status": "terminated",
"total_commands": 5,
"total_duration_seconds": 120
}定价
按请求的 timeout 自动选择两种计费模式:短时突发按次固定价,长驻沙箱按小时计费。 下表为参考价,精确扣费金额请读响应中的 price.amount。
突发 —— timeout ≤ 300 秒
sandbox/create 按次固定收费,覆盖整个短会话。生命周期操作(exec、status、terminate) 各 $0.001/次。
| 配置 | 价格 | 资源 |
|---|---|---|
| CPU | $0.012/沙箱 | 最多 1 vCPU、1 GiB 内存、300 秒生命周期 |
| GPU — T4 | $0.05/沙箱 | 最多 8 vCPU、32 GiB 内存 |
| GPU — L4 | $0.08/沙箱 | 最多 8 vCPU、32 GiB 内存 |
| GPU — A10G | $0.10/沙箱 | 最多 8 vCPU、32 GiB 内存 |
| GPU — A100 | $0.20/沙箱 | 最多 8 vCPU、32 GiB 内存 |
| GPU — H100 | $0.40/沙箱 | 最多 8 vCPU、32 GiB 内存 |
长驻 —— timeout > 300 秒,最长 24 小时
按小时单价 × 请求时长,一次性预先通过 x402 结算。提前终止不退款(与云实例惯例一致)。 小时数精确计算 —— T4 上 90 分钟 = 1.5 × $1.50 = $2.25。
| 配置 | 每小时 | 适用场景 |
|---|---|---|
| CPU (1 vCPU / 1 GiB) | $0.10 | 批处理、爬虫、定时任务 |
| T4 | $1.50 | 轻量推理、小模型 |
| L4 | $2.00 | 中端推理 |
| A10G | $2.50 | Stable Diffusion、7B 推理 |
| A100 | $4.00 | 训练、13B–70B 推理 |
| H100 | $8.00 | 前沿训练、低延迟 70B+ |
四个 sandbox 操作全部是 POST;不存在 GET /sandbox 路由。
SDK 示例
python
from jarvisclaw import MarketplaceClient
client = MarketplaceClient(api_key="sk-your-api-key")
# 创建沙盒
sandbox = client.call("compute", "/sandbox/create", method="POST", json={
"timeout": 300,
})
print(f"沙盒 ID: {sandbox['sandbox_id']}")
# 执行命令
result = client.call("compute", "/sandbox/exec", method="POST", json={
"sandbox_id": sandbox["sandbox_id"],
"command": "python -c \"print('hello world')\"",
})
print(f"输出: {result['stdout']}")
# 检查状态
status = client.call("compute", "/sandbox/status", method="POST", json={
"sandbox_id": sandbox["sandbox_id"],
})
print(f"已执行命令数: {status['commands_executed']}")
# 终止
client.call("compute", "/sandbox/terminate", method="POST", json={
"sandbox_id": sandbox["sandbox_id"],
})python
from jarvisclaw import MarketplaceClient
# x402 Agent 钱包 — 通过 Base 上的 USDC 按调用付费
client = MarketplaceClient(private_key="0x<evm-private-key>")
# 创建沙盒
sandbox = client.call("compute", "/sandbox/create", method="POST", json={
"timeout": 300,
})
# 执行任意代码
result = client.call("compute", "/sandbox/exec", method="POST", json={
"sandbox_id": sandbox["sandbox_id"],
"command": "pip install numpy && python -c \"import numpy; print(numpy.random.rand(3))\"",
})
print(f"输出: {result['stdout']}")
# 终止
client.call("compute", "/sandbox/terminate", method="POST", json={
"sandbox_id": sandbox["sandbox_id"],
})go
package main
import (
"context"
"fmt"
jc "github.com/api-jarvisclaw/go-sdk/v2"
)
func main() {
ctx := context.Background()
mc, _ := jc.NewMarketplaceClient(jc.WithAPIKey("sk-your-api-key"))
// 创建沙盒
sandbox, _ := mc.Post(ctx, "compute", "/sandbox/create", map[string]interface{}{
"timeout": 300,
})
fmt.Printf("沙盒 ID: %s\n", sandbox["sandbox_id"])
// 执行命令
result, _ := mc.Post(ctx, "compute", "/sandbox/exec", map[string]interface{}{
"sandbox_id": sandbox["sandbox_id"],
"command": "python -c \"print('hello')\"",
})
fmt.Printf("输出: %s\n", result["stdout"])
// 检查状态
status, _ := mc.Post(ctx, "compute", "/sandbox/status", map[string]interface{}{
"sandbox_id": sandbox["sandbox_id"],
})
fmt.Printf("状态: %s, 命令数: %v\n", status["status"], status["commands_executed"])
// 终止
mc.Post(ctx, "compute", "/sandbox/terminate", map[string]interface{}{
"sandbox_id": sandbox["sandbox_id"],
})
}go
package main
import (
"context"
"fmt"
jc "github.com/api-jarvisclaw/go-sdk/v2"
)
func main() {
ctx := context.Background()
// x402 Agent 钱包 - 通过 Base 上的 USDC 按调用付费 (Chain ID 8453)
mc, _ := jc.NewMarketplaceClient(jc.WithPrivateKey("0x<evm-private-key>"))
// 创建沙盒
sandbox, _ := mc.Post(ctx, "compute", "/sandbox/create", map[string]interface{}{
"timeout": 300,
})
fmt.Printf("沙盒 ID: %s\n", sandbox["sandbox_id"])
// 执行命令
result, _ := mc.Post(ctx, "compute", "/sandbox/exec", map[string]interface{}{
"sandbox_id": sandbox["sandbox_id"],
"command": "python -c \"print('agent execution')\"",
})
fmt.Printf("输出: %s\n", result["stdout"])
// 完成后终止
mc.Post(ctx, "compute", "/sandbox/terminate", map[string]interface{}{
"sandbox_id": sandbox["sandbox_id"],
})
}限制
- 最大超时 24 小时 — 沙盒在配置的超时后自动终止(最大 86400 秒)
- 短任务固定费率 — ≤300 秒 CPU 固定 $0.012,更长会话按小时计费
- 无持久存储 — 沙盒终止后所有数据丢失
- 无入站连接 — 沙盒无法接收来自外部的网络流量
- 最大载荷 10MB — 请求和响应体上限为 10MB
- 允许出站 HTTP — 沙盒可以发起出站请求(pip install、curl 等)