Resources / Code execution
AI code execution: how to run code an AI wrote, safely
What AI code execution is, the risks of running model-written code, and the controls that make it safe: sandboxes, limits, secrets and review.
"AI code execution" covers two different things: an agent running code it just wrote to answer a question (analysing a CSV, calling an API, checking a calculation), and deploying model-written code that runs for real users. Both mean running code nobody wrote by hand — so both need the same foundations.
Why it's risky
Model-written code is usually right. When it isn't, it fails in ways that matter:
- It does too much — deletes files, loops forever, downloads something huge.
- It follows instructions it shouldn't — prompt injection from a web page or document can steer what the code does.
- It leaks — prints secrets into output, sends data to a URL it shouldn't call.
- It's expensive — a tight loop or a fan-out of API calls burns compute and money.
The five controls
### 1. Run it in a sandbox
Never run generated code in the same process, machine or account as your application. Each run gets an isolated environment that it can't escape and that can't see other users' data. See sandboxing for AI code for the options.
### 2. Cap time, memory and output
Every execution has a hard timeout, a memory limit and a maximum response size. A runaway job should hit a wall in seconds, not run up a bill overnight.
### 3. Control the network
Decide what the code may reach. Analysis code often needs no network at all; integration code needs specific APIs. Deny by default where you can, and log outbound calls.
### 4. Keep secrets out of the code and the model
Inject credentials at run time from an encrypted store. The model should know a secret exists ("use the STRIPE_KEY variable") but never see its value, and outputs should be scanned so values don't leak into chat or logs.
### 5. Separate testing from production
Run new code against test inputs first, then promote it deliberately. Keep the previous version one click away.
Ephemeral vs deployed execution
| Ephemeral (agent scratchpad) | Deployed (endpoint or job) | |
|---|---|---|
| Lifetime | Seconds, then thrown away | Runs on every request until replaced |
| Typical use | Data analysis, quick checks | APIs, webhooks, scheduled jobs |
| Key control | Disposable sandbox, no secrets | Versioning, review, monitoring |
How Wisp runs AI-written code
Wisp is built for the deployed kind: the code the assistant writes becomes an endpoint or task that runs isolated per project, with a 15-second time limit, 256 MB of memory, encrypted secrets the model never sees, rate limits before code runs, and a log of every request. Every change is a draft you test with ?test before publishing — and it scales automatically once it's live.
Build it with Wisp — describe an API, get it live.