• Docs
  • Talk to an expert
Blog
Blog
BlogProductCase studiesNewsInsights
Blog

AI agent sandbox vs production environment: where should agents run?

cloud application platformclouddeploymentAI Agentsdatapreview environments
16 November 2026
Share
This post is also available in German and in French.

Note: pricing and features may have changed since publication. Check each provider’s documentation for current details.

Where an AI agent should run depends on what the agent does.

  • An agent that runs code it wrote, or code from users, belongs in a sandbox: an isolated, short-lived machine with no access to production systems.
  • An agent that reads or changes application data should be tested in a production-like environment first, then run in production with only the permissions its task needs.

Many teams need both: a sandbox for running code, and a production-like environment for testing agents against real services before they go live.

What is an AI agent sandbox, and how is it different from production?

  • A sandbox is an isolated, short-lived environment for running code the agent writes or downloads. Dedicated sandbox providers such as E2B and Vercel Sandbox run each sandbox in its own Firecracker microVM with its own kernel. A sandbox starts without access to the application's database and can restrict which internet addresses the agent reaches.
  • A production environment is the live application, with real users, real data, and real credentials. Anything an agent does there has real effects.
  • A production-like environment (often called a preview or staging environment) runs the same application, services, and configuration as production, but separately from the live system. Mistakes there do not reach users as long as it uses its own credentials.

How do a sandbox, a production-like environment, and production compare?

Feature

Sandbox

Production-like environment

Production

Main purposeRun untrusted codeTest agents against real servicesServe real users
IsolationOften a microVM per sessionSeparate copy of the app and servicesShared with the live app
DataEmpty unless you add itTest data or a copyLive data
Cost of a mistakeA discarded sandboxA discarded test environmentReal users and data affected
Typical agentsCode interpreters, coding agentsAgents being built or changedApproved agents with limited access

What can go wrong when an agent runs in production?

The OWASP Top 10 for LLM Applications 2026 names two risks that matter most for agents. Prompt injection (LLM01) means instructions hidden in content the agent reads, such as an email, a support ticket, or a web page, can change what the agent does. Excessive agency (LLM03) means an agent has more functions, permissions, or autonomy than its task requires, so a mistaken or manipulated output turns into a damaging action.

In practice, the two combine into four kinds of failure:

  • Destructive changes: an agent with write access to a production database can change or delete records.
  • Data leaks: an agent that can reach any internet address can send data out of the system.
  • Exposed credentials: long-lived API keys in the agent's environment can be read and reused.
  • Runaway cost: an agent stuck in a loop can keep calling paid model APIs and tools. OWASP lists this as unbounded consumption (LLM06).

When should an agent run in a sandbox?

Typical cases include code interpreters, analysis of files that users upload, coding agents that install packages and run tests, and browser automation on unknown sites. In each case, the code itself is the risk, so the agent needs a boundary that holds even if the code is malicious.

Dedicated sandbox providers give each session its own microVM:

  • E2B runs every sandbox in its own Firecracker microVM, with sessions of up to 1 hour on Hobby and 24 hours on Pro.
  • Vercel Sandbox also uses Firecracker microVMs, and its sandbox firewall can block outbound traffic and add API keys to outgoing requests, so secrets never sit inside the sandbox.
  • AWS Lambda MicroVMs, launched in June 2026, give VM-level isolation for up to 8 hours per session.

The trade-off is that a sandbox starts without the application's data. The agent can only work with what it is given, which is the point for untrusted code, but a limit for agents whose job is to work with application data.

Upsun Cloud does not offer a microVM sandbox. Its task containers use the same container isolation as applications, and we suggest running untrusted code inside bubblewrap within the task. For VM-level isolation, a task can call a sandbox provider.

When should an agent run in a production-like environment?

A sandbox shows whether an agent's code runs. It does not show how the agent behaves against the real database schema, the real services, and realistic data. A production-like environment does, because it runs the same application and services as production without touching live users.

Use one whenever something about the agent changes:

  • a new agent, or a new task for an existing one
  • a new prompt, model, or tool
  • a change to the data or services the agent can reach

To keep the test environment safe:

  • Use separate credentials. Give the test environment its own API keys, ideally test keys for payment and email providers.
  • Turn off real side effects. Stop outgoing emails, payments, and messages, or point them at test endpoints.
  • Decide what data to use. If the environment holds a copy of production data, remove personal data before people or agents work with it.
  • Review what the agent did. Check its logs and the changes it made before promoting it to production.

How do you limit what an agent can do in production?

OWASP's guidance on excessive agency comes down to one idea: do not rely on the model to behave. Put the limits in the systems around it.

  1. Limit data access. Connect the agent only to the services it needs. Where possible, use a database user with read-only access or access to specific tables.
  2. Use short-lived credentials. Prefer tokens that expire in minutes over API keys that last for months.
  3. Restrict outbound traffic. Allow connections only to the APIs the agent needs, such as the model provider and one internal service.
  4. Ask a person to approve high-impact actions. Payments, deletions, and messages to customers should wait for approval.
  5. Cap run time and spend. Set a timeout close to the expected run length and a budget for model calls.
  6. Log every run. Record what started the run, what the agent did, how long it took, and how many tokens it used.

How do you run this pattern on Upsun Cloud?

On Upsun Cloud, an agent runs as a task container, is tested in a branch environment, and reaches production with a Git merge, using the same configuration in both places.

1. Define the agent as a task

A task container is declared under tasks: in .upsun/config.yaml. Its relationships list the only services it can reach and run.timeout stops runs that go on too long.

tasks:
  ticket-triage:
    type: "python:3.14"
    hooks:
      build: "pip install -r requirements.txt"
    relationships:
      database: {}
    run:
      command: "python agent/triage.py"
      timeout: 1800

 
services:
  database:
    type: "postgresql:18"

Each run is recorded as an activity with an ID, a status, and logs. Write the agent as a single idempotent script, so a run can be started again safely if it fails or a deploy stops it.

2. Test it in a branch environment

Each active branch environment runs the apps, services, and tasks defined in that branch’s .upsun/config.yaml, in its own containers, separate from production.

3. Keep production keys out of branches

Set the production key on the production environment only, and mark it as non-inheritable so branch environments never receive it. Give each test environment its own key. 

# Production key: stays on production, not passed to branch environments
upsun variable:create --level environment -e <production-environment> --name env:MODEL_API_KEY --value "<production key>" --sensitive true --visible-build false --inheritable false
# Test key: set on each environment where the agent runs tests
upsun variable:create --level environment -e <test-environment> --name env:MODEL_API_KEY --value "<test key>" --sensitive true --visible-build false

For every option, see Set variables.

4. Restrict who and what can act

  • People: Upsun roles are set per environment type, so a developer can be a contributor on development environments and a viewer on production.
  • The application: workload authorizations let an app start a specific task through the Upsun API, using a token that lasts 60 seconds by default and up to 900 seconds.
  • Outbound traffic: Upsun Cloud apps can define an outbound firewall allow list. Task containers don’t support outbound restrictions yet, so keep the agent’s credentials narrow and run untrusted code inside bubblewrap or with a sandbox provider.
  • Untrusted code: run it inside bubblewrap within the task, or send it to a sandbox provider.

5. Add an approval step for high-impact actions

Tasks cannot pause while they wait for a person. The agent saves its proposed action to a database and exits. When someone approves it, the app starts a follow-up task that carries out the action.

6. Merge to deploy

Merging redeploys production, which stops any task already running there, so the agent should save progress and exit cleanly when it receives SIGTERM. 
 

Frequently asked questions

Is a staging environment the same as a sandbox?

No. A sandbox isolates untrusted code from everything, including the application. A staging or preview environment is a separate copy of the application and its services, used to test changes before they reach production. An agent can need both.

Should coding agents run in a sandbox?

The code they write and the tests they run should, because that code has not been reviewed. Their changes should reach production only through review. Upsun Dispatch, a separate Upsun product for coding agents, opens a pull request for each change, and a person owns the merge. 

Can Upsun Cloud run untrusted code?

Upsun Cloud task containers use container isolation: namespaces, dropped capabilities, seccomp, cgroups, and network isolation. The Upsun docs recommend running untrusted code inside bubblewrap within the task. For VM-level isolation per run, use a dedicated sandbox provider.

Does an Upsun Cloud branch environment contain production data?

A new environment can clone its parent's data, including databases and files, when it is created, and the push option environment.clone_parent_on_create=False turns this off. Teams that copy production data define their own sanitization script in a deploy hook.

Stay updated

Subscribe to our monthly newsletter for the latest updates and news.

Deploy possibility.
Try Upsun for free.

Build with DispatchDeploy on Cloud