blaxel
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBlaxel Skill Reference
Blaxel技能参考
What is Blaxel
什么是Blaxel
Blaxel (https://blaxel.ai) is a cloud platform that gives AI agents their own compute environments. Its flagship product is perpetual sandboxes: instant-launching microVMs that resume from standby in under 25ms and scale to zero after a few seconds of inactivity.
You use Blaxel primarily to:
- Spin up a sandbox, install dependencies, run a dev server, and expose a live preview URL
- Build and deploy sandbox templates (custom Docker images) for reusable environments
- Deploy AI agents, MCP servers, and batch jobs as serverless endpoints
SDKs: TypeScript () and Python ()
CLI: (install from https://docs.blaxel.ai/cli-reference/introduction)
Docs: https://docs.blaxel.ai
@blaxel/coreblaxelblBlaxel(https://blaxel.ai)是一款为AI Agent提供专属计算环境的云平台。其旗舰产品是永久沙箱:可即时启动的microVM,从待机状态恢复仅需不到25毫秒,且在闲置数秒后会自动缩容至零。
你可以使用Blaxel完成以下主要操作:
- 创建沙箱、安装依赖、启动开发服务器并暴露实时预览URL
- 构建并部署沙箱模板(自定义Docker镜像)以实现可复用环境
- 将AI Agent、MCP服务器和批处理作业部署为无服务器端点
SDK:TypeScript()和Python()
CLI:(从https://docs.blaxel.ai/cli-reference/introduction安装)
文档:https://docs.blaxel.ai
@blaxel/coreblaxelblAuthentication
身份验证
The SDK authenticates using these sources in priority order:
- Blaxel CLI, when logged in
- Environment variables in file (
.env,BL_WORKSPACE)BL_API_KEY - System environment variables
- Blaxel configuration file ()
~/.blaxel/config.yaml
Log in locally (recommended for development):
shell
bl login YOUR-WORKSPACEOr set environment variables (for remote/CI environments):
shell
export BL_WORKSPACE=your-workspace
export BL_API_KEY=your-api-keyWhen running on Blaxel itself, authentication is automatic.
SDK会按以下优先级顺序从这些来源获取身份验证信息:
- 已登录的Blaxel CLI
- 文件中的环境变量(
.env、BL_WORKSPACE)BL_API_KEY - 系统环境变量
- Blaxel配置文件()
~/.blaxel/config.yaml
本地登录(推荐用于开发环境):
shell
bl login YOUR-WORKSPACE或设置环境变量(用于远程/CI环境):
shell
export BL_WORKSPACE=your-workspace
export BL_API_KEY=your-api-key在Blaxel自身环境中运行时,身份验证会自动完成。
Sandbox workflow (primary use case)
沙箱工作流(主要使用场景)
This is the most common workflow: create a sandbox, run commands in it, and get a preview URL.
这是最常见的工作流:创建沙箱、在其中运行命令并获取预览URL。
Step 1: Create a sandbox
步骤1:创建沙箱
Use a public image from the Blaxel Hub (https://github.com/blaxel-ai/sandbox/tree/main/hub):
- — minimal Linux
blaxel/base-image:latest - — Node.js
blaxel/node:latest - — Next.js
blaxel/nextjs:latest - — Vite
blaxel/vite:latest - — Expo (React Native)
blaxel/expo:latest - — Python
blaxel/py-app:latest
Or use a custom template image you deployed yourself.
Declare the ports you need at creation time. Ports cannot be added after creation. Ports 80, 443, and 8080 are reserved.
typescript
import { SandboxInstance } from "@blaxel/core";
const sandbox = await SandboxInstance.createIfNotExists({
name: "my-sandbox",
image: "blaxel/base-image:latest",
memory: 4096,
ports: [{ target: 3000, protocol: "HTTP" }],
});python
from blaxel.core import SandboxInstance
sandbox = await SandboxInstance.create_if_not_exists({
"name": "my-sandbox",
"image": "blaxel/base-image:latest",
"memory": 4096,
"ports": [{"target": 3000, "protocol": "HTTP"}],
})Use / to reuse an existing sandbox by name or create a new one.
createIfNotExistscreate_if_not_exists使用Blaxel Hub中的公开镜像(https://github.com/blaxel-ai/sandbox/tree/main/hub):
- — 轻量Linux镜像
blaxel/base-image:latest - — Node.js环境
blaxel/node:latest - — Next.js环境
blaxel/nextjs:latest - — Vite环境
blaxel/vite:latest - — Expo(React Native)环境
blaxel/expo:latest - — Python环境
blaxel/py-app:latest
也可以使用你自行部署的自定义模板镜像。
创建时需声明所需端口,端口无法在创建后添加。80、443和8080端口为预留端口。
typescript
import { SandboxInstance } from "@blaxel/core";
const sandbox = await SandboxInstance.createIfNotExists({
name: "my-sandbox",
image: "blaxel/base-image:latest",
memory: 4096,
ports: [{ target: 3000, protocol: "HTTP" }],
});python
from blaxel.core import SandboxInstance
sandbox = await SandboxInstance.create_if_not_exists({
"name": "my-sandbox",
"image": "blaxel/base-image:latest",
"memory": 4096,
"ports": [{"target": 3000, "protocol": "HTTP"}],
})使用 / 方法可按名称复用现有沙箱,若不存在则创建新沙箱。
createIfNotExistscreate_if_not_existsStep 2: Write files and run commands
步骤2:写入文件并运行命令
typescript
// Write files
await sandbox.fs.write("/app/package.json", JSON.stringify({
name: "my-app",
scripts: { dev: "astro dev --host 0.0.0.0 --port 3000" },
dependencies: { "astro": "latest" }
}));
// Or write multiple files at once
await sandbox.fs.writeTree([
{ path: "src/pages/index.astro", content: "<h1>Hello</h1>" },
{ path: "astro.config.mjs", content: "import { defineConfig } from 'astro/config';\nexport default defineConfig({});" },
], "/app");
// Execute a command and wait for it to finish
const install = await sandbox.process.exec({
name: "install",
command: "npm install",
workingDir: "/app",
waitForCompletion: true,
timeout: 60000,
});
// Start a long-running dev server (don't wait for completion)
const devServer = await sandbox.process.exec({
name: "dev-server",
command: "npm run dev",
workingDir: "/app",
waitForPorts: [3000], // returns once port 3000 is open
});python
await sandbox.fs.write("/app/package.json", '{"name":"my-app","scripts":{"dev":"astro dev --host 0.0.0.0 --port 3000"},"dependencies":{"astro":"latest"}}')
await sandbox.fs.write_tree([
{"path": "src/pages/index.astro", "content": "<h1>Hello</h1>"},
{"path": "astro.config.mjs", "content": "import { defineConfig } from 'astro/config';\nexport default defineConfig({});"},
], "/app")
install = await sandbox.process.exec({
"name": "install",
"command": "npm install",
"working_dir": "/app",
"wait_for_completion": True,
"timeout": 60000,
})
dev_server = await sandbox.process.exec({
"name": "dev-server",
"command": "npm run dev",
"working_dir": "/app",
"wait_for_ports": [3000],
})IMPORTANT: Dev servers must bind to (not ) to be reachable through preview URLs. Use or the env variable.
0.0.0.0localhost--host 0.0.0.0HOSTtypescript
// 写入文件
await sandbox.fs.write("/app/package.json", JSON.stringify({
name: "my-app",
scripts: { dev: "astro dev --host 0.0.0.0 --port 3000" },
dependencies: { "astro": "latest" }
}));
// 或批量写入多个文件
await sandbox.fs.writeTree([
{ path: "src/pages/index.astro", content: "<h1>Hello</h1>" },
{ path: "astro.config.mjs", content: "import { defineConfig } from 'astro/config';\nexport default defineConfig({});" },
], "/app");
// 执行命令并等待完成
const install = await sandbox.process.exec({
name: "install",
command: "npm install",
workingDir: "/app",
waitForCompletion: true,
timeout: 60000,
});
// 启动长期运行的开发服务器(不等待完成)
const devServer = await sandbox.process.exec({
name: "dev-server",
command: "npm run dev",
workingDir: "/app",
waitForPorts: [3000], // 端口3000开放后返回
});python
await sandbox.fs.write("/app/package.json", '{"name":"my-app","scripts":{"dev":"astro dev --host 0.0.0.0 --port 3000"},"dependencies":{"astro":"latest"}}')
await sandbox.fs.write_tree([
{"path": "src/pages/index.astro", "content": "<h1>Hello</h1>"},
{"path": "astro.config.mjs", "content": "import { defineConfig } from 'astro/config';\nexport default defineConfig({});"},
], "/app")
install = await sandbox.process.exec({
"name": "install",
"command": "npm install",
"working_dir": "/app",
"wait_for_completion": True,
"timeout": 60000,
})
dev_server = await sandbox.process.exec({
"name": "dev-server",
"command": "npm run dev",
"working_dir": "/app",
"wait_for_ports": [3000],
})重要提示:开发服务器必须绑定到(而非),才能通过预览URL访问。可使用参数或环境变量设置。
0.0.0.0localhost--host 0.0.0.0HOSTStep 3: Create a preview URL
步骤3:创建预览URL
typescript
const preview = await sandbox.previews.createIfNotExists({
metadata: { name: "app-preview" },
spec: { port: 3000, public: true },
});
const url = preview.spec?.url;
// url => https://xxxx.us-pdx-1.preview.bl.runpython
preview = await sandbox.previews.create_if_not_exists({
"metadata": {"name": "app-preview"},
"spec": {"port": 3000, "public": True},
})
url = preview.spec.urlFor private previews, set and create a token:
public: falsetypescript
const preview = await sandbox.previews.createIfNotExists({
metadata: { name: "private-preview" },
spec: { port: 3000, public: false },
});
const token = await preview.tokens.create(new Date(Date.now() + 10 * 60 * 1000));
// Access: preview.spec?.url + "?bl_preview_token=" + token.valuetypescript
const preview = await sandbox.previews.createIfNotExists({
metadata: { name: "app-preview" },
spec: { port: 3000, public: true },
});
const url = preview.spec?.url;
// url => https://xxxx.us-pdx-1.preview.bl.runpython
preview = await sandbox.previews.create_if_not_exists({
"metadata": {"name": "app-preview"},
"spec": {"port": 3000, "public": True},
})
url = preview.spec.url若要创建私有预览,设置并生成令牌:
public: falsetypescript
const preview = await sandbox.previews.createIfNotExists({
metadata: { name: "private-preview" },
spec: { port: 3000, public: false },
});
const token = await preview.tokens.create(new Date(Date.now() + 10 * 60 * 1000));
// 访问方式:preview.spec?.url + "?bl_preview_token=" + token.valueStep 4: Manage the sandbox
步骤4:管理沙箱
typescript
// Reconnect to an existing sandbox
const sandbox = await SandboxInstance.get("my-sandbox");
// List files
const { subdirectories, files } = await sandbox.fs.ls("/app");
// Read a file
const content = await sandbox.fs.read("/app/src/pages/index.astro");
// Get process info / logs
const proc = await sandbox.process.get("dev-server");
const logs = proc.logs; // available if waitForCompletion was true
// Kill a process
await sandbox.process.kill("dev-server");
// Delete the sandbox (all data is erased)
await sandbox.delete();python
sandbox = await SandboxInstance.get("my-sandbox")
result = await sandbox.fs.ls("/app")
content = await sandbox.fs.read("/app/src/pages/index.astro")
proc = await sandbox.process.get("dev-server")typescript
// 重新连接到现有沙箱
const sandbox = await SandboxInstance.get("my-sandbox");
// 列出文件
const { subdirectories, files } = await sandbox.fs.ls("/app");
// 读取文件
const content = await sandbox.fs.read("/app/src/pages/index.astro");
// 获取进程信息/日志
const proc = await sandbox.process.get("dev-server");
const logs = proc.logs; // 仅当waitForCompletion为true时可用
// 终止进程
await sandbox.process.kill("dev-server");
// 删除沙箱(所有数据将被清除)
await sandbox.delete();python
sandbox = await SandboxInstance.get("my-sandbox")
result = await sandbox.fs.ls("/app")
content = await sandbox.fs.read("/app/src/pages/index.astro")
proc = await sandbox.process.get("dev-server")proc.logs available if wait_for_completion was True
proc.logs 仅当wait_for_completion为True时可用
await sandbox.process.kill("dev-server")
await sandbox.delete()
undefinedawait sandbox.process.kill("dev-server")
await sandbox.delete()
undefinedSandbox templates (custom images)
沙箱模板(自定义镜像)
When you need a reusable environment (e.g. an Astro project with all deps pre-installed), create a template:
shell
bl new sandbox my-astro-template
cd my-astro-templateThis creates: , , , .
blaxel.tomlDockerfileentrypoint.shMakefileCustomize the Dockerfile. Always include the sandbox-api binary:
dockerfile
FROM node:22-alpine
WORKDIR /app
COPY /sandbox-api /usr/local/bin/sandbox-api
RUN npm install -g astro
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]The entrypoint.sh must start the sandbox-api:
bash
#!/bin/sh
/usr/local/bin/sandbox-api &
while ! nc -z 127.0.0.1 8080; do sleep 0.1; done
echo "Sandbox API ready"当你需要可复用环境(例如预安装所有依赖的Astro项目)时,可创建模板:
shell
bl new sandbox my-astro-template
cd my-astro-template此命令会创建以下文件:、、、。
blaxel.tomlDockerfileentrypoint.shMakefile自定义Dockerfile时,务必包含sandbox-api二进制文件:
dockerfile
FROM node:22-alpine
WORKDIR /app
COPY /sandbox-api /usr/local/bin/sandbox-api
RUN npm install -g astro
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]entrypoint.sh必须启动sandbox-api:
bash
#!/bin/sh
/usr/local/bin/sandbox-api &
while ! nc -z 127.0.0.1 8080; do sleep 0.1; done
echo "Sandbox API ready"Optionally start a process via the sandbox API:
可选项:通过sandbox API启动进程:
curl http://127.0.0.1:8080/process -X POST -d '{"workingDir":"/app","command":"npm run dev","waitForCompletion":false}' -H "Content-Type: application/json"
curl http://127.0.0.1:8080/process -X POST -d '{"workingDir":"/app","command":"npm run dev","waitForCompletion":false}' -H "Content-Type: application/json"
wait
Deploy the template:
```shell
bl deployThen retrieve the IMAGE_ID and use it to create sandboxes:
shell
bl get sandboxes my-astro-template -ojson | jq -r '.[0].spec.runtime.image'typescript
const sandbox = await SandboxInstance.createIfNotExists({
name: "project-sandbox",
image: "IMAGE_ID",
memory: 4096,
ports: [{ target: 3000, protocol: "HTTP" }],
});wait
部署模板:
```shell
bl deploy然后获取IMAGE_ID并用于创建沙箱:
shell
bl get sandboxes my-astro-template -ojson | jq -r '.[0].spec.runtime.image'typescript
const sandbox = await SandboxInstance.createIfNotExists({
name: "project-sandbox",
image: "IMAGE_ID",
memory: 4096,
ports: [{ target: 3000, protocol: "HTTP" }],
});Tutorials and Examples
教程与示例
Sandboxes
沙箱
Agents
Agent
Core CLI commands
核心CLI命令
For CLI commands that may prompt for input (like confirmations), add to auto-confirm when running in non-interactive / no-TTY environments (e.g. scripts, CI, agents).
-y| Command | Purpose |
|---|---|
| Authenticate to workspace |
| Initialize new resource from template |
| Build and deploy resource to Blaxel |
| Deploy from a specific directory |
| Run resource locally for testing |
| Run locally with hot reload |
| List resources |
| Watch a sandbox deployment status |
| Remove resource |
| Open interactive terminal in sandbox |
| Interactive chat with deployed agent |
| Execute a deployed batch job |
对于可能需要交互输入的CLI命令(如确认操作),在非交互式/无TTY环境(例如脚本、CI、Agent)中运行时,添加参数可自动确认。
-y| 命令 | 用途 |
|---|---|
| 登录到工作区 |
| 从模板初始化新资源 |
| 构建并将资源部署到Blaxel |
| 从指定目录部署资源 |
| 本地运行资源以进行测试 |
| 本地运行并启用热重载 |
| 列出资源 |
| 监控沙箱部署状态 |
| 删除资源 |
| 在沙箱中打开交互式终端 |
| 与已部署的Agent进行交互式对话 |
| 执行已部署的批处理作业 |
blaxel.toml structure
blaxel.toml结构
toml
name = "my-resource"
type = "sandbox" # sandbox, agent, function, job, volume-template
[env]
NODE_ENV = "development" # NOT for secrets — use Variables-and-secrets
[runtime]
memory = 4096 # MB
generation = "mk3"toml
name = "my-resource"
type = "sandbox" # sandbox, agent, function, job, volume-template
[env]
NODE_ENV = "development" # 请勿在此存储敏感信息 — 使用变量与密钥功能
[runtime]
memory = 4096 # 单位:MB
generation = "mk3"timeout = 900 # seconds (agents max 900, jobs max 86400)
timeout = 900 # 单位:秒(Agent最长900秒,作业最长86400秒)
Ports (sandbox only)
端口配置(仅沙箱可用)
[[runtime.ports]]
name = "dev-server"
target = 3000
protocol = "tcp"
undefined[[runtime.ports]]
name = "dev-server"
target = 3000
protocol = "tcp"
undefinedImportant gotchas
重要注意事项
- Ports must be declared at sandbox creation time — they cannot be added later
- Ports 80, 443, 8080 are reserved by Blaxel
- Dev servers must bind to , not
0.0.0.0, for preview URLs to worklocalhost - ~50% of sandbox memory is reserved for the in-memory filesystem (tmpfs). Use volumes for extra storage
- Sandboxes auto-scale to zero after ~5s of inactivity. State is preserved in standby and resumes in <25ms
- has a max timeout of 60 seconds. For longer processes, use
waitForCompletionwithprocess.wait()maxWait - Secrets should never go in — use the Variables-and-secrets page in the Console
[env]
- 端口必须在沙箱创建时声明,无法在创建后添加
- 80、443和8080端口为Blaxel预留端口
- 开发服务器必须绑定到而非
0.0.0.0,才能通过预览URL访问localhost - 沙箱约50%的内存会预留给内存文件系统(tmpfs),如需额外存储请使用卷
- 沙箱在闲置约5秒后会自动缩容至零,待机状态下的状态会被保留,恢复仅需不到25毫秒
- 的最大超时时间为60秒,如需更长时间的进程,请使用
waitForCompletion并设置process.wait()maxWait - 敏感信息绝不能存储在中 — 请使用控制台的“变量与密钥”功能
[env]
Agent Drive (shared filesystem)
Agent Drive(共享文件系统)
Agent Drive is a distributed filesystem backed by SeaweedFS that can be mounted to multiple sandboxes or agents at any time, including while they are already running. Unlike volumes (block storage attached only at sandbox creation), drives support concurrent read-write access from multiple sandboxes and can be attached/detached dynamically.
This feature is currently in private preview. During the preview, Agent Drive is only available in theregion. Both drive and sandbox must be in this region.us-was-1
Use cases:
- Passing data between sandboxes without intermediary services
- Storing tool outputs and context histories for other agents
- Sharing datasets across multiple agents
- Creating a shared filesystem cache of package dependencies
Agent Drive是基于SeaweedFS的分布式文件系统,可随时挂载到多个沙箱或Agent,即使它们已处于运行状态。与卷(仅能在沙箱创建时挂载的块存储)不同,Drive支持从多个沙箱并发读写,且可动态挂载/卸载。
该功能目前处于私有预览阶段。预览期间,Agent Drive仅在区域可用,且Drive与沙箱必须位于同一区域。us-was-1
适用场景:
- 无需中间服务即可在沙箱间传递数据
- 为其他Agent存储工具输出和上下文历史
- 在多个Agent间共享数据集
- 创建包依赖的共享文件系统缓存
Create a drive
创建Drive
typescript
import { DriveInstance } from "@blaxel/core";
const drive = await DriveInstance.createIfNotExists({
name: "my-drive",
region: "us-was-1",
displayName: "My Project Drive", // optional; defaults to name
labels: { env: "dev", project: "x" }, // optional
});python
from blaxel.core.drive import DriveInstance
drive = await DriveInstance.create_if_not_exists(
{
"name": "my-drive",
"region": "us-was-1",
"display_name": "My Project Drive",
"labels": {"env": "dev", "project": "x"},
}
)typescript
import { DriveInstance } from "@blaxel/core";
const drive = await DriveInstance.createIfNotExists({
name: "my-drive",
region: "us-was-1",
displayName: "My Project Drive", // 可选;默认值为name
labels: { env: "dev", project: "x" }, // 可选
});python
from blaxel.core.drive import DriveInstance
drive = await DriveInstance.create_if_not_exists(
{
"name": "my-drive",
"region": "us-was-1",
"display_name": "My Project Drive",
"labels": {"env": "dev", "project": "x"},
}
)Mount a drive to a sandbox
将Drive挂载到沙箱
typescript
import { SandboxInstance } from "@blaxel/core";
const sandbox = await SandboxInstance.get("my-sandbox");
await sandbox.drives.mount({
driveName: "my-drive",
mountPath: "/mnt/data",
drivePath: "/", // optional; defaults to root of the drive
});python
from blaxel.core import SandboxInstance
sandbox = await SandboxInstance.get("my-sandbox")
await sandbox.drives.mount(
drive_name="my-drive",
mount_path="/mnt/data",
drive_path="/",
)Once mounted, any file written to the mount path inside the sandbox is stored on the drive and persists even after the sandbox is deleted.
typescript
import { SandboxInstance } from "@blaxel/core";
const sandbox = await SandboxInstance.get("my-sandbox");
await sandbox.drives.mount({
driveName: "my-drive",
mountPath: "/mnt/data",
drivePath: "/", // 可选;默认值为Drive的根目录
});python
from blaxel.core import SandboxInstance
sandbox = await SandboxInstance.get("my-sandbox")
await sandbox.drives.mount(
drive_name="my-drive",
mount_path="/mnt/data",
drive_path="/",
)挂载完成后,沙箱内写入挂载路径的所有文件都会存储在Drive上,即使沙箱被删除,数据也会保留。
Mount a subdirectory
挂载子目录
typescript
await sandbox.drives.mount({
driveName: "my-drive",
mountPath: "/app/project",
drivePath: "/projects/alpha",
});python
await sandbox.drives.mount(
drive_name="my-drive",
mount_path="/app/project",
drive_path="/projects/alpha",
)typescript
await sandbox.drives.mount({
driveName: "my-drive",
mountPath: "/app/project",
drivePath: "/projects/alpha",
});python
await sandbox.drives.mount(
drive_name="my-drive",
mount_path="/app/project",
drive_path="/projects/alpha",
)List, unmount, and delete drives
列出、卸载和删除Drive
typescript
// List mounted drives on a sandbox
const mounts = await sandbox.drives.list();
// List all drives
const drives = await DriveInstance.list();
// Unmount
await sandbox.drives.unmount("/mnt/data");
// Delete a drive
await DriveInstance.delete("my-drive");
// or instance-level:
const drive = await DriveInstance.get("my-drive");
await drive.delete();python
mounts = await sandbox.drives.list()
drives = await DriveInstance.list()
await sandbox.drives.unmount("/mnt/data")
await DriveInstance.delete("my-drive")typescript
// 列出沙箱上已挂载的Drive
const mounts = await sandbox.drives.list();
// 列出所有Drive
const drives = await DriveInstance.list();
// 卸载Drive
await sandbox.drives.unmount("/mnt/data");
// 删除Drive
await DriveInstance.delete("my-drive");
// 或通过实例对象删除:
const drive = await DriveInstance.get("my-drive");
await drive.delete();python
mounts = await sandbox.drives.list()
drives = await DriveInstance.list()
await sandbox.drives.unmount("/mnt/data")
await DriveInstance.delete("my-drive")or instance-level:
或通过实例对象删除:
drive = await DriveInstance.get("my-drive")
await drive.delete()
CLI: `bl get drives`drive = await DriveInstance.get("my-drive")
await drive.delete()
CLI命令:`bl get drives`Full Agent Drive example
Agent Drive完整示例
typescript
import { SandboxInstance, DriveInstance } from "@blaxel/core";
// 1. Create a drive
const drive = await DriveInstance.createIfNotExists({
name: "agent-storage",
region: "us-was-1",
});
// 2. Create a sandbox (use image ID from custom template)
const sandbox = await SandboxInstance.createIfNotExists({
name: "my-agent-sandbox",
image: "my-sandbox-image-id",
memory: 2048,
region: "us-was-1",
});
// 3. Mount the drive
await sandbox.drives.mount({
driveName: "agent-storage",
mountPath: "/mnt/storage",
drivePath: "/",
});
// 4. Write a file to the mounted drive
await sandbox.fs.write("/mnt/storage/hello.txt", "Hello from the drive!");
// 5. Read it back
const content = await sandbox.fs.read("/mnt/storage/hello.txt");
console.log(content); // "Hello from the drive!"
// 6. List mounted drives
const mounts = await sandbox.drives.list();
console.log(mounts);python
import asyncio
from blaxel.core.drive import DriveInstance
from blaxel.core import SandboxInstance
async def main():
drive = await DriveInstance.create_if_not_exists(
{"name": "agent-storage", "region": "us-was-1"}
)
sandbox = await SandboxInstance.create_if_not_exists(
{
"name": "my-agent-sandbox",
"image": "my-sandbox-image-id",
"memory": 2048,
"region": "us-was-1",
}
)
await sandbox.drives.mount(
drive_name="agent-storage",
mount_path="/mnt/storage",
drive_path="/",
)
await sandbox.fs.write("/mnt/storage/hello.txt", "Hello from the drive!")
content = await sandbox.fs.read("/mnt/storage/hello.txt")
print(content)
mounts = await sandbox.drives.list()
print(mounts)
asyncio.run(main())typescript
import { SandboxInstance, DriveInstance } from "@blaxel/core";
// 1. 创建Drive
const drive = await DriveInstance.createIfNotExists({
name: "agent-storage",
region: "us-was-1",
});
// 2. 创建沙箱(使用自定义模板的镜像ID)
const sandbox = await SandboxInstance.createIfNotExists({
name: "my-agent-sandbox",
image: "my-sandbox-image-id",
memory: 2048,
region: "us-was-1",
});
// 3. 挂载Drive
await sandbox.drives.mount({
driveName: "agent-storage",
mountPath: "/mnt/storage",
drivePath: "/",
});
// 4. 向挂载的Drive写入文件
await sandbox.fs.write("/mnt/storage/hello.txt", "Hello from the drive!");
// 5. 读取文件内容
const content = await sandbox.fs.read("/mnt/storage/hello.txt");
console.log(content); // "Hello from the drive!"
// 6. 列出已挂载的Drive
const mounts = await sandbox.drives.list();
console.log(mounts);python
import asyncio
from blaxel.core.drive import DriveInstance
from blaxel.core import SandboxInstance
async def main():
drive = await DriveInstance.create_if_not_exists(
{"name": "agent-storage", "region": "us-was-1"}
)
sandbox = await SandboxInstance.create_if_not_exists(
{
"name": "my-agent-sandbox",
"image": "my-sandbox-image-id",
"memory": 2048,
"region": "us-was-1",
}
)
await sandbox.drives.mount(
drive_name="agent-storage",
mount_path="/mnt/storage",
drive_path="/",
)
await sandbox.fs.write("/mnt/storage/hello.txt", "Hello from the drive!")
content = await sandbox.fs.read("/mnt/storage/hello.txt")
print(content)
mounts = await sandbox.drives.list()
print(mounts)
asyncio.run(main())Other Blaxel resources
Blaxel其他资源
Agents Hosting
Agent托管
Deploy AI agents as serverless auto-scaling HTTP endpoints. Framework-agnostic (LangChain, CrewAI, Claude SDK, etc.).
shell
bl new agent将AI Agent部署为可自动扩缩容的无服务器HTTP端点。支持所有主流框架(LangChain、CrewAI、Claude SDK等)。
shell
bl new agentdevelop in src/agent.ts or src/agent.py
在src/agent.ts或src/agent.py中开发
bl serve # test locally
bl deploy # deploy
bl chat AGENT-NAME # query
Sync endpoint handles requests up to 100s, async endpoint up to 10 minutes.
Docs: https://docs.blaxel.ai/Agents/Overviewbl serve # 本地测试
bl deploy # 部署
bl chat AGENT-NAME # 与Agent交互
同步端点可处理最长100秒的请求,异步端点可处理最长10分钟的请求。
文档:https://docs.blaxel.ai/Agents/OverviewMCP Servers Hosting
MCP服务器托管
Deploy custom tool servers following the MCP protocol.
shell
bl new mcp部署遵循MCP协议的自定义工具服务器。
shell
bl new mcpimplement in src/server.ts or src/server.py
在src/server.ts或src/server.py中实现
bl serve --hotreload # test locally
bl deploy
Agents connect to deployed MCP servers via SDK:
```typescript
const tools = await blTools(["functions/my-mcp-server"]);Every sandbox also exposes its own built-in MCP server at with tools for process management, filesystem, and code generation.
Docs: https://docs.blaxel.ai/Functions/Overview
https://<SANDBOX_URL>/mcpbl serve --hotreload # 本地测试并启用热重载
bl deploy
Agent可通过SDK连接到已部署的MCP服务器:
```typescript
const tools = await blTools(["functions/my-mcp-server"]);每个沙箱还会在暴露内置的MCP服务器,提供进程管理、文件系统和代码生成等工具。
文档:https://docs.blaxel.ai/Functions/Overview
https://<SANDBOX_URL>/mcpBatch Jobs
批处理作业
Scalable compute for parallel background tasks (minutes to hours).
shell
bl new job用于并行后台任务的可扩缩容计算(支持数分钟到数小时的任务)。
shell
bl new jobimplement in src/index.ts or src/index.py
在src/index.ts或src/index.py中实现
bl deploy
bl run job NAME --data '{"tasks": [...]}'
Max 24h per task. Set `maxConcurrentTasks` in blaxel.toml.
Docs: https://docs.blaxel.ai/Jobs/Overviewbl deploy
bl run job NAME --data '{"tasks": [...]}'
每个任务最长可运行24小时。可在blaxel.toml中设置`maxConcurrentTasks`。
文档:https://docs.blaxel.ai/Jobs/OverviewResources
资源链接
- Deployment configuration reference: https://docs.blaxel.ai/deployment-reference
- CLI command reference: https://docs.blaxel.ai/cli-reference/introduction
Read individual SDK files for detailed explanations and code examples:
- ./references/sdk-python.md
- ./references/sdk-typescript.md
Each SDK README contains:
- An overview of the SDK
- Requirements
- Code examples for working with sandboxes, volumes, agents, batch jobs, MCP
- Additional useful information
For additional documentation, see: https://docs.blaxel.ai/llms.txt
- 部署配置参考:https://docs.blaxel.ai/deployment-reference
- CLI命令参考:https://docs.blaxel.ai/cli-reference/introduction
查看各个SDK文件以获取详细说明和代码示例:
- ./references/sdk-python.md
- ./references/sdk-typescript.md
每个SDK的README包含:
- SDK概述
- 环境要求
- 沙箱、卷、Agent、批处理作业、MCP的代码示例
- 其他实用信息
更多文档请查看:https://docs.blaxel.ai/llms.txt