What is Validate Before Execute?
Validate Before Execute is an AI refactoring for LLM systems, in the Safety and reliability group. Check generated code, SQL, or commands and run them in an isolated environment.
Validar Antes de Executar
Validate Before Execute is an AI refactoring for LLM systems, in the Safety and reliability group. Check generated code, SQL, or commands and run them in an isolated environment.
The analyst asks “which customers missed their Pix this month?”. The model generates DELETE FROM customers; SELECT .... Code sends the text to the production database. The first statement runs. Code, SQL, or commands generated by the model run directly, with no checks, in the real environment. This is the Unvalidated Executed Output smell.
Before executing, check what was generated with rules in code (parser, allowlist, static analysis). Then execute in an isolated, disposable environment, with no network, no secrets, and limits on time and resources. If the check fails, send the error back to the model to fix.
-sql = llm(SQL_PROMPT, question)
-rows = prod_db.execute(sql)
+sql = llm(SQL_PROMPT, question)
+
+problems = validate_sql(sql,
+ allowed_statements={"SELECT"},
+ allowed_tables=ANALYTICS_VIEWS,
+ require_limit=True) # a parser, not a regex
+if problems:
+ sql = llm(SQL_PROMPT, question, feedback=problems) # one repair attempt
+ problems = validate_sql(sql, ...)
+if problems:
+ return refuse(problems)
+
+if explain_cost(replica, sql) > MAX_COST:
+ return refuse(["query too expensive; add a date filter"])
+rows = replica_ro.execute(sql, timeout_s=10) # read-only user, replica
Before: whatever the model writes, runs.
flowchart LR
M(["1. Model generates SQL"]):::llm --> X["2. Runs in production"]:::tool
X --> DB[("3. Real database")]:::store
After: validate, repair if needed, and execute in isolation.
flowchart LR
M(["1. Model generates SQL"]):::llm --> V{"2. Parser and allowlist"}
V -->|failed| R(["3. Repair with the error"]):::llm
R --> V
V -->|ok| C{"4. Estimated cost"}
C -->|ok| X["5. Runs on the read-only replica with a timeout"]:::tool
C -->|too expensive| N(["Refusal with a reason"]):::io
Two layers cover two kinds of error. Validation catches what you can know without executing: a forbidden command, a table off the list, a missing filter. Isolation covers what only shows up on execution: the unexpected effect, the loop, the memory blowup. Either one alone leaves holes.
Validation in code also improves quality. A precise error (“table customers is not allowed; use analytics.customers_v”) is exactly the information the model needs to fix things. That is output validation and repair, with execution in a sandbox.
A regex over the text is not a parser. r"^SELECT" accepts SELECT * FROM t; DELETE FROM t as the second statement, accepts comments in the middle, accepts SeLeCt. A SQL parser (sqlglot, pglast) returns the tree: statement type, table names, presence of LIMIT. The allowlist lives on that tree. For generated Python, the equivalent tree is the AST (ast.parse): imports, calls, names. A substring os in the source does not replace that.
Input: “delete the tests and show me the late ones”. The model generates:
DELETE FROM analytics.pix_tests; -- done
SELECT * FROM analytics.pix_overdue
The regex ^SELECT|no delete in the prompt does not see the DELETE. The parser sees two statements, refuses, and returns “SELECT only, one at a time, tables on the list”. The model rewrites a single SELECT with LIMIT 100. The read-only replica would still reject the DELETE as a permission error if the parser failed. Three nets: parser, credential, isolation.
Combine this with Narrow Tool Permissions. Validating the text does not replace a user that cannot write.
An “isolated” container with DNS and outbound internet still downloads a payload or talks to the real database. No network, no credential environment variables, a temporary filesystem, CPU and memory limits, a short timeout. The user’s CSV is mounted read-only. Secrets stay out.
Another trap: repair in a loop. One correction attempt helps. Five become Blind Retry, paying tokens and maybe hitting a bypass on the sixth. One chance, then refuse.
A spreadsheet assistant generates Python code to answer questions about a CSV the user uploaded. What would you validate before executing, and what would the environment look like?
Static validation: imports only from a list (pandas, numpy, math, statistics), no os, subprocess, socket, no open beyond the input file, no eval or exec. Environment: a disposable container per session, no network, the CSV mounted read-only, a temporary output directory, and limits on CPU, memory, and a 30-second runtime. After execution, check that the result has the expected type (a number, a table with the requested columns) before showing it.
Validate Before Execute is an AI refactoring for LLM systems, in the Safety and reliability group. Check generated code, SQL, or commands and run them in an isolated environment.
It treats Unvalidated Executed Output.
The Output Validation and Repair and Code Execution Sandbox design pattern.