Skip to content
Skip to Content
Coder Docs

Sandbox agents

Run AI agents in workspaces with fast startup and kernel, network, and secret isolation.

On this page

The term "sandboxing" has become popular in the era of agentic coding to describe secure, isolated environments for AI agents. Common traits of a sandbox include:

  • Kernel isolation: agents run inside their own kernel, which reduces the risk of an escape to the host.
  • Network isolation: agents run with no (or limited) network access, which reduces the risk of data loss, a compromised supply chain, or prompt injection.
  • Secrets isolation: agents run with isolated or scoped-down secrets and identity, compared to the user, which reduces the blast radius.

Additionally, sandboxes are often seen as faster and lighter weight than traditional compute, quickly provisioning for agentic tasks. Coder workspaces can have every trait of a sandbox, including fast startup, and Coder supports the wider sandbox ecosystem. Each trait is a building block you add to a template based on your infrastructure and requirements, giving you control over where and how agents run.

This page is a guide for Coder administrators on how to build and configure Coder templates and the underlying infrastructure to sandbox AI agents, according to the previous definitions. Because Coder is flexible and infrastructure-agnostic, this page outlines several ways to achieve each trait, and the tradeoffs of each.

TraitOptionsMain tradeoff
Kernel isolationVMs, gVisor, Kata Containers, Nitro SandboxesStronger isolation needs node support that managed node pools can lack
Network isolationNetworkPolicy, egress proxy, OpenShell, CiliumFiner rules need more components to run
Secrets isolationAPI key scopes, AI Gateway, service accounts, dynamic secretsLess access means more setup for each credential
Fast startupPrebuilt workspaces, faster templates, warm poolsReady capacity costs idle resources

Start from a registry template

Start from a registry template

These templates in the Coder Registry support all three traits, plus fast startup. Each one is a starting point that you adapt to your cluster.

TemplateKernel isolationNetwork isolationSecrets isolationFast startup
agent-sandbox (Kubernetes)gVisor or KataNetworkPolicyScoped AI Gateway tokenPrebuilds or agent-sandbox warm pools
Plain Sandbox (Kubernetes)Optional gVisor or KataSquid proxy by host nameScoped AI Gateway tokenPrebuilds
OpenShell (Kubernetes)Optional KataOpenShell rules by host name and binaryScoped AI Gateway tokenPrebuilds
AWS Nitro SandboxesNitro Sandboxes on Amazon Elastic Kubernetes Service (EKS)NetworkPolicy and Squid proxyScoped AI Gateway tokenPrebuilds

AWS Nitro Sandboxes is a community template, and Coder doesn't verify it.

Tip

agent-sandbox is a Kubernetes Special Interest Group (SIG) project that adds primitives for agent workloads. A cluster administrator sets the runtime, image, and network rules once in a SandboxTemplate, and a SandboxWarmPool keeps booted sandboxes ready. To use it with Coder, start from the agent-sandbox (Kubernetes) registry template.

Sandbox coverage by access method

Sandbox coverage by access method

The sandbox is the workspace that your template builds. Kernel, network, and secrets isolation apply to every process in that workspace, and to nothing outside it.

You can use one template to sandbox every way agents access Coder, but the amount of control varies by access method, because each one runs a different share of the agent in the workspace:

Access methodInside the sandboxOutside the sandboxWhat the sandbox controls
CLI Agents, such as the Claude Code or Codex modulesThe agent loop, tool calls, commands, Model Context Protocol (MCP) servers, hooks, and subagentsTools that the model provider hosts, such as web searchTool calls, commands, servers, model requests, and credentials
Coder AgentsTool calls, commands, and servers from a workspace .mcp.jsonThe agent loop, model credentials, admin-configured MCP servers, and control plane tools such as create_workspaceTool calls, commands, and servers in the workspace
Agent RelayTool calls, commands, and servers that the worker startsThe agent loop and the provider's own tools, in the provider's cloudTool calls, commands, and servers in the workspace

To control what runs outside the sandbox, use the settings of the system that runs it, such as MCP server availability in Coder Agents.

Define sandbox profiles as parameters

Define sandbox profiles as parameters

One template can offer several sandbox profiles, such as "Fully isolated" and "Internal GitHub". Each profile is a fixed set of rules: the RuntimeClass, the hosts that the proxy allows, and the token scopes.

Make the profile an immutable parameter, with mutable = false. Coder rejects any later build that changes an immutable parameter, so a workspace keeps the profile it was created with until you delete it. Neither the agent nor the user can loosen the sandbox with a workspace update or restart.

Add one preset for each profile, and make the strictest one the default. A preset sets the profile when the workspace is created, and can keep a pool of prebuilt workspaces for each profile. Because the preset sets every immutable parameter, a claimed prebuild never changes its profile.

locals {
  profiles = {
    isolated = { display_name = "Fully isolated", runtime = "gvisor", hosts = [] }
    github   = { display_name = "Internal GitHub", runtime = "gvisor", hosts = ["github.example.com"] }
    develop  = { display_name = "GitHub and packages", runtime = "kata-qemu", hosts = ["github.example.com", "artifactory.example.com"] }
  }
  profile = local.profiles[data.coder_parameter.sandbox_profile.value]
}

data "coder_parameter" "sandbox_profile" {
  name         = "sandbox_profile"
  display_name = "Sandbox profile"
  description  = "What the workspace can reach. You cannot change it after create."
  type         = "string"
  form_type    = "dropdown"
  default      = "isolated"
  mutable      = false

  dynamic "option" {
    for_each = local.profiles
    content {
      name  = option.value.display_name
      value = option.key
    }
  }
}

data "coder_workspace_preset" "profile" {
  for_each   = local.profiles
  name       = each.value.display_name
  default    = each.key == "isolated"
  parameters = { sandbox_profile = each.key }
}

Then use local.profile.runtime as the Pod's runtime_class_name, and local.profile.hosts as the proxy's allowlist. The Plain Sandbox (Kubernetes) template uses this pattern for its network profiles and kernel isolation.

Coder Agents chooses the preset and parameter values when it calls create_workspace, so a parameter alone lets the agent pick a looser profile. To decide the profile for the agent:

  • Limit options by group: with identity-aware parameters, show a profile only to members of a group, such as data.coder_workspace_owner.me.groups. Coder rejects values that aren't in the owner's options. If prebuilds use a limited profile, add the prebuilds user to the group.
  • Use one template for each profile: give agents only the strict templates. On every other template, turn off Allow Coder Agents to create workspaces using this template, as in Restrict available templates. Developers can still create workspaces from the other templates.

Template names, descriptions, and organization instructions help the agent pick the right profile, but they don't enforce it.

Kernel isolation

Kernel isolation

Containers on a node share its kernel, so a kernel exploit can escape to the node and its other workloads. Kernel isolation gives each workspace its own kernel, or a kernel in user space, so the exploit stays inside.

MethodHow it worksTradeoffs
VM-based templatesEach workspace is its own virtual machine, such as the AWS Linux template.More expensive, and slower to start than a container.
gVisorA kernel that runs in user space and handles the workspace's system calls.Slower, especially for file-heavy work. It still uses some host kernel features, and some Linux features don't work.
Kata ContainersEach Pod runs in a small VM with its own kernel, with a hypervisor such as QEMU or Cloud Hypervisor.Needs bare metal or nested virtualization, which many managed nodes don't have. Uses more memory for each Pod.
Nitro SandboxesEach Pod runs in its own Firecracker VM on the AWS Nitro hypervisor, with the isolated-sandbox RuntimeClass.EKS only. Built into EKS Auto Mode; managed node groups need a custom Amazon Machine Image (AMI). No GPUs, and a fixed size for each Pod.

Sysbox isn't kernel isolation. It runs Docker and systemd in the workspace without a privileged container, by mapping root in the workspace to an unprivileged user on the node, but every system call still goes to the node's kernel. Use it when agents need Docker, and add gVisor or Kata if they also need kernel isolation. For what its user namespaces cover, refer to Sysbox security.

On Kubernetes, install gVisor or Kata on the nodes, and add a RuntimeClass for it to the cluster. EKS Auto Mode already has the isolated-sandbox RuntimeClass for Nitro Sandboxes. To use one, set runtime_class_name on the workspace Pod in your template:

resource "kubernetes_pod_v1" "workspace" {
  # ...
  spec {
    runtime_class_name = "gvisor"
    # ...
  }
}

The RuntimeClass name is whatever the cluster administrator created, such as gvisor, kata-qemu, or isolated-sandbox. To let users pick a runtime, set runtime_class_name from a parameter. To run sandboxed Pods on their own nodes, add a node selector and a toleration for a tainted node pool.

Workspace process logging runs a privileged eBPF sidecar in the workspace Pod. With gVisor, Kata, or Nitro Sandboxes, that sidecar runs inside the sandbox too, so test process logging with your runtime before you rely on it.

Examples and references:

Network isolation

Network isolation

Network isolation works as a firewall for the agent. It limits where the agent can send data, so a prompt injection can't send your code or credentials to an attacker's host. It also limits where the agent can download from, which reduces the risk of a compromised package or a page with injected instructions.

The workspace always needs to reach the Coder server. The Coder workspace agent relays connections through it, so the workspace needs no inbound access. For the connections Coder uses, refer to Outbound network access.

MethodRules byApplies toTradeoffs
Kubernetes NetworkPolicyIP address and portThe whole PodBuilt in, but IP-based, so services that change addresses, such as GitHub, are hard to allow.
Egress proxy, such as SquidHost nameThe whole PodAnother component to run and update, as a separate Pod or a sidecar.
NVIDIA OpenShellHost name and the binary that connectsThe whole sandboxNested setup: a cluster gateway, plus a launcher Pod and a supervisor Pod for each workspace. Slower to stop than a plain Pod.
CiliumHost name, IP address, port, and HTTP requestsThe whole PodCilium must be the cluster's network plugin.

Warning

Kubernetes accepts a NetworkPolicy even when the network plugin doesn't enforce it. Test each rule from inside a workspace before you run an agent in it.

An egress proxy runs in one of two places:

  • Separate proxy Pod: the proxy runs in its own Pod, either one for each workspace or one shared by all workspaces. A NetworkPolicy lets the workspace Pod reach only Coder and the proxy Pod, so the agent can't go around the proxy. The Plain Sandbox (Kubernetes) template uses this setup, with a separate Squid Pod for each workspace.
  • Sidecar: the proxy runs as a second container in the workspace Pod. It's simpler to deploy, but the two containers share one network, and a NetworkPolicy can't tell their traffic apart. To stop the agent from going around the proxy, send all of the Pod's outbound traffic to the proxy, except the proxy's own, such as with iptables rules in an init container.

Rules by host name still allow every path on an allowed host. If an allowed host stores user content, such as a code host or a file share, an agent can still send data to it.

A rule that applies to the whole Pod is harder to bypass than one that wraps a process, because a command the agent starts in another way still goes through it. For more detail, refer to Why network boundaries are more effective than process-level controls.

Every method in this section runs in the cluster, so a mistake in a template or cluster setting can open it. For a layer that holds anyway, have your security team run a firewall outside the cluster, such as AWS Network Firewall with an allowlist of TLS server names, Route 53 Resolver DNS Firewall, or an S3 gateway endpoint policy. Limit DNS too, because an agent that can resolve any name can send data out in DNS lookups.

Example rules

Example rules

Start with a rule that matches what the agent does. The examples use github.example.com for your internal GitHub, such as GitHub Enterprise Server, and artifactory.example.com for your internal artifact store, such as JFrog Artifactory or Sonatype Nexus. Replace them with your own hosts.

Use caseAllowBlockFits
Send all model traffic through AI GatewayEverything elseModel providers, such as api.anthropic.com and api.openai.comEgress proxy, Cilium, or a corporate firewall
A known workflow, such as a docs review bot that comments on pull requestsCoder, github.example.com, and artifactory.example.comEverything elseEgress proxy, OpenShell, or Cilium
A research task on sensitive dataCoder and artifactory.example.comEverything elseNetworkPolicy

The first rule makes sure that AI Gateway can monitor and audit every model request. If your network already blocks model providers, you don't need it in the template. The second rule fits any workflow where you know what the agent needs. An internal GitHub keeps the agent's traffic in your network, which is stricter than an allow rule for all of github.com. OpenShell can also limit github.example.com to the git binary. The third rule turns off the internet, which suits long tasks that don't need it. The agent can still install packages from the artifact store.

Note

An artifact store that proxies public registries, such as npm or PyPI, can send requests to the internet for the agent. For the strictest rules, allow only repositories that serve packages your team already approved.

Examples and references:

Secrets isolation

Secrets isolation

An agent can use any credential it can read, and a prompt injection can tell it to send that credential somewhere. Secrets isolation keeps credentials that the task doesn't need out of the workspace, and gives the rest the smallest scope that works.

Every process that the Coder workspace agent starts, such as a terminal, an app, a startup script, or an AI agent, gets the workspace agent token in CODER_AGENT_TOKEN. An AI agent that reads this token can use it to get:

So the identity that owns the workspace sets the blast radius. To limit what an agent can read:

MethodHow it worksTradeoffs
API key scopesGive the agent a token with only the scopes it needs, instead of the owner's session token.Available in Coder. You pick the scopes for each task, and a scope that's too narrow fails the task.
AI GatewayThe agent calls model providers through Coder with a Coder token, so the provider API keys stay on the server.Coder Premium. The agent still needs a Coder token to authenticate, so pair it with a scoped token or a service account.
Service accountsRun autonomous agents as a headless user with no linked external auth, no personal secrets, and only the template access they need.Coder Premium. Work isn't tied to the person who asked for it, so set a Git identity for commits.
Dynamic secretsCreate a short-lived credential or cloud identity for each workspace in the template, tied to the workspace lifecycle.A leaked credential expires on its own. Needs a secrets manager or cloud identity setup for each provider.

Scope the workspace agent token

Scope the workspace agent token

Set api_key_scope = "no_user_data" on the coder_agent so its token can't fetch the owner's Git SSH key or external auth tokens:

resource "coder_agent" "main" {
  os            = "linux"
  arch          = "amd64"
  api_key_scope = "no_user_data"
}

The tradeoff is that Git over SSH and Git credentials from external auth stop working in the workspace, so give the agent its own Git credential, such as a dynamic secret. The scope doesn't affect user secrets and workspace secrets, which Coder still injects. To keep those out, run the agent as a service account that has none.

Scope the AI Gateway token

Scope the AI Gateway token

data.coder_workspace_owner.me.session_token has the owner's full access to the Coder API, and the AI Gateway client examples inject it so the agent can authenticate. Instead, use the session token once at build time to create a token with only the user:read_personal scope, which AI Gateway accepts, and give the workspace that token:

data "coder_workspace" "me" {}
data "coder_workspace_owner" "me" {}

data "http" "ai_gateway_token" {
  count  = data.coder_workspace.me.is_prebuild ? 0 : data.coder_workspace.me.start_count
  url    = "${data.coder_workspace.me.access_url}/api/v2/users/me/keys/tokens"
  method = "POST"
  request_headers = {
    "Coder-Session-Token" = data.coder_workspace_owner.me.session_token
    "Content-Type"        = "application/json"
  }
  request_body = jsonencode({
    token_name = "ai-gateway-${substr(data.coder_workspace.me.id, 0, 8)}-${formatdate("YYYYMMDDhhmmss", timestamp())}"
    scopes     = ["user:read_personal"]
    # Nanoseconds. Keep it longer than the template's autostop.
    # lifetime = 8 * 3600 * 1000000000
  })

  lifecycle {
    postcondition {
      condition     = self.status_code == 201
      error_message = "Coder did not create the AI Gateway token."
    }
  }
}

resource "coder_env" "anthropic_base_url" {
  count    = data.coder_workspace.me.start_count
  agent_id = coder_agent.main.id
  name     = "ANTHROPIC_BASE_URL"
  value    = "${data.coder_workspace.me.access_url}/api/v2/ai-gateway/anthropic"
}

resource "coder_env" "anthropic_auth_token" {
  count    = data.coder_workspace.me.is_prebuild ? 0 : data.coder_workspace.me.start_count
  agent_id = coder_agent.main.id
  name     = "ANTHROPIC_AUTH_TOKEN"
  value    = sensitive(jsondecode(data.http.ai_gateway_token[0].response_body).key)
}

Each start creates a new token. Coder can't revoke it at stop, so it stays valid until it expires, after the deployment's default token lifetime (7 days by default). To shorten that, set lifetime, but keep it longer than the template's autostop so the token doesn't expire in a running workspace. The count skips prebuilds, because Coder doesn't create tokens for the prebuilds user, and the claim build creates the token instead. For a full version, refer to the Plain Sandbox (Kubernetes) template.

The scoped token helps only if the workspace has no other copy of the session token. Coder doesn't set CODER_SESSION_TOKEN in a workspace by itself. The coder-login module sets it, so the coder CLI in the workspace acts as the owner. Other modules also inject the session token when you turn on their AI Gateway option, such as ANTHROPIC_AUTH_TOKEN in the Claude Code module with enable_ai_gateway = true. To keep the session token out of the workspace:

  • Remove the coder-login module.
  • Turn off the AI Gateway option in agent modules, and set the scoped token yourself, as in the example.
  • Search the template for other references to data.coder_workspace_owner.me.session_token, and keep only those used at build time, such as the http data source in the example.

Then run env | grep -i token in a workspace to confirm that only the scoped token and CODER_AGENT_TOKEN are there.

Provision just-in-time credentials

Provision just-in-time credentials

Create a short-lived credential for each workspace start, instead of a long-lived key in the image or a user secret. This example reads AWS Security Token Service (STS) credentials from the Vault AWS secrets engine at build time, with a one-hour lifetime:

data "vault_aws_access_credentials" "agent" {
  count   = data.coder_workspace.me.start_count
  backend = "aws"
  role    = "coder-agent"
  type    = "sts"
  ttl     = "1h"
}

resource "coder_env" "aws_access_key_id" {
  count    = data.coder_workspace.me.start_count
  agent_id = coder_agent.main.id
  name     = "AWS_ACCESS_KEY_ID"
  value    = data.vault_aws_access_credentials.agent[0].access_key
}

resource "coder_env" "aws_secret_access_key" {
  count    = data.coder_workspace.me.start_count
  agent_id = coder_agent.main.id
  name     = "AWS_SECRET_ACCESS_KEY"
  value    = sensitive(data.vault_aws_access_credentials.agent[0].secret_key)
}

resource "coder_env" "aws_session_token" {
  count    = data.coder_workspace.me.start_count
  agent_id = coder_agent.main.id
  name     = "AWS_SESSION_TOKEN"
  value    = sensitive(data.vault_aws_access_credentials.agent[0].security_token)
}

The Vault role sets what the credential can do, so give it only the AWS permissions that the agent's tasks need. A leaked credential stops working when its lifetime ends, and a restart issues a new one. The credential is also stored in the workspace's Terraform state, so keep the lifetime short.

On EKS, give each workspace its own service account and Pod Identity association, such as one named after the workspace ID. Workspaces can then share one AWS Identity and Access Management (IAM) role, and its policy can limit each workspace to its own data with the ${aws:PrincipalTag/kubernetes-service-account} session tag, such as an S3 prefix for each workspace. Credentials copied out of a workspace work from anywhere until they expire, so add an aws:SourceVpce or aws:SourceIp condition to tie them to your network.

Examples and references:

Fast startup

Fast startup

Fast startup isn't isolation, but it decides whether people use the sandbox. An agent that starts a new workspace for each task creates many more workspaces than a person does. If the sandbox takes minutes to start, users run the agent on their own machine instead, outside every control on this page.

To see a Kubernetes template go from 98 seconds to 1.8 seconds, watch Improving Coder Workspace Startup Times.

MethodHow it worksTradeoffs
Prebuilt workspacesCoder keeps a pool of ready workspaces for each preset, and a user claims one at create.Coder Premium. Idle workspaces cost compute, and a slow claim can cancel out the gain.
Speed up templatesFind slow steps with the build timeline, and move tools into the image instead of startup scripts.Available in Coder, with no idle cost. A larger image takes longer to pull on a new node.
agent-sandbox warm poolsA SandboxWarmPool keeps booted sandboxes ready in the cluster.Another controller to run in the cluster, and idle sandboxes cost compute.

A claim changes the workspace owner, so a resource that uses the owner's name in an immutable field is replaced at claim, and the claim is slow. To keep the claim fast, refer to Prevent resource replacement.

With sandbox profiles, each profile's preset keeps its own pool of prebuilt workspaces. Because the profile is immutable and the preset sets it, the claim keeps the profile the prebuild was built with. Size each pool for how often agents use that profile, and skip the pool for profiles that are rarely used.

Examples and references:

Audit agent activity

Audit agent activity

A sandbox limits what an agent can do. Logs show what it did, so you can tell whether the limits worked.

MethodProvided byWhat it records
AI Gateway auditCoder PremiumPrompts, tool calls, and token use for each model request.
Workspace process loggingCoder PremiumEach process started in the workspace container, with eBPF.
Connection logsCoder PremiumSSH, app, and port-forward sessions into the workspace.
Audit logsCoder PremiumChanges to templates, workspaces, users, and tokens.

Learn more

Learn more