Skip to main content
Claude Managed Agents runs Claude’s agent loop on Anthropic’s platform. With a self-hosted sandbox, the agent’s tools run on infrastructure you choose instead of Anthropic’s cloud sandbox. An Agent37 instance makes a good one: an always-on Linux computer that runs Anthropic’s environment worker as its main process, keeps the agent’s files on its own disk, and bills by the minute, from $4.76 a month. This is not the same as Host Claude Code. There, the whole agent runs on the instance and you talk to it through the Agent API. Here, Anthropic runs the agent, you talk to it through the Claude API, and the instance runs its tool calls.
Paste this into your coding agent

How it works

  • Anthropic runs the agent loop and the model. Each session you create on a self-hosted environment waits in that environment’s work queue.
  • Your Agent37 instance runs ant beta:worker poll, the environment worker from Anthropic’s ant CLI. It claims a session from the queue, runs the agent’s bash and file tools in /workspace, and posts the results back. It works one session at a time.
  • The worker only makes outbound HTTPS calls to api.anthropic.com. The instance needs no public URL, open port, or webhook.

Before you begin

  • An Agent37 API key, exported as AGENT37_API_KEY. Create one in the dashboard.
  • A Claude API key, exported as ANTHROPIC_API_KEY.
  • Node.js, for the npx agent37 CLI that builds the image. No local Docker is needed.

1. Create a self-hosted environment

Create the environment with the Claude API, or in the Claude Console under Environments > New > Self-hosted:
curl
Then open the environment in the Console and click Generate environment key. Key generation is Console-only. Export both values:
The environment key is what the worker authenticates with. A Claude API key in its place fails with 401 Invalid bearer token.

2. Build the worker image

Put this Dockerfile in an empty folder:
Dockerfile
The worker is the image’s main process, and every bash command the agent issues runs inside this image, so install whatever your agent needs: a language runtime, a CLI, your internal packages. tini reaps the background processes those commands leave behind. The image runs on linux/amd64, which is why it fetches the amd64 build of ant; newer versions are on the ant releases page. Build it on Agent37 and publish it as a workspace template:
The build runs on Agent37’s infrastructure and streams its log to your terminal. See Build a custom image for build secrets, private base images, and updates.

3. Start the worker

Create an instance from the template, with the environment id and key as instance env:
curl
The instance is running within seconds, and the worker starts polling. Leave auto-sleep off: the worker’s outbound polling does not count as activity, so a sleeping instance stops claiming sessions. Confirm the worker is connected. workers_polling should read 1:
curl
response
The worker’s own output is in the instance logs: idle; polling for work while it waits, then claimed work and executing tool lines once a session runs.

4. Run a session

Any Managed Agents agent works; nothing in the agent says where its tools run. If you don’t have one yet, create one with the built-in toolset:
curl
Start a session on your self-hosted environment, then send it a message:
curl
Read the turn back with GET /v1/sessions/$SESSION_ID/events, or stream it; see Anthropic’s events and streaming. The bash tool result shows the command ran on your instance, with the instance id as the hostname:
response

Work with the agent’s files

Everything the agent writes stays on the instance’s disk, across sessions and restarts. Read it from your backend with exec:
curl
Anthropic doesn’t mount files or GitHub repositories into a self-hosted sandbox, so stage inputs yourself: git clone or curl them into /workspace over exec before you start the session. To watch the agent work, SSH into the instance.

Scale out

  • More sessions at once. A worker holds one session until a minute after its turn ends (the --max-idle flag, default 1m), so the next message in a conversation continues without going back to the queue. Other sessions wait their turn. To run several at once, create more instances from the same template with the same env: every worker on an environment polls one queue, and each session goes to the first free worker. A later message on a session that was let go queues again and runs on whichever worker is free.
  • Heavier tools. Resize the instance for more CPU, memory, or disk.
  • Separate customers. Sessions on one instance share its disk. When customers must not see each other’s files, give each one its own self-hosted environment and instance.
  • Memory stores. The ant worker doesn’t mount memory stores. If your sessions attach them, replace the ENTRYPOINT with the Python, TypeScript, or Go SDK’s EnvironmentWorker; see Use memory stores.

Cost

The default 2 vCPU / 4 GB instance costs $4.76 a month running around the clock, metered per minute from your Agent37 wallet; larger shapes are on Instances. Model usage bills to your Claude account, not to Agent37.

Troubleshooting