---
title: "Run Claude Code, Codex and OpenCode in a sandbox · Pethost"
description: "Claude Code, Codex and OpenCode are installed in every Pethost sandbox: one call gives an agent a prompt, another streams each tool call. Keys stay outside."
---

[Agent Sandboxes](https://pethost.dev/sandboxes/) Coding agents, installed

# Coding agents in the cloud: Claude Code, Codex and OpenCode

Run Claude Code, Codex or OpenCode in the cloud at full autonomy, each in a VM made for the task: the agent may do anything inside it, and it holds none of your keys.

A prompt to a coding agent in a sandbox, and its tool calls streaming back: it reads a file, edits it, runs the tests, and the turn ends done with its token count.

## Three coding agents, installed

Our `base` template has Claude Code, Codex and OpenCode installed with their official SDKs, and their CLIs on the PATH: `claude`, `codex`, `opencode`. Each runs without asking for permission: Claude Code in `bypassPermissions` mode, Codex with full access and no approvals, OpenCode with every permission allowed and its question tool off, since nobody would answer it. The sandbox is the boundary: a Firecracker microVM with its own kernel, where the agent has root and reaches what your egress allow-list lets it.

## One call per prompt

`prompt_agent` gives an agent a prompt, in a new conversation or one you name, with a model and a working directory if you want them, and waits up to 45 seconds for the turn. A conversation runs one turn at a time: further prompts are queued. A turn answers its state (QUEUED, RUNNING, DONE, FAILED, STOPPED or LOST), the agent’s last message up to 16 KiB, and the tokens it read and wrote. `get_agent_turn` waits for a longer turn, and `stop_agent_turn` ends one.

`watch_conversation` streams what the agent does, in the same shape for all three agents: each prompt, each message it writes, each tool call with its input as JSON, each result, and the end of each turn. When a turn ends, the agent ends with it, but what it started in the background runs on: a dev server it started is still there to open at its [preview URL](https://pethost.dev/sandboxes/preview-urls/).

Python

CLI

```
import os
from pethost.sandboxes import EnvVar, Environment, Sandboxes, SandboxSettings

sandboxes = Sandboxes()  # reads PETHOST_TOKEN
key = os.environ["ANTHROPIC_API_KEY"]
sandboxes.set_secret("anthropic", value=key, hosts=["api.anthropic.com"])
env = Environment(variables=[EnvVar(name="ANTHROPIC_API_KEY", secret="anthropic")])
box = sandboxes.create_sandbox(settings=SandboxSettings(environment=env))

prompt = "Write fizzbuzz.py with a test and run it"
turn = sandboxes.prompt_agent(box.sandbox_id, prompt=prompt, wait_seconds=0)
conversation = turn.conversation_id
for event in sandboxes.watch_conversation(box.sandbox_id, conversation_id=conversation):
    if event.tool_call:
        print("›", event.tool_call.name, event.tool_call.input[:60])
    elif event.turn_end:
        print(event.turn_end.state.name, event.turn_end.reply)
        break
```

```
pethost sandbox secret set anthropic --host api.anthropic.com < key.txt
pethost sandbox new --name fizzbuzz --env ANTHROPIC_API_KEY=secret:anthropic
pethost sandbox agent fizzbuzz "Write fizzbuzz.py with a test and run it"
pethost sandbox agent fizzbuzz --agent opencode "Explain fizzbuzz.py"
```

## Signing in without the key inside

Keep your API key as a workspace secret and bind it to `ANTHROPIC_API_KEY` or `OPENAI_API_KEY`. The variable inside the sandbox holds a placeholder, and the egress proxy outside the VM puts the real key into the request headers, only on the way to the hosts the secret names, such as `api.anthropic.com`. A program that prints its environment sees the placeholder. [How secrets the sandbox never holds work](https://pethost.dev/sandboxes/secrets/).

Or a person signs in once in the sandbox’s terminal, with `claude auth login` or `codex login --device-auth`. OpenCode needs no sign-in at all: without a provider’s key it uses OpenCode’s own free models. A prompt to an agent that cannot reach its model is refused with the variable to set and the command that signs in, so your code can say what is missing.

## The conversation stays with the sandbox

A conversation’s history is on the sandbox’s disk. A [pause](https://pethost.dev/sandboxes/pause-resume/) keeps it with the agent’s files and processes, and the next prompt picks it up. A fork carries it: copy a sandbox between two turns into ten, and send each copy’s conversation its next prompt in another direction. A template keeps no conversations: a sandbox started from one begins with none.

To watch an agent work, stream its conversation, or open the sandbox’s page in the web console, whose Agents tab shows its conversations.

## Limits and prices

One turn at a time per conversation. The agent runs on the sandbox’s vCPUs and memory, billed by the second at its size’s price, $0.018 an hour for SMALL (2 vCPU, 4 GiB) and $0.036 for DEFAULT (4 vCPU, 8 GiB). A running turn keeps the sandbox awake; between turns, idle for 5 minutes, it pauses at 30% of that price. The model’s tokens are your provider’s bill, not ours.

## How it compares

On 21 November 2026, boat.dev’s proxy that keeps AI keys out of its sandboxes cost $1 for each day it is on, plus $1 per 1,000 requests. At Pethost the swap is part of every sandbox, for any key and any HTTPS host you name, with no price of its own.

[Read the docs](https://pethost.dev/docs/sandboxes/coding-agents/)

## Questions

**Do I need an API key to run Claude Code in a sandbox?**

For Claude Code and Codex, either a key bound as a secret or a person’s sign-in in the terminal. Claude Code also signs in with `CLAUDE_CODE_OAUTH_TOKEN`, Bedrock or Vertex. OpenCode runs with no key, on its own free models.

**Does my key enter the sandbox?**

Not when it is a secret: the agent and every process see a placeholder, and the key is added to requests outside the VM. A sign-in made in the terminal is stored inside the sandbox, as on your computer.

**Can I branch a conversation?**

Yes: [fork the sandbox](https://pethost.dev/sandboxes/fork/). Each copy carries the conversation, and a prompt with its id goes on from there in that copy alone.

**Can I watch the agent work?**

Yes: stream its conversation with `watch_conversation`, or open the sandbox in the web console. A terminal link that runs `claude` gives you a session of your own beside it.

## Related

- [Inside the sandbox the API key is a placeholder; the egress proxy puts the real key into the request to api.anthropic.com, and a request to another host keeps the placeholder. Keys that never enter A sandbox sees a placeholder; the egress proxy swaps in the real API key in HTTPS request headers, only for the hosts you name. How it works](https://pethost.dev/sandboxes/secrets/)
- [One running sandbox at step 41 of a task is forked into three copies in 0.25 s; each carries on from step 41 on its own. Fork, memory and all Copy a running sandbox, its memory, processes and disk, into new sandboxes in one call. Three copies take about a quarter of a second. How it works](https://pethost.dev/sandboxes/fork/)

## Give your agent a computer in 0.15 s

[Start the free trial](https://console.pethost.dev/welcome?kind=sandboxes)

Pethost · Stephan Gomer · NIP 1133096755

Pethost

- [Templates](https://pethost.dev/templates/)
- [About](https://pethost.dev/about/)
- [Docs](https://pethost.dev/docs/)
- [Blog](https://pethost.dev/blog/)
- [API](https://pethost.dev/docs/api/)
- [CLI](https://pethost.dev/docs/cli/)
- [Plugin](https://github.com/pethost-dev/plugin)
- [Terms](https://pethost.dev/terms/)
- [Privacy](https://pethost.dev/privacy/)
- [Refunds](https://pethost.dev/refunds/)
- [support@pethost.dev](mailto:support@pethost.dev)

Databases

- [Postgres](https://pethost.dev/postgres/)
- [Valkey](https://pethost.dev/valkey/)

Agent Sandboxes

- [Sandboxes](https://pethost.dev/sandboxes/)
- [Pricing](https://pethost.dev/sandboxes/#pricing)
- [Docs](https://pethost.dev/docs/sandboxes/)
- [API](https://pethost.dev/docs/sandboxes/api/)

Comparisons

- [Coolify vs Dokploy vs CapRover vs Dokku](https://pethost.dev/blog/coolify-vs-dokploy-vs-caprover-vs-dokku/)
- [Coolify Cloud vs Dokploy Cloud](https://pethost.dev/blog/coolify-cloud-vs-dokploy-cloud/)
- [Railway vs Render vs Fly.io pricing](https://pethost.dev/blog/railway-vs-render-vs-fly-io-pricing/)
- [Managed Docker Compose hosting](https://pethost.dev/blog/managed-docker-compose-hosting/)

Alternatives

- [Railway alternatives](https://pethost.dev/blog/railway-alternatives/)
- [Heroku alternatives](https://pethost.dev/blog/heroku-alternatives/)
- [Dokploy alternatives](https://pethost.dev/blog/dokploy-alternatives/)
- [Coolify alternatives](https://pethost.dev/blog/coolify-alternatives/)
- [Render alternatives](https://pethost.dev/blog/render-alternatives/)
- [Sliplane alternatives](https://pethost.dev/blog/sliplane-alternatives/)
