Claude Platform Docs
Managed AgentsSelf-hosted sandboxes

Self-hosted worker reference

Reference for self-hosted sandbox workers: ant CLI flags, environment variables, host requirements, filesystem paths, and SDK helper options.

This page documents the prebuilt workers that serve a self_hosted environment. For task-oriented guides, start with Self-hosted sandboxes and Deploy self-hosted workers.

CLI commands and flags

CommandDescription
ant beta:worker pollClaims work items from the environment’s queue and runs each session in process. With --on-work, calls your script for each work item instead.
ant beta:worker runHandles one claimed session and exits. Use it as the entrypoint of a per-session sandbox.
FlagDescription
--environment-idThe environment to poll for work. Also reads from ANTHROPIC_ENVIRONMENT_ID.
--environment-keyAuthenticates the worker with this environment. Also reads from ANTHROPIC_ENVIRONMENT_KEY.
--workdirDirectory where the worker downloads skills and where tools read and write files. Defaults to . (the current directory). The directory must exist before the worker starts.
--on-workScript to call for each claimed work item instead of running tools in process. Receives session details as environment variables and the work item as JSON on standard input.
--max-idleHow long the worker waits while a session is idle before ant beta:worker run exits or ant beta:worker poll moves on to the next work item. Idle time with a requires_action stop reason does not count. Defaults to 60s. Set it to 0 to turn off the timeout. With an --on-work script, ant beta:worker poll ignores this flag. Pass it instead to the ant beta:worker run command that your script starts.
--log-formatLog output format. Use json for structured log ingestion. Defaults to text.

Environment variables

VariableDescriptionSet by
ANTHROPIC_ENVIRONMENT_IDThe environment whose queue the worker serves.You, on the worker host. The poller passes it to the --on-work script.
ANTHROPIC_ENVIRONMENT_KEYAuthenticates the worker to its queue.You, on the worker host. The poller passes it to the --on-work script.
ANTHROPIC_SESSION_IDThe session that a claimed work item represents.The poller, for the --on-work script.
ANTHROPIC_WORK_IDThe claimed work item.The poller, for the --on-work script.
ANTHROPIC_WORK_SECRETThe work item’s secret for its session.You. The poller does not set it. See Forward the work item’s secret.
ANTHROPIC_BASE_URLOverrides the default API endpoint. Optional.You, on the worker host.
ANTHROPIC_WEBHOOK_SIGNING_KEYVerifies incoming webhook payloads.You, on a webhook handler host.

Host requirements

WorkerRequirement
All workersA Linux or macOS host with /bin/bash at that exact path. The worker’s bash tool runs it directly, without consulting PATH. On macOS, that is the system Bash 3.2.
TypeScript SDKunzip and tar on the PATH, and Node.js 22 or later.
Python and Go SDKsNo additional binaries. These SDKs use their standard libraries for archive extraction.

Memory stores add their own requirements.

On macOS, the root volume is read-only, so mkdir can’t create /workspace. Set the worker’s working directory to one that you can write to, such as a workspace directory in your home directory. To use memory stores on macOS, first prepare the macOS host.

Sandbox filesystem

PathContents
/workspaceThe working directory in the samples on the self-hosted sandboxes pages. It is not the worker’s default: the worker uses the current directory unless you set --workdir or workdir. Anthropic’s cloud sandboxes keep skills and repositories in /workspace. If you use a different working directory, update your agent’s system prompt so Claude can locate the skill files.
<workdir>/skills/<name>/The agent’s downloaded skills.
/mnt/memory/<store>/A local copy of each attached memory store, one directory per store. The API returns each directory’s full path in the mount_path field of the session’s memory store resource (for example, /mnt/memory/user-preferences/).

The worker creates the memory store directories when it claims a session. It removes them when it finishes with that session or when you stop the worker gracefully. That removes only the local copies, and the memories stay in the store.

Skills can include executables that the agent can run directly. The CLI and SDK workers preserve the executable permissions recorded in the skill bundle when they extract it. If you download skills yourself, set their executable permissions yourself.

When the worker finishes with a session

The worker finishes with a session in any of these cases:

  • The session ends.
  • You stop the session’s work item.
  • The session stays idle for the worker’s idle limit. The session itself stays open. When you then send it a new event, such as a user message, the same worker or another one claims it. That worker downloads the stores again.

The idle limit is 60 seconds by default. To change it, use --max-idle for the ant CLI worker or max_idle for EnvironmentWorker. The cma-sandboxes Helm chart starts its workers with a limit of 30 minutes instead. To change that limit, change the chart’s sandbox.idleTimeout setting.

Final deliverables

Self-hosted sandboxes have no /mnt/session/outputs directory. On Anthropic’s cloud sandboxes, the session’s system prompt tells the agent to copy final deliverables there. On self-hosted environments, the system prompt leaves that instruction out. Final deliverables stay wherever the agent writes them, typically under the working directory, and the worker doesn’t upload them.

To collect final deliverables in one place, give the full path of a directory in your agent’s system prompt. Choose a directory that the worker’s file tools can write to:

  • Inside the working directory: For example, /workspace/outputs if the working directory is /workspace.
  • Anywhere else, with an SDK worker only: Add the directory to allowed_roots. See AgentToolContext and the agent toolset.

SDK helpers

The Python, TypeScript, and Go SDKs provide three helpers at different levels of control:

HelperWhat it doesUse it when
EnvironmentWorkerHandles polling, setup, and tool runs end to end.Most cases.
work.poller()Polls the work queue and gives you each claimed session.You determine how to handle each session, for example by launching a sandbox rather than running tools in process.
client.beta.sessions.events.tool_runner()Runs tool calls for a single session, given the session ID and a tool list.You’ve already claimed the work and only need to run the tools.

EnvironmentWorker

MethodDescription
run()Runs indefinitely, picking up sessions as they arrive.
handle_item()Handles one work item already claimed by a poller. The poller can be ant beta:worker poll --on-work or your own code. The method returns when the worker finishes with the item’s session. You can pass work_id, session_id, environment_id, and work_secret. For each one that you leave out, the method reads the matching ANTHROPIC_* variable.
OptionDescription
toolsA factory that receives the session’s AgentToolContext and returns the tool list. Defaults to the standard agent toolset.
max_idleHow long the worker waits while a session is idle before it finishes with the session. Idle time with a requires_action stop reason does not count. Defaults to 60 seconds. To wait 5 minutes, set it to 300. To turn the limit off, set it to None.
memory_sync_intervalThe shortest time between two syncs of the attached memory stores while the session runs. The worker checks the interval after each call to a tool that runs in your sandbox, not on a timer. Defaults to 15 seconds. See Sync interval.
memory_sync_deletionsWhether files the agent deletes locally are also deleted from the store. See Deletions.

EnvironmentWorker manages the AgentToolContext and the toolset automatically. Pass a tools factory to customize the tool list:

import asyncio
import contextlib
import os
import signal
from anthropic import AsyncAnthropic, beta_async_tool
from anthropic.lib.environments import EnvironmentWorker
from anthropic.lib.tools.agent_toolset import beta_bash_tool


@beta_async_tool
async def get_order_status(order_id: str) -> str:
    """Look up an order in the internal fulfillment system by order ID."""
    return f"Order {order_id}: shipped"


async def main() -> None:
    environment_key = os.environ["ANTHROPIC_ENVIRONMENT_KEY"]
    environment_id = os.environ["ANTHROPIC_ENVIRONMENT_ID"]
    async with AsyncAnthropic(auth_token=environment_key) as client:
        worker = EnvironmentWorker(
            client,
            environment_id=environment_id,
            environment_key=environment_key,
            # List every tool that the agent can call. The worker answers no other tool.
            tools=lambda env: [beta_bash_tool(env), get_order_status],
        )
        task = asyncio.create_task(worker.run())
        loop = asyncio.get_running_loop()
        for signum in (signal.SIGINT, signal.SIGTERM):
            loop.add_signal_handler(signum, task.cancel)
        with contextlib.suppress(asyncio.CancelledError):
            await task


asyncio.run(main())

Work poller

OptionDescription
drainWhether to stop polling once the queue is empty rather than waiting for new work.
block_msHow long each poll waits for work to arrive before returning, in milliseconds. Must be between 1 and 999. The helper polls again automatically. Pass None for a non-blocking check. Defaults to a 999ms long-poll.
reclaim_older_than_msReclaims work items that were claimed but never acknowledged within this many milliseconds.
auto_stopWhether the poller posts a stop signal for each work item once your loop body finishes with it. Defaults to True. Set it to False in the two cases that follow this table.

Set auto_stop to False in two cases:

  1. Your loop calls handle_item(), which posts the stop itself.
  2. Your loop launches a sandbox for the item and moves on. The worker in that sandbox must post the stop when the session’s work ends, as ant beta:worker run and handle_item() do.

For a complete example, see Launch sandboxes from the SDK poller.

Session tool runner

client.beta.sessions.events.tool_runner() takes a tool list as tools. To build that list, set up AgentToolContext yourself and call beta_agent_toolset_20260401(env):

from anthropic.lib.tools.agent_toolset import (
    AgentToolContext,
    beta_agent_toolset_20260401,
)

async with AgentToolContext(
    workdir="/workspace", client=client, session_id=work.data.id
) as env:
    # skills downloaded to /workspace/skills/<name>/
    tools = beta_agent_toolset_20260401(env)

AgentToolContext and the agent toolset

AgentToolContext is the execution context for tool calls. It defines the working directory and path policy, and can download the session’s skills.

OptionDescription
allowed_rootsDirectories, in addition to the working directory, that the file tools (read, write, edit, glob, grep) can reach.
read_only_rootsDirectories under which write and edit refuse paths.

EnvironmentWorker adds the session’s memory store directories to allowed_roots itself, and the directories of stores attached with access: "read_only" to read_only_roots.

The confinement is a guardrail for the file tools only, not a sandbox. It does not constrain bash.

beta_agent_toolset_20260401(env) takes an AgentToolContext and returns the standard tool implementations (bash, read, write, edit, glob, grep).

Was this page helpful?