Context
DataSays is a product I build mostly on my own, under its own GitHub organisation. The repo’s private, so I’ll stick to the shape of the system here. It started in late October 2025 as a Slack bot that answered questions over a BigQuery warehouse, then grew into a web app with its own sign-up, organisations, plans and billing.
I split it into three parts. A Next.js web app, a NestJS API with Prisma on Postgres, and a small FastAPI service that runs the Slack bot. The API does the real work. It holds the chat agent, the connectors, the ingestion queue and the retrieval graph.
What’s in it
The chat agent
A question goes to the API, which loads the chat history and the active data source, then calls streamText from the Vercel AI SDK with up to 25 tool steps. The tools are picked per source type, so a MySQL chat gets execute_sql and a Snowflake chat gets the Snowflake discover and call tools. The answer streams back word by word. I show each tool step as it starts, so you can see what it’s doing and where it went wrong.
The usual run is: generate SQL, execute it, generate analysis code if the question needs more than the raw rows, run that code, then render a chart or table. The same tools can build a dashboard or a slide deck, and decks export to PowerPoint.
Schema and metadata
A model won’t write good SQL unless it sees the real schema. When a source connects, the API pulls tables, columns, types, primary and foreign keys, nullability, sample values and basic stats into a metadata table. That metadata goes into the prompt, and the SQL prompts tell the model to use only those names.
CSV ingestion
I moved uploads off the request and onto a BullMQ worker. The worker streams the file in batches with fast-csv, so a large file is never fully in memory. Each batch is written to two places in parallel: embedded chunks in csv_chunks using OpenAI text-embedding-3-small and pgvector, and raw rows as JSONB in csv_rows. Progress is reported as bytes read. When it’s done, the source flips to active or failed.
The retrieval graph
CSV questions go through a LangGraph state graph before the agent sees them. A classifier using gpt-4.1-mini labels the question as sql, semantic or both. SQL questions go to text to SQL over csv_rows. Semantic ones go to hybrid search, which runs a pgvector query and a Postgres full text query and fuses them with reciprocal rank fusion. Compound questions run both.
A grading step keeps only the chunks that are relevant. If nothing useful came back, the graph rewrites the question and tries again. I capped that at two rewrites so a bad question can’t loop and keep spending calls. The result is added to the system prompt as context. If the graph fails, the chat doesn’t stop; it just goes on without it.
Guardrails on generated code
I don’t trust anything the model writes, so generated CSV SQL is cleaned and checked before it runs. It must start with SELECT, must not contain write or DDL keywords, and must hit csv_rows. If it doesn’t filter by data source, the filter is added. Generated analysis code runs in a Node worker thread against at most 50,000 rows, and the worker is terminated after 30 seconds.
Accounts and billing
Users sign in with email OTP or Google. I wanted billing in from the start, since every question costs me model calls. Chats belong to users and organisations, with roles and invitations. Plans live in the database and are synced with Stripe. A quota service enforces daily, monthly and total question limits per plan and returns a typed error the web app can show as an upgrade prompt.
Constraints
- Mostly one developer across the web app, API, agents and deployment.
- Every query and every piece of analysis code is written by a model, so nothing it produces can be trusted to be safe or correct.
- Each warehouse has different auth and query APIs, and the agent has to handle all of them without one code path per question.
- LLM calls cost money, so usage has to be limited per plan from the start.
Results
- Five source types connected: CSV, MySQL, BigQuery, Snowflake and HubSpot.
- Answers come back as text, tables, charts, dashboards or PowerPoint decks.
- Analysis code is capped at 50,000 rows and 30 seconds per run.
- The retrieval graph retries at most twice, so a bad question can’t loop.
- Stripe subscriptions and per plan question limits are in place.