How this was built: Agentic AI

The AI SQL Agent lets people who don't know SQL get data from a PostgreSQL or SQLite database by asking in plain English. I treated SQL generation like compiling code: the output has to pass strict checks before it runs. Pydantic validates each query against the database schema, so the agent returns SQL that actually runs instead of a guess.
If a query fails or returns no rows, the agent reads the error, adds it to its context, and tries again with a fixed query. This reflection loop works a lot like a developer debugging their own code. It makes the agent much more reliable on complex joins and aggregations, and the user doesn't need to step in.
The agent only has read-only access to the database, so it can't change or delete data. To save tokens and time, it only loads the table schemas that matter for each question. Results come back as structured JSON with both the SQL query and the data, so they are easy to plug into dashboards or reporting tools.