Validate agent SQL before running
Agents hallucinate table names and mismatch placeholder counts. db.validate() parses and bind-checks SQL server-side without executing it, surfacing the same structured error detail a real query would return — for zero row reads. Wire it into the agent loop as a pre-flight gate.
import { PerSQL } from "@persql/sdk";
const persql = new PerSQL({ token: process.env.PERSQL_TOKEN! });const db = persql.database("acme/orders");
async function planAndRun(sql: string, params: unknown[]) { const check = await db.validate(sql, params); if (!check.ok) { console.log("Validation failed:", check.error); console.log("Detail:", check.errorDetail); return check; } return db.query(sql, params);}
// Catches unknown table before a query burns budgetawait planAndRun("SELECT * FROM ordrs WHERE id = ?", [1]);
// Catches param mismatchawait planAndRun("SELECT * FROM orders WHERE id = ? AND status = ?", [1]);Validation uses the same parser as execution, so false positives are impossible. Errors include the exact kind, table, column, and hint the agent needs to self-correct without retrying.