Blog / 4 December 2026

Deep dive · 4 December 2026 · 8 min read

LLM-written code: what to control before, during and after it runs

Code an LLM wrote is code from an author you cannot question, so the safe way to run it is to plan for four moments: before it runs, while it runs, after it finishes and between one run and the next. Before, control what it can install. While it runs, put it behind a kernel boundary it does not share, with no secrets inside and a closed network. After, treat its output as untrusted input, because it flows straight back into your model. Between runs, start clean and keep a record the code cannot edit.

run untrusted llm codellm generated code sandboxsafely execute ai generated codecode interpreter security

01Generated code is an unknown author's code, even when the model is yours

A model has no intent, but the code it writes can still do harm in three ways. It can be wrong: a cleanup loop with the wrong path, a database update without a filter, a retry with no back-off that hammers an API. It can be steered: a prompt injection hidden in a README, an issue or a web page the agent read can shape the code it writes next. And it can pull in code nobody picked, through the package names it suggests.

OWASP's Top 10 for LLM Applications names the two failures underneath most of the damage. Improper Output Handling (LLM05) is passing model output to a shell or interpreter without controls. Excessive Agency (LLM06) is giving a model more reach than its task needs. Both are decided by the environment the code runs in, not by how good the model is, and the stages below walk through that environment in order. For the whole checklist on one page, see how to safely run untrusted code generated by LLMs.

02Before it runs: control what the code can pull in

The install step is where generated code most often reaches past anything you reviewed. A model can name a package that does not exist, and if someone registers that name, the install runs their code with whatever access the install step has. The paper We Have a Package for You! tested code-generating models and found the average share of hallucinated packages was at least 5.2% for commercial models and 21.7% for open-source models, with 205,474 unique hallucinated package names. Those are names an attacker can register.

Controls that hold up in practice:

  • Allow one package index, or your own mirror, on the network during installs, and nothing else.
  • Pre-install the packages you know you need into a pinned image, built once, rather than letting each run fetch them.
  • Run installs inside the sandbox, never on the host, so install scripts sit behind the same wall as the code itself.
  • Log every package name the code asks for. A name nobody on the team recognises deserves a look before it becomes a habit.
  • Keep static review and linters as a cheap first check. They catch obvious mistakes, but obfuscated or harmless-looking code gets past filters, so they are not the boundary.

03While it runs: a wall it does not share, and nothing worth taking inside

The wall decides what a bug in the code, or in the layer under it, can reach. A plain container runs the code's system calls on the host kernel, so a kernel or runtime bug can become a host escape, and the Docker Engine security guide is clear that the daemon runs with root privileges and only trusted users should control it, which rules out handing its socket to generated code. A user-space kernel such as gVisor answers system calls itself, so the host kernel sees far fewer of them. A microVM such as Firecracker gives the code its own guest kernel behind hardware virtualization. For unreviewed code that needs a full Linux with pip, npm and compilers, pick one of the last two. The trade-offs are laid out in microVM vs container isolation for LLM code.

A strong wall does nothing about what you put inside it. Three more controls decide how much a mistaken or steered run can do:

  • No secrets inside. No API keys in environment variables, no mounted home directory, no SSH agent, no cloud credentials. If the code must call an API, a broker outside the sandbox adds the key on the way out. See how to give an AI agent API keys without exposing them.
  • A closed network. Deny by default, allow named hosts, and enforce the rule outside the sandbox, where root inside cannot change it. Answer DNS only for allowed names. More in how to limit an agent's network to an allow-list.
  • Hard limits that fail closed. CPU, memory, disk, process count and a wall-clock timeout for every run. A timeout should end the run and return an error to the agent, not hang the worker that called it.

04After it runs: the output is the next prompt

This is the stage most designs skip. In a code-interpreter loop, the run's stdout, stderr and the files it wrote are handed back to the model as context. That turns output into an input channel. Code that reads a hostile web page or a poisoned data file can print text that reads like instructions, and the model sees it on its next turn next to everything else it trusts.

You cannot make a model immune to that, but you can limit what one misled step leads to:

  • Cap the size of stdout and stderr before they reach the model, and say plainly when output was cut.
  • Mark tool output as data in the prompt, and never let it change the agent's tools or permissions on its own.
  • Return files through one known output path, and check type and size before anything else opens them.
  • Keep actions that touch the outside world, such as a push, a payment or a message, behind a separate human approval rather than letting the next generated script do them.

05Between runs: start clean and keep a record the code cannot edit

Run-to-run contamination is quiet: a package one run installed, a file left in a temp folder, a background process still listening on a port. Across users or tenants it becomes a data leak. Use a fresh sandbox per run, or restore a clean snapshot before each one. Restoring is now fast enough to do every time: in kosa8's own benchmark (bench/README.md), a restore took 74 ms on Linux.

Keep a record of every run: the code, the commands, exit codes, network attempts allowed and refused, and output sizes. Write it from outside the sandbox, so the code cannot edit its own history. When something goes wrong, that record is how you tell a wrong answer from a bad dependency from a steered run, and it is what you hand a security reviewer who asks what the agent actually did.

06One way to run all four stages with kosa8

kosa8 is post-quantum cybersecurity infrastructure for AI agents, and it covers these stages in one tool. Each sandbox is a real microVM with its own kernel, and it sees one workspace folder and nothing else on the host. The network is closed except for hosts on an allow-list you write. API keys stay on the host, where an egress broker injects them on the way out, so the sandbox holds only a placeholder. Every action goes to a hash-chained, tamper-evident audit log that kosa8 audit verify checks.

For the clean-state stage, set the sandbox up, take a checkpoint, run the generated code, then rewind. kosa8 checkpoint sealed --label "before the risky bit" saves the whole running machine, memory and processes included, and kosa8 rewind sealed puts it back, still running, in under a second. What kosa8 does not do: it does not judge whether the code is correct, and it does not detect prompt injection. It limits what a mistaken or hijacked run can reach.

Takeaways

  1. Allow one package index or mirror during installs, and log every package name the code requests.
  2. Run generated code behind a kernel it does not share: a microVM or a user-space kernel, not a plain container.
  3. Put no secrets inside; broker keys from outside and enforce the network allow-list outside too.
  4. Cap and label output before it goes back to the model, and keep outside-world actions behind approval.
  5. Start every run from a fresh sandbox or a clean snapshot, and log each run from outside it.

Where kosa8 fits · Post-quantum cybersecurity infrastructure for AI agents

kosa8 gives each run its own microVM that sees one workspace folder, with a closed network, keys kept on the host behind an egress broker and a hash-chained audit log. Checkpoint and rewind put the whole running machine back to a clean state between runs.

Read next: AI security for the enterprise · AI for cybersecurity · Cybersecurity for AI agents, topic by topic

Check yourself · +100 XP

Your code interpreter runs in a microVM with no network and no secrets. Which risk is still open?

Questions people ask

Is it safe to run LLM-generated code with exec() or eval() if I review it first?

No. Review catches obvious mistakes, but running the code inside your application's process gives it your process's memory, credentials and network. Run it in a separate sandbox with its own kernel, no secrets and a closed network, even when it looks harmless.

What is package hallucination?

A model suggesting a package that does not exist. If an attacker registers that name, installing it runs their code. Limit installs to one index or mirror, pre-install known packages, and run installs inside the sandbox.

Why does a code interpreter's output need controls?

Because it goes back into the model's context. Code that reads hostile content can print text that steers the model's next step, so cap output size, mark it as data and keep risky actions behind a human approval.

Sources

  1. LLMRisks Archive — OWASP Gen AI Security Project
  2. We Have a Package for You! A Comprehensive Analysis of Package Hallucinations by Code Generating LLMs — arXiv
  3. Docker Engine security — Docker docs
  4. What is gVisor? — gVisor docs
  5. Firecracker — Firecracker project

Written with Claude from the sources above and checked automatically before publishing: news claims link their source, every number and quote is in a source or on kosa8.com, and kosa8 commands come from our fact sheet. Spotted a mistake? Tell us.

Try it on your own machine

Give your next agent its own sealed machine.

One line installs kosa8 on your own machine. Start free with no card; plans from $29 a month.