> ## Documentation Index
> Fetch the complete documentation index at: https://boxrouter.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Run Claude Managed Agents

> Run Anthropic's self-hosted environment worker in a BoxRouter sandbox, so Claude's tool calls execute there.

Claude Managed Agents runs the agent on Anthropic's servers. A worker polls Anthropic for tool calls and runs them where it lives. `boxrouter launch claude-managed-agents-worker` puts that worker in a BoxRouter sandbox, so the agent's files, installed tools and network are the sandbox's.

**Before You Begin:** A BoxRouter API key and CLI, a self-hosted environment with its key, and an agent in the Claude Console.

## Before You Begin

[Set Up Your Account](https://boxrouter.ai/docs/setup.md) and install the [CLI](https://boxrouter.ai/docs/cli.md#install). Then, following Anthropic's [quickstart](https://platform.claude.com/docs/en/managed-agents/quickstart):

1. In the [Claude Console](https://platform.claude.com/environments), create a `self_hosted` environment and generate its environment key.
2. Create an agent with the `agent_toolset_20260401` toolset.

The launch needs two secrets: `ANTHROPIC_ENVIRONMENT_ID` identifies the queue, and
`ANTHROPIC_ENVIRONMENT_KEY` authorizes the worker to poll it. Save them as
[workspace secrets](https://boxrouter.ai/docs/launch/secrets.md). If a name is not saved, the CLI uses its terminal environment
variable or asks at a hidden prompt. That fallback is not saved.

Keep `ANTHROPIC_API_KEY`, which creates sessions and inspects the queue, on your own machine.

## Launch a Worker

```python Python theme={"theme":"css-variables"}
from boxrouter import BoxRouter

with BoxRouter() as client:
    box = client.sandboxes.launch("claude-managed-agents-worker")
    print(f"Sandbox {box.id} started")
```

```typescript TypeScript theme={"theme":"css-variables"}
import { BoxRouter } from '@boxrouter/sdk';

const client = new BoxRouter();
try {
  const box = await client.sandboxes.launch('claude-managed-agents-worker');
  console.log(`Sandbox ${box.id} started`);
} finally {
  await client.close();
}
```

```bash CLI theme={"theme":"css-variables"}
boxrouter launch claude-managed-agents-worker
```

Output

```text theme={"theme":"css-variables"}
Launch queued
Launch running: create running
Launch running: create completed, setup running
Launch succeeded: create completed, setup completed
Sandbox SANDBOX_ID started
```

The SDK examples print only the last line.

Setup installs `ant` 2.1.0 after checking its SHA-256. Then the sandbox starts `ant beta:worker poll --workdir "$HOME"` as an unattended command. The worker needs `/bin/bash` and outbound HTTPS.

The sandbox lifetime is 3600 seconds unless you pass `--lifetime`; it can run past that time. When the sandbox ends, the worker goes with it.

## Send a Task

For this example, use an environment with only this worker. Any worker polling the same environment
can claim a session; the file read below uses this sandbox's ID.

Install the Anthropic Python SDK with `pip install anthropic`. On your own machine, set
`ANTHROPIC_API_KEY`, `ANTHROPIC_ENVIRONMENT_ID`, and your agent's `ANTHROPIC_AGENT_ID`. Save this as `claude-session.py`:

```python theme={"theme":"css-variables"}
import os
import anthropic

client = anthropic.Anthropic()
environment_id = os.environ["ANTHROPIC_ENVIRONMENT_ID"]
stats = client.beta.environments.work.stats(environment_id)
print("workers_polling:", stats.workers_polling)
if stats.workers_polling < 1:
    raise SystemExit("No workers polling; try again when a worker is ready.")

session = client.beta.sessions.create(
    agent=os.environ["ANTHROPIC_AGENT_ID"], environment_id=environment_id
)
prompt = "Run uname -a and write its output to /tmp/boxrouter-hello.txt. Make the file readable by all users."
client.beta.sessions.events.send(
    session.id,
    events=[{"type": "user.message", "content": [{"type": "text", "text": prompt}]}],
)
print("session:", session.id)
```

Run `python claude-session.py`.

If `workers_polling` stays at 0, see the next section. Follow the session in the Console or Anthropic's [event stream](https://platform.claude.com/docs/en/managed-agents/events-and-streaming). When it finishes, replace `SANDBOX_ID` with the ID printed at launch and read the result. The sample file is readable by other users in the sandbox, so do not put secrets in it:

```python Python theme={"theme":"css-variables"}
import sys

from boxrouter import BoxRouter

with BoxRouter() as client:
    box = client.sandboxes.get("SANDBOX_ID")
    sys.stdout.buffer.write(box.files.read("/tmp/boxrouter-hello.txt"))
```

```typescript TypeScript theme={"theme":"css-variables"}
import { BoxRouter } from '@boxrouter/sdk';

const client = new BoxRouter();
try {
  const box = await client.sandboxes.get('SANDBOX_ID');
  process.stdout.write(await box.files.read('/tmp/boxrouter-hello.txt'));
} finally {
  await client.close();
}
```

```bash CLI theme={"theme":"css-variables"}
boxrouter read SANDBOX_ID --path /tmp/boxrouter-hello.txt
```

## If `workers_polling` Stays at 0

A worker whose key or environment ID Anthropic rejects exits on its first poll with `401 Unauthorized`. The sandbox stays up, and the launch still reports success.

Check that the environment ID and key belong to the same self-hosted environment. Update the saved
secrets, or the terminal variables if the names are not saved. Then destroy this sandbox and launch
again.

## More Workers

`--copies N` starts N sandboxes that poll the same environment ([Copies](https://boxrouter.ai/docs/launch/copies.md) covers limits and results). `--lifetime` cannot be combined with `--copies`. An idle worker takes later sessions, so N copies run up to N sessions at once. A session's files stay on the worker that claimed it.

## Clean Up

Destroy a worker's sandbox with `boxrouter destroy SANDBOX_ID`, or stop a run with `boxrouter launch stop RUN_ID`. Destroying the sandbox does not end the Anthropic session; end that in the Console or through Anthropic's API.

To run the worker from your own code, call [Create a launch](https://boxrouter.ai/docs/reference/launch-create.md) with the catalog manifest from `GET /agents/claude-managed-agents-worker`. To give the agent a project, set the manifest's [code field](https://boxrouter.ai/docs/launch/manifest.md#fields) and tell the agent the checkout path. Worker authentication and session isolation are in Anthropic's [self-hosted sandbox guide](https://platform.claude.com/docs/en/managed-agents/self-hosted-sandboxes).

## Next Steps

> [card] [Run Copies of an Agent](https://boxrouter.ai/docs/launch/copies.md)
>
> Run several workers at once.

> [card] [Supply an Agent's Secrets](https://boxrouter.ai/docs/launch/secrets.md)
>
> Save the environment values once.

> [card] [Choose an Agent Integration](https://boxrouter.ai/docs/integrations/overview.md)
>
> Compare the integrations for your agent stack.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.