Code has become the universal tool for AI agents. Instead of building a custom tool for every task, you give an agent a shell so it can write and run its own scripts. That leaves you with two distinct parts: the brain is the loop that calls the model, and the hands are the shell and workspace where commands run and files live.
Giving an agent a shell makes it far more capable, and also dangerous. Anything the agent reads (a webpage, a file, or a tool output) can talk it into running commands you never approved. A system prompt won't reliably stop that, because a prompt is a set of instructions, not a security boundary. Real safety has to live in the infrastructure, starting with locking those hands inside an isolated sandbox.
Once you draw that boundary around the hands, one architecture question shapes your design: should the agent loop run within the sandbox or outside of it? In other words, should the brain stay outside the sandbox, or live inside with its hands? On request-based runtimes like Cloud Run, the brain runs outside, so your orchestration stays separate from untrusted code and scales on its own, with tool calls reaching a remote sandbox over the network. On stateful sandbox runtimes like Agent Substrate 1.0, which runs on Kubernetes and scales to millions without idle VMs, brain and hands share one sandbox, so tool calls stay local without giving up isolation. Choosing between them comes down to whether your orchestration needs a life of its own.
Coupled vs. decoupled harnesses
To answer that, start with how a harness runs before you split anything. Alongside the brain and hands, any multi-turn agent keeps a third piece, session state (its conversation history and event log). On your laptop, all three live on one machine, and one turn passes between them in three steps:
- You send a task. The brain loads the session history and sends it to the model with your new message.
- The model answers with a tool call, such as a shell command to run or a file to edit. The brain passes the call to the hands on the same machine, and the hands run the command, change files, or start a background process.
- The output comes straight back to the brain. The brain appends the tool call and its result to the session, then calls the model again.
Steps 2 and 3 repeat until the model returns a final answer. Every hand-off is local, so only the model call leaves the machine.
Figure 1. On your laptop, brain, hands, and session share one machine, so only model calls leave it.
In production, those same three steps run on every turn, and you can build them with two different architectures:
- Coupled (one machine per agent): you keep the laptop model and give each agent its own isolated box in the cloud. Brain and hands stay inside the same sandbox, so every tool call is local. When the agent waits, a stateful sandbox runtime like Agent Substrate 1.0 (which calls that box an actor) snapshots its memory and files and wakes it on the next message.
- Decoupled (a microservice architecture): you split the harness into a web service, a database, and a remote sandbox. The brain runs on a request-based runtime like Cloud Run (where any running copy of your service can answer the next request), sessions live in an external database, and every command to the hands crosses the network.
At a high level, the distinction is whether you treat each agent as its own suspendable machine, or run the brain as a web service that drives remote hands. Think of a coupled harness a bit like a workshop with a desk inside it, where you lock the door when you step away and find your tools where you left them, and a decoupled harness like a dispatch desk that calls out to separate workshops only when a job needs tools. Every trade-off that follows comes from that choice.
Pros and cons of decoupling
We'll start with the decoupled layout, where the brain runs outside the sandbox on a request-based runtime like Cloud Run.
Figure 2. In a decoupled harness, the brain on a request-based runtime (such as Cloud Run) reaches its hands and session state over the network.
In Figure 2, each of the three laptop steps now crosses a network:
- Your task arrives at the brain over HTTPS requests.
- The brain sends each command as a remote tool call to the sandbox and waits for the output.
- The brain writes each tool call and result to the database (save state) so any instance can load it next.
Pros
On a request-based runtime like Cloud Run, decoupling gives you five strengths:
- Guardrails, approvals, and core credentials sit out of reach of generated code. Tool allowlists, spend caps, approval gates, and the audit log run in the brain's process, where an injected shell command can't tamper with them. Model and platform keys stay there too. The remote sandbox can still block outbound traffic by default, and an egress gateway (its single exit) adds tool credentials outside it.
- Tools that don't need a sandbox run on the brain, with their own path to the outside world. You can give the agent web search or internal company APIs there and keep the sandbox locked down.
- Waiting on the model is cheap, and plain chat or API turns never wake a sandbox. A single brain instance can hold many slow model calls at once (Cloud Run, for example, allows up to 1,000 concurrent requests per instance), while turns that skip shell or file tools never touch a sandbox.
- One brain can drive many disposable sandboxes, each sized for its job. You can run subagents in parallel, give a heavy tool more CPU, memory, or a GPU, and throw away a compromised sandbox without losing the session.
- Sessions live in an external database, and a brain upgrade is a rolling deploy. Offline evaluations and background jobs read that database without waking a sandbox, and a new brain version ships with traffic splitting and rollback.
Cons
Decoupling costs you four things:
- Two sources of truth can drift apart, because the session database and the sandbox's files are never saved in one step. After a crash during a tool call or a recycled sandbox, you need idempotent tools (safe to run twice) or a step that reconciles the two.
- Remote tools add latency, and a laptop harness needs changes before it runs here. Hundreds of file reads become hundreds of network round trips, and interactive terminals need a custom remote protocol.
- One brain process serves every user. It reads untrusted tool output while it holds core credentials, so brain-side tools need tight per-user authorization and allowlists.
- Long turns need their own durability and deploy handling. Request-based runtimes are built for requests that start and finish, and on Cloud Run a rollout sends
SIGTERM(a shutdown signal) with a 10-second grace period.
Example: Long Horizon on Cloud Run
The public Long Horizon recipe in google/adk-recipes is a decoupled harness on Cloud Run built for long turns:
- The brain is a FastAPI web app with an Agent Development Kit (ADK) runner on Cloud Run.
- Sessions sit in Cloud SQL, a managed database, through ADK's
DatabaseSessionServicein the recipe's Terraform deployment. - Long-term memory across conversations lives in Memory Bank, where a nightly job consolidates it without waking any sandbox.
- The hands run in one sandbox per person, built from the recipe's own container image.
Picture a long turn on a default Cloud Run service before those settings are tuned. A person starts a task in their browser, and it runs past Cloud Run's default request timeout of 5 minutes, so Cloud Run closes the stream with an HTTP 504 error. If the person closes the tab while the agent keeps working, the turn has no open request attached. When they reconnect later, any instance can answer that new request, so the client has to find the work still in flight and catch up on missed events.
The table puts that long turn beside what each runtime category does.
| Request-based runtime (e.g. Cloud Run) | Long-horizon turn | Stateful actor (e.g. Agent Substrate) |
|---|---|---|
| Requests finish within a configured timeout (5 minutes by default, up to 60 on Cloud Run). | It keeps running after its connection closes. | The turn runs as a process inside the actor, with no single HTTP request to outlive. |
| Any instance can answer the next request, and session affinity is best effort. | A returning client has to find the work still in flight. | Every request names its actor, so a reconnect goes back to it. |
| State lives in an external database. | It builds up working memory, files, and running processes. | A suspend saves memory, files, and processes, and a wake restores them. |
Cloud Run settings for hour-long turns
To keep a long turn running across timeouts and disconnects, Long Horizon gives Cloud Run four settings in Terraform (its infrastructure as code). List them from a google/adk-recipes clone:
$ grep -nE "^ +(timeout|session_affinity|min_instance_count|cpu_idle)" \ core/python/long-horizon-harness/terraform/cloud_run.tf 18: timeout = "3600s" 22: session_affinity = true 37: min_instance_count = 1 64: cpu_idle = false Each setting protects one moment of that long turn:
-
timeout = "3600s"raises the limit from 5 minutes to the 60-minute maximum, so the stream stays open mid-task. -
session_affinity = truesteers a reconnecting client back to the instance running its turn. -
min_instance_count = 1keeps one warm instance up when traffic is light, so the service doesn't scale to zero mid-turn. -
cpu_idle = falseleaves CPU allocated after the tab closes, so the turn keeps running.
Session affinity is best effort, so Long Horizon also writes task events to a DatabaseTaskStore on Cloud SQL. A returning client reads any missed events from that store, whichever instance answers.
Pros and cons of keeping them coupled
Now flip the design around and move the brain back inside the sandbox. A coupled harness runs on a stateful sandbox runtime, which separates each workspace from the machine under it. For example, Agent Substrate 1.0 suspends an idle actor to local disk and Cloud Storage and wakes it in under a second.
Figure 3. Brain and hands share one actor, and the egress gateway adds keys outside it.
Pros
Keeping brain and hands in one box gives you four strengths:
- Tool calls stay local, and development matches production. The container from your laptop runs in production without SDK changes, with ADK, Google Antigravity, or any other agent framework or harness. Dev servers, interactive terminals, and language servers stay alive across turns.
- Working memory, files, and running processes are saved together in one atomic snapshot, so they can't drift apart. On Agent Substrate 1.0, you can rewind a crashed actor to its last snapshot, or fork a snapshot to branch a whole agent and its workspace for evaluations or bug reproduction.
- Each person's brain is isolated too, in its own gVisor sandbox or Cloud Hypervisor microVM (a lightweight virtual machine). Keys stay outside here too, because outbound traffic is blocked by default and the egress gateway adds model and tool credentials on the way out.
- Waiting between turns and at human approval gates is cheap. A suspended actor frees its slot on a warm worker, so hours of waiting on a person cost only storage. A new actor starts from a golden snapshot (the pre-initialized state of your harness).
Let's trace one turn across Figure 3, from left to right:
- Your app sends the message to the router and names the actor, so the router delivers it to that person's sandbox.
- Inside the actor, brain and hands run the laptop loop, and calls to the model or a tool's API leave through the egress gateway.
- The person closes the tab, and the turn keeps running as a process inside the actor. A reconnect names the same actor, so the router sends it back there.
- Once the turn ends, your app calls
SuspendActor, and the actor's memory and files go down the "suspend" arrow to Cloud Storage.
Cons
You might ask whether local tool calls and sub-second wakes make one box the right choice for every agent, and they don't, because one box costs you five things:
- The harness shares its box with generated code. Keys stay behind the egress gateway, but an injected command can still reach harness files, prompts, and local logs. Put hard gates outside the box (egress policy and approvals in your app), and stream events to an external append-only log.
- You pay for the full sandbox while the model thinks, because an active actor holds its worker slot and RAM until the model's stream finishes. One box wins between turns, and a decoupled brain wins during slow model calls.
- Upgrading a sleeping fleet means cold boots. On Agent Substrate 1.0, a suspended actor that moves to a new template (its image and compute settings) keeps only the data on its volumes, so the next wake boots fresh. Keep durable state on volumes and build the harness to restart cleanly.
- Brain and hands share one compute shape and serve one session at a time. You can't size a lightweight loop separately from a memory-hungry or GPU-heavy tool (Agent Substrate 1.0, for example, has no GPU passthrough), and each actor serves one person rather than pooling many shared callers behind one API.
- Suspending drops live connections, and your app owns suspend timing. Pausing a box closes open client streams, database pools, and Model Context Protocol (MCP) sessions, so clients and harnesses must reconnect on wake. Your app also decides when it is safe to suspend so an unattended turn isn't paused mid-work.
How to choose: brain only, one box, or decoupled
With both layouts on the table, how do you pick one? First check whether your agent needs hands at all, which gives you the three shapes in Figure 4.
Figure 4. Two questions sort an agent into brain only on a request-based runtime, a decoupled harness, or one box on a stateful sandbox runtime.
Question 1 asks whether your agent needs files or processes. If its tools are plain API calls and its state lives in a database, run the brain alone on a request-based runtime like Cloud Run. If it runs a shell, writes files, or keeps processes alive between tool calls, move on to question 2.
Question 2 asks whether any of these four signs is true for your agent:
- Most of your turns skip sandbox tools, so a decoupled brain answers plain chat and API calls without waking the sandbox.
- Your sessions and memory must stay readable, searchable, or available to background jobs without waking every sandbox, as with Long Horizon's nightly memory consolidation.
- Many clients or channels share one stateless service API, as Long Horizon's brain does for its web frontend, a scheduler, and other agents.
- Your guardrails, approvals, or brain-side tools need their own trust boundary or release cycle, as when the brain runs web search and the sandbox stays locked down.
Sign 1 is really about idle time, and agents have two kinds of it. While the model thinks, a decoupled brain costs less, because only the small brain service is working and the full sandbox does nothing. Between turns and while a turn waits for approval, one box costs less, because the whole agent suspends in one piece.
So, should the brain stay outside the sandbox or live inside with its hands? If at least one sign is true, your orchestration needs a life of its own. Keep the brain outside on a request-based runtime like Cloud Run and have it drive a remote sandbox, as Long Horizon does. If no sign is true, put the brain inside with its hands on a stateful sandbox runtime like Agent Substrate 1.0. For long-horizon harnesses that live in a workspace, this gives you far fewer moving parts in your application code. Tool calls stay local, one atomic snapshot saves the whole agent, and you have no timeout or task-store plumbing to maintain.
What to take away
- Match the runtime to the workspace. Skip the sandbox on a request-based runtime like Cloud Run for API-only agents, keep brain and hands coupled in one box for workspace-heavy long-horizon harnesses, and decouple when your orchestration needs a life of its own.
- Decide where your guardrails live, because a decoupled brain keeps approvals, spend caps, and logs away from generated code, while one box needs those gates outside the sandbox.
- Count two kinds of idle time, since a decoupled brain costs less while the model thinks and one box costs less between turns.
- Plan upgrades and state consistency up front, with idempotent tools for a decoupled harness and durable state on volumes for one box.
Try it
- To try a decoupled harness on Cloud Run, start with the Long Horizon recipe in
google/adk-recipes. - To try one box per agent, start with Agent Substrate 1.0 (open source under Apache 2.0 in the Cloud Native Computing Foundation Sandbox), read the previous post on how actors suspend and wake, or follow the Google Kubernetes Engine (GKE) install guide.




