Building agents · Architecture · 7 min read
How I build agents, and the harness that keeps them in bounds
I build production agents and the harness around them. Subagents get a context of their own, nothing the model must not hold comes within its reach, memory has a budget, and evals decide which changes stay.
- Delegation
- Specialists · forks with a clean context
- Containment
- Key outside the sandbox · egress fails closed
- Evals
- Held-out cases · pass^k
Contents
These patterns come from two systems I run. One is an internal agent that engineers reach from their team chat, with company accounts and roles, next to the company's production. The other is FlyWorkers, my own product, where agents work unattended for customers, run whatever code they decide to run and read pages written by strangers. Most of what follows holds for both. Where it doesn't, the difference explains the design.
Delegation
Two ways to delegate
An agent that carries every tool and every instruction gets worse at each job as the list grows. Delegation splits the work, and it comes in two shapes that solve different problems.
Specialists. An orchestrator receives the request and hands specialized work to subagents, each with its own prompt, its own tools and its own evals: test automation, triage of support requests and crash alerts, SRE support during an incident. A narrower tool set is also a smaller blast radius, and each specialist's score moves on its own. The orchestrator I run this way is built with the Strands Agents SDK on AWS Bedrock AgentCore. In front of it, a gateway that holds no model receives the chat event and picks the system prompt for the channel, so I can try a prompt change with one HTTP call and no chat at all. Not every job needs the orchestrator, either. When a test management service already knows the scenario, phone and build for a job, it calls the test automation specialist directly, since a general agent in that path would only add a model call.
Forks with a clean context. Here an agent delegates to itself. A delegate_task tool runs a child turn in the same harness process, with the same identity, sandbox and key. The child inherits the parent's system prompt plus a short section at the end that says it is a subagent; at the end, so the parent's prompt stays an exact prefix and the prompt cache still applies. It gets the task and whatever context the parent writes, never the parent's conversation, and it cannot delegate again. What the fork buys is context: reading ten files to answer a one-line question doesn't fill the parent's.
Forks are where spend gets away. My first version gave a child at most half of what the parent had left, with no floor. That half could be almost nothing, and each child still paid for a full iteration before the cap stopped it. Measured, a turn with four subagents spent 7.8 times its ceiling. Now a child runs only if its half covers what the parent's last iteration cost, and the bound is one the design can promise: one extra iteration for the parent, plus one per child in flight.
Two ways to delegate
Specialists with their own tools, or a fork with a clean context
Boundaries
Containment: what the agent can reach
An agent that can run code will, sooner or later, run code it shouldn't. So I design what it can reach rather than what it is told: the model's credentials, the integrations' tokens, the network, other tenants and production.
Credentials stay outside the sandbox. Each agent works in a sandbox container of its own. The harness, pi embedded through its SDK behind an interface of my own, runs beside it in the same VM and holds the model key in RAM for the length of a turn, never on disk. Every tool the agent calls runs inside the sandbox through docker exec, so the agent can run anything there and still has no way to read the key: the process that holds it isn't there. In the first plan the harness ran inside the sandbox, where an agent with a shell could read the memory of the process next to it. A review caught it before any code existed, and I didn't build on the new topology until a spike proved it. A sentinel shaped like an API key went into the harness's environment, and 33 assertions passed: it was absent from the sandbox's environment, from every process's environment and command line, from the container's configuration and from every file in its root filesystem, and the sandbox couldn't reach the harness by any route tried. A positive control made sure a sentinel that was never exported couldn't pass for the wrong reason.
Integrations follow the same line. The harness calls a remote MCP server itself, with the token from the agent's vault in a header, so the token never enters the container. A local MCP server, like Playwright's test server, has to run next to the code it works on, so it runs inside the sandbox as one of the agent's own processes, never in the harness beside the key.
The network fails closed. Traffic leaves the sandbox only through an egress proxy, and a proxy that doesn't know its policy answers 503 to everything. A per-agent domain allowlist was the default until I opened egress, because an agent meant to work like a person at a screen can't ask for permission per domain; the allowlist stayed as an option. What stays closed is the infrastructure underneath: private ranges, loopback and the cloud's metadata endpoint are refused even with egress open.
Without a sandbox, the bounds go into the tools. An agent that serves colleagues instead of running untrusted code still needs limits, and there they are code around the model. Tools are typed, and some are served by MCP servers I built behind company SSO (OIDC). Each action is checked against the engineer's role, and the infrastructure tools are read-only and refuse production before they call anything.
Containment by design
What an agent can reach inside one customer's VM, and what it can't
Memory
Memory with a budget
Whatever enters the context is paid for again on every turn after it, so memory enters when the agent asks for it, in bounded amounts.
Within a conversation, the thread is the memory: a follow-up keeps what came before, and documentation enters only through a search, over pgvector.
Across conversations, I don't rely on the model to take notes. An agent told in its prompt to write them before ending a turn didn't, in production, and its memory volume stayed empty after turns with real work. So the harness writes a session journal itself after every turn, into the agent's own volume: the user's last message, the final answer and one line per tool call, each with a size cap, with caps on each file and on the whole directory. The journal never enters the prompt on its own. The agent searches it with recall, a literal-text search that runs inside the sandbox, and the matches come back marked as untrusted text. After a turn that used tools, the harness also asks the agent whether it learned something it couldn't work out again, and gives it up to three tool calls to save it, inside the turn's spend.
Skills follow the same rule. The prompt carries only an index of them, with name, description and path, never their bodies, and the agent opens a skill when the work calls for it.
Evals
Evals decide what stays
A prompt edit that fixes one transcript can quietly break three others, so a change to a prompt, a tool, the harness or a playbook doesn't stay because it reads better. It stays if it scores higher.
The cases come from real tasks and from production failures, and each failure becomes a new case. Deterministic checks grade what can be checked exactly, and an LLM-as-judge grades the rest, calibrated against grades a person gave. A case counts only if all k runs pass (pass^k), since an agent that answers a support thread correctly most of the time still gets some threads wrong. And a change has to win on cases held out from tuning, because a prompt tuned on the cases it is measured on learns those cases.
For a new agent role, the evals come before the prompt. They are iterated against until the score stops rising, and every iteration is recorded with its transcript, so a regression shows up as a diff of scores. A Quality Engineer role has twelve cases, from a credential pasted in the chat to production offered as the test environment and a sleep proposed as the fix for a flaky test. It reached 12 of 12, then 11 of 12 twice with the failure moving between soft criteria, which is the model and the judge varying, not a rule left to write.
Safety properties aren't left to evals. They are tested as code, and the tests are checked by mutation: removing the proxy's fail-closed default turns a test red.
Retrospective
What I got wrong about tenant isolation
I chose one VM per customer so that an agent escaping its container would reach only agents of the same customer. The threat model said so: a compromised harness meant root on that customer's VM, and nothing across tenants.
Reading the deployment against that sentence showed it was false on the network. Every customer VM was an app in the same Fly organization, Fly's private network spans the whole organization, and the harness listened on that network with no authentication. The egress proxy blocked those addresses from inside the sandbox. But the sandbox runs on runc, and an agent that escaped its container in customer A's VM could reach customer B's harness with a single HTTP request. From there it could cancel B's turns, watch B's live browser view, or run turns on B's VM with B's memory mounted. The VMs separated the kernels, and nothing separated the harnesses.
The fix is a random 32-byte token per customer. The provisioner generates it when it creates the VM and stores it as a secret of that VM's app before the machine starts. The VM moves it to a file only its owner can read and removes it from the environment. The harness refuses to start without it and requires it on every route except the health check, compared in constant time. The control plane sends it from a single function on all seven paths it uses to reach a harness. An escape in A's VM now yields A's token, which works only in A.
I considered a private network per customer, but the control plane has to reach every VM and a machine belongs to only one network. Mutual TLS would also authenticate the control plane to the VM, at the price of a certificate authority and certificates for every VM; it stays the option if a second legitimate caller ever appears. One token shared by every VM would close the path from the sandbox and leave the path between customers open.
The mistake was treating a deployment decision as a security property without checking the deployment. The threat model now names what separates customers on the network, and removing the check turns tests red.
Related
Two specialists in detail
Agentic test automation covers the test automation specialist: how a scenario written in plain language becomes reviewed test code on a real phone, and why every run after that is plain code.
Triage agent covers the triage and SRE specialist: how a crash alert or a support request gets a first answer in its thread, built from the company's telemetry and the errors the team has already diagnosed.
Next system
Who writes the end-to-end tests?
An engineer describes a flow in plain language. The agent drives a real phone or a browser and writes the test. After that, the test replays the same steps every time, with no model in the run.
Device layerContact