Hexagonal Architecture and Agent Sandboxes

We used to say Dev should resemble Production (in configuration, if not scale) as closely as possible. However we're now adding completely agentic workflows to our development pipeline. Does the Dev environment still cut it? In my opinion the answer is no. The Dev environment probably needs half a dozen or so credentials / parameters to function correctly. These might be ephemeral AWS credentials provisioned to a container, access keys for APIs, or JWKS endpoints for your auth service.
Giving an agent these credentials is a twofold risk:
- An agent with access to real resources can take real actions, which you will really be billed for.
- Agents consuming shared resources will quickly start stepping on each other's toes when given overlapping or competing goals.
For point one, having access to real resources also leads to potential triggering of the lethal trifecta. There may be untrusted input in those external resources, the resources may present a potential exfiltration path, and there may (although given this is a development environment hopefully not) be sensitive data in the resources.
For point two shared resources are all well and good when our goals are independent, however as software becomes cheap to produce it may make sense to try a few or more different, incompatible, patterns to evaluate which one best solves our problem. There's been some great research by Anthropic on multiple agents using shared resources with incompatible goals where they become adversarial. It is definitely worth a read.
The Ephemeral Environment
So assume we've done everything right, we've removed sensitive credentials from our agent, we've added allowlists for the domains it's allowed to contact, and created a limited blast radius if it does something unexpected whilst unsupervised. Without any extra effort, the only thing we have is a codebase that an agent can edit, push into a PR, and then needs to be tested and validated.
This is all well and good but it is very much an open loop, the agent can check the tests pass and the code compiles but it doesn't have any validation loop it can use to check that it met the criteria it was assigned in the prompt or specification.
This is where hexagonal architecture comes into play. As an example for a personal project I have a service that needs to communicate with S3 and launch tasks on AWS Batch. Giving an agent access to storage and compute is a sure fire way to spin up a large AWS bill and potentially exfiltrate data.
Instead what we can do is to build these additional providers as hexagonal ports and adapters. The S3 adapter gets swapped out for a Docker volume adapter backed by file system operations, and the Batch adapter gets swapped out for a spawned subprocess adapter. Both things you'd never do in production, but give you a simulacrum in an ephemeral environment. The ephemeral environment validates business logic against the spec.
Add in a local database and some emulators and all of this allows the agent to test its changes in isolation before it's pushed to the Dev environment.
Key to all of this is plugging in tools that allow the agent to directly test the changes it's making against the specification. This might mean installing a headless browser and allowing the agent to navigate through the app, or connecting the agent up to a locally running version of your MCP server so it can run tests.
The same environment works for humans too. The agent's changes can be pulled and run in a sandbox to be verified if required. This forms a solid credential boundary where agents operate and rapidly iterate on a specification, whilst shared and credentialed resources are saved until the agents code goes through more rigorous checks, such as CI checks, SAST, and agentic or human review.
Now that each agent has its own ephemeral environment, the issue we mentioned earlier about the agents stepping on each others toes is mitigated. They are free to pursue competing goals, they no longer interfere while they work; approaches only meet at review time.
The Other Advantages
When switching to hexagonal architecture with agentic workflows we also see some other positives. These results are from personal experience, so feel free to disagree.
- Our environment now separates business logic from external adapters. Before, AI would have happily written an extensive mock for our external dependency. This would lock us into technology and API choices. Now we can test our business logic in isolation with mocks that implement our ports. This results in significantly less AI generated test code whilst retaining coverage.
- AI writes well when there's a predictable pattern. By encapsulating what is external to the system we make the actual process followed to perform a piece of business logic easily understandable. Agents can separately reason about the internal logic and the external effects it causes, coupled only through a shared interface.
- As long as you stick to your ports, it makes it very easy to change providers for external components. These migrations also become much easier with AI if it has a predefined behaviour it needs to fulfill.
Gotchas
- Adapter selection should be compiled in, there should be no code path in production that allows for selection of less secure or performant options. Ideally this should be an opt-in, with the agent passing the flag locally, for example a Cargo feature called
local, when building the application for local testing. Release builds on CI should fail if thelocalfeature is enabled. - I'd recommend against making a separate auth port with different security guarantees. You can insert locally generated JWKS into the real auth logic so the agent can mint its own credentials and test the application without needing to have access to your authentication provider.
- Building in an ephemeral environment with different adapters does mean you won't catch platform quirks. It will however test your business logic and allow you to test against the spec without handing over credentials. Adapter fidelity gets tested in a deployed environment.
Summary
Keep your agents environments clean, give them good guardrails and self contained operating environments. This will allow you greater confidence in running these agents unsupervised. Keep a credential boundary between verified and unverified code, real credentials live after the merged PR, and the agent can iterate freely in the interim. Lastly, give your agents the tools to interrogate the functionality of your application, not just the codebase, to close the loop on the spec.
About the author
- I have a diverse industry background ranging from startup scale embedded IOT and Industry 4.0 through to enterprise scale Edutech companies and utility providers. My expertise lies in cloud native, secure software development, with a keen interest in exploring emerging technologies.
Comments
Join the discussionTreating shared Dev environments as unsafe once agents hold real API credentials is spot on. Using ports-and-adapters to swap out external billing or auth adapters inside ephemeral sandboxes stops agents from stepping on each other or triggering unwanted side effects.



