Skip to content
Agent in a Box: sandboxing AI coding agents with Strands Box

Secrets and the egress gateway

After this page you will know how a boxed agent reaches the network at all, and how it can use a key it never holds. The story needs it now: Claude Code starts in the box but says “Not logged in”, and the security engineer does not want the real model key or the Cloudflare tokens inside the agent’s process, where a prompt injection or a log line could carry them away.

Your files at this point
$HOME/box-ws/profiles/ws/profile.toml
packs = ["@box/base"]
[project]
access = "read"
[storage]
directories = ["config", "tmp"]
[agent]
command = ["${{ home }}/.local/bin/claude", { slot = "agent-args" }]
[agent.env]
CLAUDE_CONFIG_DIR = "${{ storage.config }}"
CLAUDE_CODE_TMPDIR = "${{ storage.tmp }}"
$HOME/leaderboard-ops/cloudflare-code-mode.json
{
"mcpServers": {
"cloudflare": {
"type": "http",
"url": "https://mcp.cloudflare.com/mcp",
"headers": { "Authorization": "Bearer ${CLOUDFLARE_MCP_TOKEN}" }
}
}
}
Architecture, stage 2 of 5Pieces: macOS keychain, Developer's terminal, Claude Code, Contained filesystem view, box CLI and Box Core, Stand-in secrets, Egress gateway, Anthropic API, Cloudflare MCP server, Cloudflare API, Sample app. New at this stage: Contained filesystem view, box CLI and Box Core, Stand-in secrets, Egress gateway. A full text description follows the diagram.The boxAgent's sandboxTrusted processbox runstartsmacOS keychainthree secretsDeveloper'sterminalClaude Codethe agentContainedfilesystem viewnewbox CLI and Box CorenewStand-in secretsreal values stay herenewEgress gatewaydecides net:connect,http:request, andmcp:call for aremote MCP servernewAnthropic APIapi.anthropic.comCloudflare MCP servercode mode: search, executeCloudflare APIapi.cloudflare.comSample appWorker box-poc-workshop,its D1, KV andWorkers Logsholds names and email addressesnew at this stage
Text description of the diagram

Stage 2 of 5.

Pieces in the picture:

  • the macOS keychain, which holds the three secrets; the developer reads them into shell variables
  • the developer's terminal
  • Claude Code, the agent
  • the contained filesystem view: only the paths the profile grants exist for the agent (new at this stage)
  • the box CLI and Box Core, which build the box and run its trusted process (new at this stage)
  • stand-in secrets: the agent gets placeholders, the real values stay in the trusted process (new at this stage)
  • the egress gateway, which every network request from the box goes through (new at this stage)
  • the Anthropic API
  • Cloudflare's MCP server
  • the Cloudflare API
  • the sample app: the Worker box-poc-workshop with its D1 database, KV namespace and Workers Logs; the logs and the database hold attendee names and email addresses
  • Boundaries: the box, agent's sandbox, trusted process.

Nothing new is added to the picture on this page. It is about two pieces already in it: the egress gateway, which every network request from the box goes through, and the stand-in secrets, which the agent holds in place of the real values.

The egress gateway. The agent’s sandbox has no direct network access. Box sets proxy variables for the agent (HTTPS_PROXY, HTTP_PROXY, NODE_USE_ENV_PROXY=1), so its traffic goes to the egress gateway inside the trusted process. The gateway opens every connection itself. It decrypts HTTPS with a certificate authority Box makes for this box (given to the agent as SSL_CERT_FILE and NODE_EXTRA_CA_CERTS), so it can read each request. For each new connection it asks the policy engine to decide a net:connect, and for each request an http:request. It has no allow-list of its own: what the policy does not permit is refused. That is default deny.

Stand-in secrets. Box can bind a credential to a set of hosts. The real value stays in the trusted process. The agent gets a stand-in (Box’s design notes call it a phantom): a random value of the same shape, in the same variable. When the agent sends a permitted request to a bound host, the gateway replaces the stand-in with the real value on the way out. The agent can use the key. It cannot read it, print it or send it anywhere else.

These are from the Box design pages (docs/design/architecture.md, docs/design/credentials.md); the variable names were measured in this workshop’s research by running /usr/bin/env as the boxed agent.

Iteration 1The key is in your shell, not in the agent

The run. The free check from the previous page, with the profile as it stands.

What this doesStarts Claude Code in the box for one headless turn, with an invalid model key so nothing is billed.

Terminal window
ANTHROPIC_API_KEY=sk-ant-preflight-invalid-key box run ws --no-banner -- --bare -p "Say hello." \
--max-turns 1 --model claude-sonnet-5-5 < /dev/null
ANTHROPIC_API_KEY=sk-ant-preflight-invalid-key
An invalid key for this one command, so nothing is billed. Never drop it from a check without a money cap.
run ws --no-banner --
Runs the profile ws without the activity banner; everything after -- goes to Claude Code.
--bare
Claude Code’s minimal mode: your own Claude Code set-up stays out, and the key comes only from ANTHROPIC_API_KEY.
-p "Say hello."
One headless task, then exit.
--max-turns 1
Stop after one model turn.
--model claude-sonnet-5-5
The pinned model.
< /dev/null
Empty input, so Claude Code does not wait for it.

The flags are explained in full on the previous page.

What we see. The same as at the end of the last page:

What you should see: refused
Not logged in · Please run /login

Claude Code does not say why. Box can: every decision the box made is in the run’s history. First find the run.

What this doesLists the runs of profile ws, newest first.

Terminal window
box runs list ws
runs list
Prints one line per run under the header PROFILE UPDATED (UTC) BOX ID, newest first. Finished runs and running ones are both listed.
ws
Only this profile’s runs. Without it, every profile’s runs are listed.

The newest box ID is also in the box: policy history: box runs history b-... line that box run printed before the agent started.

What this doesPrints every policy decision the box made in that run.

Terminal window
box runs history <the newest BOX ID>
runs history
Reads the run’s decision history: one row per decision, with the time, the result, the action, the resource, the rule that decided and the reason.
<the newest BOX ID>
Replace with the ID, for example b-e4d0f0dc5f9bfc35. Type it without the angle brackets.

What we see (the CLI separates the columns with tabs):

What you should see: refused
TIME (UTC) RESULT ACTION RESOURCE RULE REASON
2026-11-22T08:28:58.260432000Z installed policy policy.dw - -
2026-11-22T08:28:58.712931000Z deny net:connect api.anthropic.com:443 <default-deny> no permit matched

Two lessons in two rows:

  1. The ANTHROPIC_API_KEY you exported in set-up did not reach the agent. The agent inherits nothing from your shell, so Claude Code had no key at all, and said “Not logged in”.
  2. Even with a key, Claude Code could not reach the model. The gateway refused the connection to api.anthropic.com on port 443, because no rule permits it. <default-deny> in the RULE column means exactly that.

The first row, installed policy, is the run’s policy.dw being loaded. Box writes one even though the profile names no policy file: it holds the rules Box generates from the profile (Part 3 looks inside).

Read morebox runs list and box runs historyBox docs

Read moreArchitecture: the egress gatewayBox docs

Iteration 2A route with a stand-in

What we add. A credentialed route for the Anthropic API.

$HOME/box-ws/profiles/ws/profile.toml
packs = ["@box/base"]
[project]
access = "read"
[storage]
directories = ["config", "tmp"]
[agent]
command = ["${{ home }}/.local/bin/claude", { slot = "agent-args" }]
[agent.env]
CLAUDE_CONFIG_DIR = "${{ storage.config }}"
CLAUDE_CODE_TMPDIR = "${{ storage.tmp }}"
[egress.anthropic]
destinations = ["api.anthropic.com"]
secret.ref = "env://ANTHROPIC_API_KEY"
secret.header = "x-api-key"
secret.phantom_prefix = "sk-ant-"

Each line, in order:

  • [egress.anthropic] declares a route: hosts the box may reach. anthropic is the route’s name; Box uses it in the names of the rules it writes.
  • destinations = ["api.anthropic.com"] is the host. Box writes two permits for it: route_anthropic_connect (net:connect to the host on port 443) and route_anthropic_request (every http:request to the host).
  • secret.ref = "env://ANTHROPIC_API_KEY" names where the real value comes from: the variable ANTHROPIC_API_KEY in the shell that runs box run. Box reads it once, at start. The agent then finds a stand-in in its own ANTHROPIC_API_KEY.
  • secret.header = "x-api-key" is the header the gateway writes the real key into. Anthropic’s API reads its key from x-api-key. The default is Authorization.
  • secret.phantom_prefix = "sk-ant-" makes the stand-in start like an Anthropic key: sk-ant- plus 64 random hex characters, so Claude Code accepts it as one. Without the line the stand-in starts with Box’s default prefix, strands_box_; whether Claude Code would accept that was not checked.

The run. The same free check.

What we see. Claude Code now prints:

What you should see
Invalid API key · Fix external API key

That is progress. The request reached Anthropic with the swapped key, and Anthropic refused it, because the key in your shell for this command is the invalid one. The history shows the path (time column left out):

What you should see: permitted
installed policy policy.dw -
permit net:connect api.anthropic.com:443 route_anthropic_connect
permit http:request api.anthropic.com:443/api/claude_code/settings route_anthropic_request
permit http:request api.anthropic.com:443/api/claude_code/policy_limits route_anthropic_request
permit http:request api.anthropic.com:443/api/eval/sdk-<id> route_anthropic_request
permit http:request api.anthropic.com:443/v1/messages route_anthropic_request
permit http:request api.anthropic.com:443/api/claude_code/organizations/metrics_enabled route_anthropic_request
permit http:request api.anthropic.com:443/api/event_logging/v2/batch route_anthropic_request
deny net:connect http-intake.logs.us5.datadoghq.com:443 <default-deny>

There are more net:connect rows than shown. /v1/messages is the model call. With the real key in your shell (the same command without the prefix), Claude Code answers the prompt.

One limit to know. The gateway cannot tell which process in the box sent a request, so every process in a box can use every credential route (docs/user/egress.md). A route protects the key’s value, not who may use it.

Read moreEgress and credentialsBox docs

Read moreCredentials: how stand-ins workBox docs

Read moreThe @box/anthropic packBox docs

Iteration 3What if the variable is missing

The run. The same profile, with ANTHROPIC_API_KEY removed for one command.

What this doesRuns the box with ANTHROPIC_API_KEY removed from the environment, to see what Box does without the real value.

Terminal window
env -u ANTHROPIC_API_KEY box run ws --no-banner -- --version
env -u ANTHROPIC_API_KEY
env runs the rest of the line with a changed environment; -u removes one variable. Your shell keeps it.
-- --version
Asks Claude Code only for its version: no model call is needed to see Box’s answer.

What we see (exit 1):

What you should see: refused
strands-box: error: credential setup failed: credential variable ANTHROPIC_API_KEY is not set, or holds only whitespace

Box stops before the agent starts. It does not hand the agent an empty stand-in and hope. Box printed a box ID before this error; in the check for this page, box runs list afterwards showed no saved run for the profile.

Iteration 4Quiet the agent's own traffic

The history in iteration 2 shows more than the task needs: Claude Code’s usage reporting (event_logging, metrics_enabled) and a refused Datadog host.

What we add. Two of Claude Code’s own variables.

$HOME/box-ws/profiles/ws/profile.toml
packs = ["@box/base"]
[project]
access = "read"
[storage]
directories = ["config", "tmp"]
[agent]
command = ["${{ home }}/.local/bin/claude", { slot = "agent-args" }]
[agent.env]
CLAUDE_CONFIG_DIR = "${{ storage.config }}"
CLAUDE_CODE_TMPDIR = "${{ storage.tmp }}"
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC = "1"
DISABLE_AUTOUPDATER = "1"
[egress.anthropic]
destinations = ["api.anthropic.com"]
secret.ref = "env://ANTHROPIC_API_KEY"
secret.header = "x-api-key"
secret.phantom_prefix = "sk-ant-"
  • CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC = "1" turns off Claude Code’s non-essential network traffic, such as usage reporting.
  • DISABLE_AUTOUPDATER = "1" stops Claude Code from trying to update itself from inside the box.

The run. The same free check. What we see: the history now holds only the settings, policy_limits and /v1/messages requests to api.anthropic.com, and no Datadog row.

Read moreClaude Code settings and environment variablesClaude Code docs

“Detected a custom API key”: the question Claude Code asks

Section titled ““Detected a custom API key”: the question Claude Code asks”

Every check on this page is headless. When you run Claude Code in the box interactively (act b1 does), it asks a question on its first screen:

Claude Code, interactive, in the box (line breaks approximate)
Detected a custom API key in your environment
ANTHROPIC_API_KEY: sk-ant-...1031a80502699ab53f77
Do you want to use this API key?
Yes
❯ No (recommended)
  • Why it asks. Claude Code found ANTHROPIC_API_KEY in its environment, and asks before it uses a key it did not set up itself.
  • What it shows. The last 20 characters of the key. In the box these are the end of Box’s stand-in, not of your key. The example above is from a real check; your characters will differ.
  • What to answer. Press the Up arrow, then Enter, to choose Yes. The default, No, sends Claude Code to its login flow, which cannot work here.
  • Why it asks every run. Claude Code remembers its answers in its configuration folder, which is in the box’s storage, so the theme, the notes and the folder-trust question are asked once. But the stand-in is new on every run, so to Claude Code it is a new key each time (a gap, listed in Builds and gaps).

Compare act a, without a box: there the 20 characters are the end of your real key. Do not show that screen on a projector.

The model key is one of the three secrets from set-up. The two Cloudflare tokens get the same treatment, in two places later on:

Secret Who uses it Where it is declared Page
CLOUDFLARE_MCP_TOKEN Claude Code, to talk to Cloudflare’s MCP server in the [mcp.cloudflare] table, with the same secret.ref = "env://..." line MCP through the broker
CLOUDFLARE_API_TOKEN the scripts of the local exec proxy, calling the Cloudflare API directly in a route [egress.cloudflare-api] Network rules for each API request

Why the MCP token does not get a plain route here: a route would only open the host. The [mcp.cloudflare] table also makes Box recognise the traffic as MCP and decide each tool call, which is the next page. Without a phantom_prefix, a Cloudflare stand-in starts with Box’s default prefix: strands_box_ plus 64 hex characters (measured in this workshop’s research).

Both Cloudflare tokens are deliberately broad: they can read the leaderboard app’s logs and delete its Worker. So when something is refused later, the refusal is Box’s, not Cloudflare’s.

Security engineerWhat can the policy see, decide and record?

The real model key is out of the agent’s reach, and bound to one host. Every connection the agent makes is now a decision you can read with box runs history, including the ones nothing permits. What a route does not give you: any say over which paths on the host are used. The route admits all of api.anthropic.com.

DeveloperWhat can the agent still do, and what does it cost?

Claude Code reaches its model, with the key you already have in your shell. Nothing about how you talk to it changes, except one extra Yes on the key question in interactive runs. The two variables in iteration 4 cost nothing.

  • The agent has no network of its own. Every request goes through the egress gateway, which asks the policy engine about each connection and each request. Nothing permitted means refused.
  • A route ([egress.<name>]) opens hosts. With secret.* it also binds a credential: the agent holds a stand-in, the gateway swaps in the real value for those hosts only.
  • box runs list <profile> finds a run; box runs history <box ID> shows what Box decided in it, and which rule decided.
  • A missing secret variable stops the run before the agent starts.

Read moreEgress and credentialsBox docs

Read moreCredentials designBox docs

Read moreWhat Box protects, and its limitsBox docs