Building a data agent that would rather say "I don't know"
The brief was short: a chat agent that answers questions about US census data stored in Snowflake. Two constraints shaped everything. The answer must arrive within 60 seconds. And it must come from the data. If the agent cannot find the answer, it should exit cleanly and say so, not invent a plausible number.
Start simple, then justify every step up
I always start with the simplest version and ask why it is not enough.
- Prompt chaining? Not enough. The questions vary too much, and the agent has to look things up before it knows what to query.
- A single agent with tools? Probably enough. One LLM, a few tools, a loop.
- A supervisor with sub-agents, routers, or parallel workers? Considered, and left out. They add latency and failure points, and the 60-second limit did not leave room for coordination overhead.
So the core is one agent loop in LangGraph, with a validator in front.
Understanding the data first
The biggest uncertainty was the data, not the AI. The census dataset has thousands of variables in coded tables, and many values come as brackets rather than plain numbers. What helped was writing the questions a real user would ask and working out by hand how to answer them from the tables. That exercise decided the tools.
The tools
- Prepared metric lookup: semantic views for the common measures (population, income, age), so frequent questions skip SQL generation entirely.
- Median from brackets: a tool that estimates a median from bracket tables, because the model is bad at that arithmetic and Snowflake is good at it.
- Catalogue search: finds the right census variable for a concept.
- Read-only SQL: one query at a time, through a SQL guard and a read-only Snowflake role with row limits.
- Playbooks: short local instructions for recurring calculations.
The model never does the math. Snowflake returns rows, and the model writes the answer from them.
The validator
Before the agent runs, a validator checks the question. A phrase list blocks prompt injection and harmful text. Then a model decides whether the question is about this census data at all. Obvious US ranking questions skip that call to save time. Out-of-scope questions get a clear reply instead of an agent run.
Speed
The first version was slow, and most of the time was not the model. Every Snowflake query opened a new connection, about 3 seconds each, and a turn runs 5 to 8 queries. Reusing connections brought each query down to about 0.3 seconds. That one change did more for the time limit than any model swap.
Evaluation
I built a 45-case dataset with expected answers, required and forbidden tool calls, and required mentions. Each experiment runs in LangSmith with Python checks and an LLM judge, and the results are exported and logged as one row per experiment with the change that triggered it.
- Baseline (smaller model): 33 of 45 cases passed every check, with 0.80 correctness.
- After the model swap, same prompts, tools, and dataset: 39 of 45 passed every check, with 1.00 correctness from the judge, a p50 of about 12 seconds, and a p95 of about 18 seconds, inside the limit.
The log makes it possible to see what each change did instead of guessing.
What I left out on purpose, and what production would need
One process, one VM, an in-memory checkpointer, and a shared login do not scale. I chose them knowingly to spend the time on what the user sees: correct numbers, answers inside the limit, and clear replies when the agent cannot answer. For production I would split the frontend and API, use a persistent checkpointer, add per-user auth, cost monitoring, CI/CD, and online evaluation on real traffic.
How I used AI tools to build it
I used Cursor with the LangChain docs MCP server for best practices, plus read access to GCP, Snowflake, and LangSmith. The project has agent instructions with one safety rule: the coding agent only reads external services and never deletes anything there. It hands me the command instead.