Prerequisites
- A Python development environment with Docker installed.
- An Anthropic API key. If not, sign up for an Anthropic account and obtain an API key.
- A Blaxel account and API key. If not, sign up for a Blaxel account and obtain an API key.
- The Blaxel CLI. If not, download and install the Blaxel CLI.
- The Anthropic CLI (optional). If not, download and install the Anthropic CLI.
How it works
Under this approach, the agent loop is managed by Anthropic, while sandbox creation and tool execution happens on the Blaxel platform.
- When a session starts, Anthropic sends an event to an orchestrator sandbox running on Blaxel. The orchestrator sandbox exposes a public preview URL that is registered with Anthropic.
- The orchestrator receives events from Anthropic and spawns a worker sandbox for the session. The worker is a short-lived sandbox that exists to execute tool calls, perform file operations and execute code.
- The worker process exits shortly after the session goes idle, and Blaxel auto-deletes the worker sandbox when its TTL (measured from creation) expires. The orchestrator sandbox is a long-running sandbox which continues to exist to receive future events.
1. Clone the repository and log in to Blaxel
Clone the sample repository, which includes templates for the orchestrator sandbox, worker sandbox, and related scripts.2. Create a self-hosted environment
Define an Anthropic environment for the agent and generate an environment key to authenticate the worker sandbox with Anthropic’s work queue.- Claude Platform Web console
- Anthropic CLI
- On the Claude Platform, navigate to Managed Agents > Environments.
- Click Add environment.
- Configure a new environment:
- Name: “Blaxel”
- Type: Self-hosted
- Once the environment is created, click Generate environment key.
- Copy and save the environment key for use in a later step.
Use one self-hosted environment per Blaxel workspace. Every orchestrator connected to an environment competes for the same work queue, and whichever claims a work item first creates the worker sandbox in its own workspace. If another orchestrator in a different workspace is connected to the same environment, your sessions still complete, but the worker sandboxes appear in that other workspace.
3. Build and push the sandbox images
The template repository includes two sandbox images: a worker that runs agent tool calls, and an orchestrator that handles webhooks and spawns workers. The next step is to push these images to Blaxel. Pushing an image to Blaxel packages your code and uploads it to Blaxel’s registry so it can be provisioned as an on-demand sandbox during agent runs. See thebl push reference for more details.
Worker image
The worker image is based onnode:22-bookworm-slim and contains multiple pre-installed applications and tools. Push it from the worker/ directory in the cookbook repo:
Orchestrator image
The orchestrator runs a FastAPI server that exposes an endpoint to receive webhook events. Push it from theorchestrator/ directory:
session.status_run_started events from Anthropic’s CMA platform, verifies the webhook signature, and schedules a background dispatch task for the session. The webhook handler returns 200 immediately; sandbox creation happens in the background.
The main workhorse is _dispatch_for_session, which runs as a background task for each incoming session:
- It debounces briefly so that multiple near-simultaneous webhooks can be batched into a single dispatch cycle, ensuring the queue is efficiently polled.
- It collects all pending session IDs and pre-warms their sandboxes in parallel.
- It calls
_drain_and_dispatch_work, which claims queued work items via the Anthropic SDK’swork.pollerand, for each item, calls_dispatch_work_item.
_dispatch_work_item function is where a specific work item is actually handed off to its session sandbox. It runs ant beta:worker run as a process on the pre-readied sandbox, passing ANTHROPIC_WORK_ID and ANTHROPIC_SESSION_ID as environment variables so the worker binds to that work item:
wait_for_completion=False and keep_alive=True so the sandbox stays active while ant run serves the session. The ant beta:worker run command owns the heartbeat and stop logic for the claimed work item; the orchestrator does not heartbeat after handing off.
The _ensure_worker_ready function creates Blaxel worker sandboxes (or reuses one if it already exists for that session):
ttl field defaults to 2h, ensuring that Blaxel deletes the sandbox 2 hours after creation regardless of activity. This is a cleanup guarantee for cases where the orchestrator never issues an explicit delete. It should be configured (via BLAXEL_WORKER_TTL) to a value greater than the longest expected session length.
4. Deploy the orchestrator
Runsetup.py from the repository to start the orchestrator sandbox and create a public preview URL:
setup.py prints the orchestrator’s public preview URL (e.g. https://<id>.preview.bl.run/webhook). Note this URL for the next step.
5. Configure the webhook
Register the orchestrator’s preview URL in the Claude Platform so the platform notifies it when a new session starts.- On the Claude Platform, navigate to Manage > Webhooks.
- Click Add webhook endpoint.
- Set the endpoint URL to the preview URL from the previous step and subscribe to the
session.status_run_startedevent. This is the only event the orchestrator consumes. - Once created, copy the generated signing key, then re-run
setup.pywith this value exported:
6. Create an agent
Create an agent configuration that defines the model and system prompt for each agent session.- Claude Platform Web console
- Anthropic CLI
- On the Claude Platform, navigate to Managed Agents > Agents.
- Click New agent.
- Click the Template tab and select Blank agent.
- In the Agent config section, update the following YAML keys:
name: “Blaxel Assistant”model: “claude-sonnet-4-6”system: “You are a helpful assistant.”
- Make sure the
toolssection includes- type: agent_toolset_20260401. This toolset provides the built-in file and bash tools the agent runs in the worker sandbox. - Click Create agent.
7. Start a session
With the environment and agent configured, start a session to run the agent.- Claude Platform Web console
- Anthropic CLI
- On the Claude Platform, navigate to Managed Agents > Sessions.
- Click New session.
- Select your agent and the Blaxel environment, then send an initial message.
session.status_run_started event to the orchestrator. The orchestrator schedules a background task that creates the worker sandbox, claims the work item from Anthropic’s queue, and uses the sandbox to execute tool calls and post results back to Anthropic. The worker process exits when the session goes idle, and the sandbox is auto-deleted when its TTL expires.
The orchestrator sandbox and its public preview URL persist until you remove them. Delete the sandbox when done so the public webhook endpoint does not stay live:
Resources
Want more info on developing and deploying agents on Blaxel? Check out the following resources:Connect to a sandbox from Claude Agent SDK
Operate a remote Blaxel sandbox from your own self-hosted Claude Agent SDK (using MCP).
Run Claude Code in a sandbox
Deploy Claude Code CLI inside of a Blaxel sandbox.
Build and deploy an agent on Blaxel with Claude Agent SDK
Complete tutorial for building an agent with Claude Agent SDK and deploying it on Blaxel as a serverless auto-scalable API.
Manage environment variables
Complete tutorial for managing variables and secrets when deploying to Blaxel.

