Skip to content
Agent loop · Data

Query Until Verified

Write a SQL query for a business question, run it on a read-only connection, and refine it until row counts, totals, and sanity checks reconcile.

Up to 6 iterationsStop rules and safety limits
The prompt

Copy the Query Until Verified loop

Stops when the query result passes every sanity check and reconciles with a known reference number, or after 6 iterations. Edit the settings block, then paste the whole prompt into your agent.

query-until-verified.prompt.md
You are running the "Query Until Verified" loop.

Settings (edit before running):
- Question: <for example, monthly active customers by plan for the last 6 months>
- Connection: read-only role on the analytics replica
- Reference number to reconcile with: <for example, last month's active customers on the billing dashboard>
- Max iterations: 6

Goal: a query whose result answers the question and passes every check below.

Before the loop: list the tables you plan to use, their primary keys, and the join keys between them. Confirm the time zone of timestamp columns.

Each iteration:
1. Write or revise the query. Use explicit joins and comment each filter.
2. Run these checks as separate queries, each with a LIMIT or aggregate:
   - Row count before and after each join (unexpected growth means a fan-out).
   - Duplicate check on the result's grain.
   - Null counts in key and grouping columns.
   - Min and max dates actually covered.
3. Compare the matching slice of the result with the reference number. Explain any difference.
4. Log: iteration, what changed, check results.

Stop when:
- All checks pass and the result reconciles with the reference (or the difference is fully explained). Success.
- You reach the max iteration count.

Rules:
- Read-only. Run SELECT statements only. No INSERT, UPDATE, DELETE, DDL, or temporary tables on shared databases.
- Always use LIMIT or aggregates when exploring. Never select entire large tables.
- Do not output personal data (emails, names, addresses). Aggregate or count instead.

Final report: the final query, the check results, the reconciliation, and assumptions a reader should know.
When to use it

When this loop fits

  • You need a number for a report and want more than a query that merely runs without errors.
  • A dashboard metric looks off and you want to rebuild it from raw tables.
  • You are exploring an unfamiliar schema and need the joins checked before trusting the result.
How it works

What happens on each pass

The agent repeats these steps until the stop condition holds or it runs out of iterations.

  1. Learn the schema. Inspect the relevant tables, keys, and column types before writing SQL.
  2. Draft the query. Write the query with explicit joins, filters, and time zone handling.
  3. Run sanity checks. Check row counts at each join, duplicates, nulls in key columns, and the date range covered.
  4. Reconcile. Compare the result with a reference number such as a dashboard value, invoice total, or count of raw events.
  5. Refine. Fix the query and repeat until every check passes.
Recommended servers

MCP servers for this loop

The 6 strongest of 1079 matching servers, ranked by how closely they fit and then by trust score.

More in Databases
  • Trust score 77 of 100, Good

    DBHub

    by bytebase · TypeScript

    Token-efficient database MCP server for PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite

    Registry
    LicenseQualityMaintenanceLicense: A. MIT is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 25 days ago.
    3.7K stars—/wk weekly downloads— tools
  • Trust score 72 of 100, Good

    Mnemosyne

    by 28naem-del · TypeScript

    Evidence-tracked local memory MCP server: one SQLite file; corrections retire dependent memories.

    Registry
    LicenseQualityMaintenanceLicense: A. MIT is a permissive open source license.Quality: C. 65/100 for listing completeness. Lost points: no listed tools, short summary.Maintenance: A. Last commit or release 11 days ago.
    153 stars—/wk weekly downloads— tools
  • SynaBun logo
    Trust score 71 of 100, Good

    SynaBun

    by danilokhury · JavaScript

    Persistent memory MCP server for Claude Code, Codex and OpenCode, stored locally in SQLite

    Registry
    LicenseQualityMaintenanceLicense: A. Apache-2.0 is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 16 days ago.
    70 stars—/wk weekly downloads— tools
  • Trust score 71 of 100, Good

    UN FAOSTAT global food & agriculture statistics over a local SQLite mirror, via MCP.

    RegistryRemote
    LicenseQualityMaintenanceLicense: A. Apache-2.0 is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 10 days ago.
    3 stars297/wk weekly downloads— tools
  • Trust score 70 of 100, Good

    dbmcp

    by haymon-ai · Rust

    Database MCP server for MySQL, MariaDB, PostgreSQL & SQLite with PII redaction and write-prevention

    Registry
    LicenseQualityMaintenanceLicense: A. MIT is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 21 days ago.
    33 stars—/wk weekly downloads— tools
  • Trust score 70 of 100, Good

    Long-term memory and context fitting for agents, with no LLM in the loop. Local SQLite.

    Registry
    LicenseQualityMaintenanceLicense: A. MIT is a permissive open source license.Quality: Not graded. No README or tool list has been captured yet.Maintenance: A. Last commit or release 9 days ago.
    24 stars—/wk weekly downloads— tools
Run it

How to run this loop

  1. Connect the tools. Install a server the loop can use, for example DBHub, or rely on your agent's built-in terminal and file access.
  2. Edit the settings block. Replace the example commands, targets, and limits with your project's own. Keep the iteration cap low on the first run.
  3. Paste it into an agent session. Start a fresh session in Claude Code, Cursor, or VS Code agent mode, in the repository root, and paste the prompt.
  4. Review before you merge. Read the final report and the diff. The loop leaves its work for you to review rather than shipping it.
FAQ

Query Until Verified questions

When does the Query Until Verified loop stop?

It stops as soon as the query result passes every sanity check and reconciles with a known reference number. It also stops after 6 iterations by default, or earlier when the prompt's own stop rules say it is stuck, and then reports what changed and what is left. Change the cap in the settings block at the top of the prompt.

How do I make sure the agent cannot change my database?

Connect the database MCP server with a role that only has SELECT privileges, ideally on a replica. Several database servers also have a read-only mode. The prompt forbids writes too, but database permissions are the guarantee that matters.

What is a join fan-out and why does the loop check for it?

A fan-out happens when a join matches one row to many, silently multiplying counts and sums. It is the most common reason a query runs cleanly but returns the wrong number, so the loop compares row counts before and after every join.

Keep exploring
All loops