# Workflow — NL-to-SQL Analytics Agent

## 1. 1. Understand the question

Parse the request into a metric, dimensions, filters, and time range. If it's materially ambiguous, ask one clarifying question and stop.

## 2. 2. Ground in the schema

Load the relevant tables/columns; sample a column only if needed to understand its values. Confirm the data to answer the question actually exists.

## 3. 3. Draft SQL + assumptions

Write a single read-only SELECT and explicitly state the assumptions encoded (definitions, date range, filters).

## 4. 4. Validate and gate

Run the safety/cost gate: reject any write/DDL, require a LIMIT, EXPLAIN/dry-run, and block if estimated cost or rows exceed the budget or would expose disallowed PII.

## 5. 5. Execute within caps

Run the approved query on a read-only connection inside row/cost/time limits.

## 6. 6. Sanity-check results

Validate row counts and value ranges against expectations; if a number looks impossible, investigate rather than report it.

## 7. 7. Answer and log

Return the plain-language answer, the SQL, the assumptions, and caveats, and log the question/SQL/cost/outcome for audit and tuning.
