---
title: "Is LLM-generated code safe to execute?"
description: "What AI-generated code can reach, what checking it before it runs can and cannot show, what QWED’s Code engine admits, and what to use for the rest."
url: https://qwedai.tech/guides/ai-code
---

[Engines](https://qwedai.tech/engines)

# Is AI-generated code safe to run? What a check can tell you first

A model that writes code is often right, and sometimes writes something that does more than it was asked. This guide covers what such code can reach, the difference between checking code before it runs and running it in a sandbox, what QWED's Code engine admits, and what to use for everything it does not.

QWED project · Published 3 December 2026 · Updated 3 December 2026

## The short answer

Not by default. Code a model writes can read files, reach the network, start processes or use names that mean something only where it runs, and it reads as confidently when it does. Check it before it runs, and run it in a sandbox even then. QWED's Code engine, open source under Apache-2.0, admits a narrow kind of code: Python that only computes, using listed standard-library modules and names. It denies everything else, and never admits JavaScript, TypeScript, Java or Go. For general application code, use static analysis such as Semgrep, CodeQL or Bandit, a sandbox and review.

## What can go wrong with AI-generated code?

-   **It reaches files or the network.** A script asked to total a column also opens a file, or sends what it read somewhere.
-   **It starts processes.** A call to `subprocess` or `os.system` runs whatever command the string holds.
-   **It runs code from text.** `eval`, `exec` and less obvious routes turn a string into code that no reader saw.
-   **It pulls in a package.** A third-party import brings in code the model did not write and you did not review.
-   **It reads a name it never set.** In a notebook or a REPL, `df` or `np` is whatever the session holds under that name.

## Before it runs, or while it runs?

There are two different things you can do with code you did not write, and they answer different questions.

Before it runs

What can this code reach?

A static check reads the code without running it and decides from its shape. It can refuse code before anything happens. It cannot see values that exist only at run time.

In QWED: the Code engine.

While it runs

What can this code damage?

A sandbox runs the code with limits: no network, a read-only filesystem, an unprivileged user, a time limit. It contains what the code does. It does not show that the code was safe or correct.

In QWED: the Symbolic engine runs CrossHair inside a Docker sandbox and returns `UNVERIFIABLE` or `BLOCKED`, never a proof. The Stats engine runs code in a sandbox and returns `UNVERIFIABLE`. `SecureCodeExecutor` runs code; it does not verify it.

Running code is not verifying it. Use both: check first, and run what passes in a sandbox.

## What does QWED's Code engine admit?

The Code engine is part of QWED's open-source core. For Python it parses the code into a syntax tree and admits it only when it is safe by its shape. Lists of what is dangerous kept missing something (the track record below shows how often), so the engine works from a list of what is safe instead:

-   **Modules.** Only computation-only standard-library modules, such as `math`, `decimal`, `fractions`, `statistics`, `itertools`, `json`, `re` and `datetime`, and in each only the listed names: `os.sep` is admitted, `os.system` is not. No third-party module is admitted.
-   **Builtins.** Only builtins that compute. `open`, `eval`, `getattr`, `vars`, `globals` and the others that reach files, code or namespaces are refused.
-   **Attributes.** Special attributes (`__x__`) are refused, and so is reading an attribute by a name made at run time.
-   **Names.** A name the code reads before it binds it is refused. If your runtime binds names before the code runs, declare them: `CodeVerifier(bound_names=["df"])`.

Other languages get less. JavaScript, TypeScript, Java and Go are only searched for listed patterns, and a call reached through a variable or an alias matches none of them. A match shows the code unsafe; no match shows nothing, so such code comes back `UNVERIFIABLE` and is never admitted. SQL sent to `/verify/code` is decided by the SQL engine without a schema, so writes are refused but tables and columns are not checked. [Checking LLM-generated SQL](https://qwedai.tech/guides/ai-sql)

## What does the check return?

| Code | Decision | Why |
| --- | --- | --- |
| Python: add up prices with decimal | `VERIFIED · ADMIT` | Every module and name it uses is on the list. |
| Python: subprocess.run(\["ls", "-la"\]) | `VERIFIED · DENY` | module\_not\_admitted, process\_execution |
| Python: open("notes.txt").read() | `VERIFIED · DENY` | builtin\_not\_admitted, file\_or\_network\_access |
| Python: import numpy | `VERIFIED · DENY` | module\_not\_admitted: no third-party module is on the list. |
| Python: print(df.head()) | `VERIFIED · DENY` | unbound\_name. With bound\_names=\["df"\], VERIFIED · ADMIT. |
| JavaScript: console.log(1 + 1); | `UNVERIFIABLE · DENY` | code\_verifier.language\_unverifiable: never admitted. |
| Python that does not parse, or Rust | `BLOCKED · DENY` | code\_verifier.parse\_error, code\_verifier.unsupported\_language |

`VERIFIED · DENY` means the engine found what the code uses outside its list, and the result names it. `UNVERIFIABLE · DENY` means it could not tell. `BLOCKED · DENY` means it could not check the code at all. Only `VERIFIED · ADMIT` may run.

## Try it

In Python, with the core package (`pip install qwed`), with the output of a run on qwed 13.0.0:

python

```
from qwed_new.core.code_verifier import CodeVerifier
from qwed_new.core.diagnostics import admission_decision

admitted = '''from decimal import Decimal

prices = ["19.99", "5.01", "0.10"]
total = sum(Decimal(p) for p in prices)
print(total)'''

refused = '''import subprocess

subprocess.run(["ls", "-la"])'''

for code in (admitted, refused):
    result = CodeVerifier().verify_code(code, language="python")
    print(result.status.value, admission_decision(result).value)
    print(sorted({i["type"] for i in result.developer_fields["issues"]}))
# VERIFIED ADMIT
# []
# VERIFIED BLOCKED
# ['module_not_admitted', 'process_execution']
```

`BLOCKED` in the second value is the admission decision; in a Verification Context record the same decision is written `DENY`. Over the [self-hosted API](https://github.com/QWED-AI/qwed-verification#readme), send the code and its language:

request

```
POST /verify/code
{
  "code": "import math\nprint(math.sqrt(2))",
  "language": "python"
}
```

## What to use for the rest

Most application code reads files, calls services and uses packages, so the Code engine will not admit it. That is the right answer for a check that only admits what it can show safe, and it means other tools have to do the rest:

-   **Static analysis** for code in any language: [Semgrep](https://github.com/semgrep/semgrep) matches patterns that look like source code, [CodeQL](https://github.com/github/codeql) runs queries over code for code scanning, and [Bandit](https://github.com/PyCQA/bandit) looks for common security issues in Python. They report findings; they do not admit code.
-   **A check on every pull request** for code that is committed. [QWED Security](https://qwedai.tech/platform/security), a GitHub App, reads the files a pull request changes with deterministic engines and no model in the verdict: Python, JavaScript and TypeScript, Go and Rust patterns, taint from untrusted input to dangerous calls, credentials shaped like known providers' keys and high-entropy secrets, shell commands, Dockerfiles, CI workflows and dependencies. It blocks on what the pull request introduces and on what a release would ship, keeps older findings visible without blocking, and reports as GitHub checks, a pull request comment and code scanning results. It reads the code; it does not run it.
-   **A sandbox** for anything you run: a container with no network and a read-only filesystem, or a stronger boundary such as [gVisor](https://github.com/google/gvisor) or a [Firecracker](https://github.com/firecracker-microvm/firecracker) microVM, with limits on time and memory.
-   **Review** by a person for code that will stay, as for code from anyone else.

## What does it not check?

-   **Whether the code is right.** Admitted code can still compute the wrong thing. The engine checks what the code can reach, not what it returns. For a figure, check the figure. [How to check AI calculations](https://qwedai.tech/guides/llm-math)
-   **Time and memory.** A loop that never ends is made only of admitted parts. Run admitted code with limits.
-   **What your runtime binds.** `bound_names` and `trusted_modules` are your declarations about your runtime; the engine takes them as given.
-   **Code in other languages.** It is never admitted, and a pattern search that finds nothing proves nothing.

Results are only as sound as QWED's own lists and parser. [Everything QWED does not verify](https://qwedai.tech/limits) · [What each engine may claim](https://qwedai.tech/engines)

## Track record

Each of these was a false accept in QWED's code checks: code admitted that should have been refused. Each is fixed, and tests cover it.

-   [GHSA-7xrf-75qr-2r5w](https://qwedai.tech/security/advisories/ghsa-7xrf-75qr-2r5w), fixed in qwed 11.0.0. /verify/code admitted JavaScript and Java that run commands, SQL that drops tables, and Python that uses names it never imports. Other languages are now never admitted, SQL is decided by SQLVerifier, and a name read before it is bound is refused.
-   [GHSA-9r7q-mx75-hcj7](https://qwedai.tech/security/advisories/ghsa-9r7q-mx75-hcj7), fixed in qwed 9.1.0. CodeVerifier 8.0.0 still admitted file, network, pickle and process code that its lists of dangerous calls did not name. Python is now admitted only by a list of what is safe.
-   [GHSA-jrxf-9jh4-86w7](https://qwedai.tech/security/advisories/ghsa-jrxf-9jh4-86w7), fixed in qwed 9.0.0. String-to-code execution through timeit, cProfile, pdb and code, and file access through tarfile, zipfile, gzip, codecs, linecache, builtins.open and os.listdir, were admitted.
-   [GHSA-p75q-7rg5-ff46](https://qwedai.tech/security/advisories/ghsa-p75q-7rg5-ff46), fixed in qwed 9.0.0. Code that typing evaluates from strings, such as string annotations run by singledispatch.register or typing.get\_type\_hints, was admitted.
-   [GHSA-qrp4-p44j-m484](https://qwedai.tech/security/advisories/ghsa-qrp4-p44j-m484), fixed in qwed 9.0.0. Shell commands reached through the module loader, \_\_loader\_\_.load\_module('posix').system, were admitted.
-   [GHSA-pr6c-cqg8-6w5r](https://qwedai.tech/security/advisories/ghsa-pr6c-cqg8-6w5r), fixed in qwed 9.0.0. Code hidden by a source encoding declaration, such as utf-7 or unicode\_escape, was admitted.
-   [GHSA-4rj3-8wqj-cqwc](https://qwedai.tech/security/advisories/ghsa-4rj3-8wqj-cqwc), fixed in qwed 8.0.0. The SDK’s verify\_code() used a local list instead of CodeVerifier, so it admitted code the server refused.
-   [GHSA-4phf-6h72-9xfg](https://qwedai.tech/security/advisories/ghsa-4phf-6h72-9xfg), fixed in qwed 7.3.0. Command execution, file access and exfiltration code came back VERIFIED · ADMIT.
-   [GHSA-6rxr-gq43-694g](https://qwedai.tech/security/advisories/ghsa-6rxr-gq43-694g), fixed in qwed 7.2.3. Code that runs shell commands came back VERIFIED and was admitted.

[The full track record](https://qwedai.tech/security#record)

## Sources and further reading

-   [OWASP Top 10 for LLM Applications 2025: LLM05 Improper Output Handling](https://genai.owasp.org/llmrisk/llm052025-improper-output-handling/). Lists remote code execution among the harms of passing model output to a shell, exec or eval unchecked.
-   [Semgrep](https://github.com/semgrep/semgrep). Static analysis for many languages, with patterns that look like source code.
-   [CodeQL](https://github.com/github/codeql). The libraries and queries behind code scanning in GitHub Advanced Security.
-   [Bandit](https://github.com/PyCQA/bandit). A tool designed to find common security issues in Python code.
-   [gVisor](https://github.com/google/gvisor). An application kernel for containers.
-   [Firecracker](https://github.com/firecracker-microvm/firecracker). MicroVMs for serverless computing.

Written and reviewed by the QWED maintainers under the project's [governance model](https://qwedai.tech/governance). Found an error? [Contact us](https://qwedai.tech/contact).

## More guides

-   [### Runtime verification for AI output Checking each output before it is used Read](https://qwedai.tech/guides/runtime-verification)
-   [### How to verify LLM output What each kind of check catches Read](https://qwedai.tech/guides/verify-llm-output)
-   [### What fail-closed means For AI checks, and when fail-open is fine Read](https://qwedai.tech/guides/fail-closed)
-   [### Deterministic guardrails Rules, parsers and solvers for agents Read](https://qwedai.tech/guides/deterministic-guardrails)
-   [### How a claim gets proven What a proof engine does with a claim Read](https://qwedai.tech/guides/how-a-claim-gets-proven)
-   [### Can LLMs do maths? How to check AI calculations Read](https://qwedai.tech/guides/llm-math)
-   [### Checking reasoning with Z3 What a solver can settle, and what it cannot Read](https://qwedai.tech/guides/llm-reasoning-z3)
-   [### JSON Schema for LLM output Checking shape and values Read](https://qwedai.tech/guides/llm-json-schema)
-   [### LLM-generated SQL Guardrails that check a query before it runs Read](https://qwedai.tech/guides/ai-sql)
-   [### Agent tool calls and MCP Three checks before a tool call runs Read](https://qwedai.tech/guides/agent-tool-calls)
-   [### Financial figures Stopping a wrong figure from being used Read](https://qwedai.tech/guides/llm-finance)
-   [### Audit trails What each decision record holds Read](https://qwedai.tech/guides/ai-audit-trail)
