Michael Schmuklermann
← All cases

Public dataset · Snowflake

Census data agent: a text-to-SQL agent on Snowflake that answers from data or not at all

A LangGraph agent that answers questions about US census data in Snowflake, within 60 seconds and only from the data. A validator, read-only SQL tools, and a 45-case eval set with an LLM judge keep it honest.

OrchestrationLangGraph · single agent with validator and evals
StackLangGraph · LangSmith · Snowflake · Streamlit · OpenAI · Docker · GCP
QuestionValidatorAgentRead-only SQLSnowflake does the mathAnswer or 'I don't know'
codeguardrailmodel stepagenthuman

In short

The task was a chat agent for US Open Census data in Snowflake, with two hard constraints: answer within 60 seconds, and never invent a number. If the data cannot answer, the agent says so or asks for more detail. I built one Python app: Streamlit for the chat page, running a LangGraph graph in the same process. A validator runs before the agent. A phrase list blocks injection and harmful text, and a small model then decides whether the question is about this census data. The agent is a single tool-using loop, not a multi-agent system, because one loop met the time limit and stayed easy to debug. Its tools are a prepared metric lookup on semantic views, a median estimate from bracket tables, a search of the census variable catalogue, one read-only SQL query with a SQL guard, and short playbooks for common calculations. Snowflake does the math, and the model only writes the answer from the returned rows. Reusing Snowflake connections cut query time from about 3 seconds to about 0.3, which mattered with 5 to 8 queries per turn. A 45-case eval set runs in LangSmith with Python checks and an LLM judge, and every bigger change is recorded as a new experiment row.

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.

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

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.

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.

Code on GitHubMore cases