If your app already runs on AWS, putting an agent's run_code tool in a Lambda function looks like the obvious move. Lambda runs on Firecracker, scales on its own, and you pay per invocation. For some workloads that is fine. For running code an LLM wrote from untrusted input, a few properties of Lambda functions matter more than they first appear to, and the most important one is how execution environments are reused.

This post uses AWS's Lambda quotas, function timeout and execution environment lifecycle pages, read in November 2026. AWS also has a separate compute type, Lambda MicroVMs, built for sandbox-style workloads; Sandscale vs AWS Lambda MicroVMs covers that one.

What a Lambda function gives you

From the docs, for standard functions:

  • Time. The function timeout defaults to 3 seconds. The quotas page lists the maximum as 1,800 seconds (30 minutes).
  • Memory and CPU. 128 MB to 10,240 MB in 1 MB steps. CPU is allocated in proportion to memory; at 1,769 MB a function has the equivalent of one vCPU.
  • Disk. A /tmp directory of 512 MB to 10,240 MB per execution environment.
  • Packaging. A zip up to 50 MB zipped (250 MB unzipped), or a container image up to 10 GB.

For short, CPU-light snippets, such as a calculation or a small data transformation, those limits are comfortable.

Execution environments are reused

The lifecycle page explains that after an invocation, Lambda freezes the execution environment, and when the function is invoked again it may thaw and reuse it. It lists what that means:

  • Objects declared outside the handler stay initialized.
  • The contents of /tmp remain when the environment is frozen, which the docs present as a transient cache for later invocations.
  • Background processes or callbacks that had not finished resume when the environment is thawed.

The docs also say not to assume an environment persists indefinitely. For ordinary functions this is a feature: connection pools and caches survive between calls.

For agent code it is a problem. If invocation A runs code an LLM wrote for user Alice, and invocation B for user Bob lands on the same environment, then:

  • Files A wrote to /tmp may still be there for B's code to read.
  • A background process A's code started may still be running during B's invocation.
  • Anything A's code changed in the runtime's global state may still be changed.

Successive invocations of the same function can share one execution environment. So the isolation unit for untrusted code is the environment, not the invocation, and you do not control which invocations share it.

Making Lambda work anyway

People do run untrusted snippets on Lambda. The usual mitigations:

  • Run the code in a child process with its own temporary directory, kill the whole process group at the end, and delete the directory. This narrows what leaks but does not stop code that writes elsewhere or forks a process that escapes the group.
  • One function per tenant, so reuse only ever happens within one tenant. This multiplies functions and deployment work.
  • Never put anything sensitive in the environment. The function's execution role is available to code inside it through the standard AWS credential mechanisms, so the role must grant nothing, and environment variables must hold nothing secret.
  • Restrict egress with VPC configuration and security groups so code cannot reach anything it should not.

Each of these is doable. Together they are a fair amount of infrastructure to get right for a tool call.

Where a per-run sandbox differs

On Sandscale, each sandbox is its own Firecracker microVM that you create, use and stop. Nothing is reused unless you reuse it.

  • The isolation unit is yours to pick. One sandbox per tool call, per task or per conversation; per-run vs shared sandboxes goes through the choice. Two tasks share a box only if your code gives them the same sandbox id.
  • State carries over when you want it to. If a conversation needs files to persist between turns, keep its sandbox and pause it between turns; resume brings back memory and disk. Stopping a sandbox deletes its disk, so a stopped box leaks nothing to the next task.
  • No credentials inside by default. A sandbox has no role attached. If a run needs a secret, Vault hands named values to that sandbox's environment only.
  • No network by default. The SDK's network option is none unless you ask for your organization's allowlist.
  • Longer runs. A sandbox's timeoutSeconds has a server default of 3600 and a maximum of 86400, within your plan (Free sessions are up to 4 hours). Each command inside has its own time limit, with a default of 60 (in seconds) that you can set per run.
  • Results an agent can use. sandboxes.run returns the exit code, standard output and standard error, plus timedOut and oomKilled, so the model can tell a bug from a timeout from a memory kill.

Cost models

Lambda functions charge per request and per GB-second of duration, which suits many short invocations. See AWS Lambda pricing for rates.

Sandscale plans include awake vCPU-hours (100 a month on Free, 1,500 on Pro, 2,000 per member pooled on Team), and the plans reference says a box counts only while it is awake. Paid plans can add usage at $0.05 per awake vCPU-hour if you turn it on. For many very short, stateless snippets, per-invocation pricing can be cheaper. For runs that hold state across a conversation or take minutes, a sandbox you pause between steps is easier to reason about.

Which to use

Use Lambda functions for code you wrote, and for agent snippets only if you accept environment reuse and build the mitigations above, or you run one function per tenant.

Use a per-run sandbox when the code comes from a model working on untrusted input, when you need state across steps without sharing it across users, or when you want the agent to install packages, run test suites and move files without designing a container around each case. The Free plan includes 100 awake vCPU-hours a month to try it.

Originally published on the Sandscale blog.