agentic-data-analyst
One assistant for your data and your docs: people ask in plain language and get answers they can check, at a cost you set. Numbers come from your DuckDB data through SQL that Claude writes and a second agent reviews; explanations come from your Markdown pages through a full-text search, and every page used is cited. When neither the data nor the docs hold the answer, the assistant says so instead of guessing. Deterministic guards decide what may run and what it may cost.
It runs RetroSpeedLab's Ask me anything: visitors at
ask.retrospeedlab.com and /ask on Discord ask about the AI's speedruns, records,
games, highscores and streams, and how the studio works. Both come from one nightly DuckDB snapshot: the lake's public
tables and the docs-site and website pages, answered on a cheap model within a daily budget.
Without data tables it is a website chat: a company answers visitors from its own pages, each answer linking them.
The documentation covers getting started, how it works, the API reference and the RetroSpeedLab case study.
Why
- Self-service answers without a dashboard or a support ticket for every question, and without database access.
- Answers you can check: every figure comes from a query you can inspect, every explanation links its page.
- No guessing: the model answers only from what its tools returned and says when they do not hold the answer.
- Safe by construction: one read-only, sandboxed
SELECTover an allowlist of tables; the guards hold whatever the model is told. - Predictable cost: a cheap model, cached answers, per-person rate limits and a hard daily budget.
- Nothing extra to host: the docs index is BM25 in the same DuckDB file as the data; no embeddings, no vector store.
Modes
The mode is part of the Domain you configure; it is never guessed from the database.
| Mode | Answers from | Tools | Example |
|---|---|---|---|
data_and_docs |
Tables and pages | search_docs and the data tools |
RetroSpeedLab's Ask me anything |
docs |
Pages only; politely declines data questions | search_docs |
A company website chat |
data |
Tables only | list_tables, describe_table, run_sql, show_table, show_chart |
An internal data desk |
Use it as a library
uv add agentic-data-analyst
Index the pages into the DuckDB file once per snapshot, next to the data tables:
from pathlib import Path
from agentic_data_analyst import index_docs, read_pages
pages = read_pages(Path("docs-site/pages"), lambda page: f"https://docs.retrospeedlab.com/{page.with_suffix('.html').as_posix()}")
index_docs(Path("retrospeedlab.duckdb"), pages, "porter") # the stemmer for the pages' language, e.g. "german"
Each page is split at its headings into passages with the page title, heading, URL and text. Pages built some other
way go in as Page(url, markdown).
Then answer questions:
from datetime import UTC, datetime
import anthropic
from agentic_data_analyst import (
Analyst,
AnswerCache,
DailyBudget,
Domain,
GuardedAnalyst,
Limits,
Models,
RateLimit,
connect,
network_key,
)
domain = Domain(
about="RetroSpeedLab, a game studio run by AI agents: an AI speedruns its own 8-bit games and humans race it",
mode="data_and_docs",
sql_rules="- Lower seconds are better; only won runs have a record.",
review_rules="- times compared across games without saying so.",
)
limits = Limits(max_question_chars=300, max_model_rounds=6, max_queries=3, answer_budget_usd=0.05)
models = Models(analyst="claude-haiku-5-5", analyst_effort="low", reviewer="claude-haiku-5-5", reviewer_effort="low")
analyst = Analyst(anthropic.AsyncAnthropic(api_key="..."), connect(Path("retrospeedlab.duckdb")), models, limits, domain)
answer = await analyst.answer("How does the speedrun search work, and which game has the fastest AI record?")
# answer.text, answer.queries (SQL with rows), answer.passages (the pages it found), answer.usage, answer.complete
guarded = GuardedAnalyst(
analyst,
DailyBudget(Path("state/budget.json"), 2.00, limits.answer_budget_usd, lambda: datetime.now(UTC)),
AnswerCache(500, 6 * 3600),
burst=RateLimit("20/minute"),
networks=RateLimit("15/hour"),
)
visitors = RateLimit("5/hour")
reply = await guarded.ask("Which game has the fastest AI record?", visitors, "web:203.0.113.7", network_key("203.0.113.7"))
A docs-only website chat indexes only its pages and sets
Domain(about="...", mode="docs", sql_rules=None, review_rules=None).
Analyst.converse(history, observe) answers within a chat history and reports each step, for chat UIs.
The search uses DuckDB's fts extension. index_docs installs it; the sandboxed connection only loads it, so install
it once where the app runs, e.g. at image build:
python -c "import duckdb; duckdb.install_extension('fts')".
Guards
| Guard | What it stops | Where |
|---|---|---|
| Question check | Empty and over-long questions, before any model call | check_question |
| Answer cache | Paying twice for the same question (case and spacing ignored); only complete answers are kept | AnswerCache |
| Rate limit | One person asking too much, per asker key in a moving window | RateLimit (limits) |
| Network limit | One host rotating addresses: a limit per IPv4 /24 or IPv6 /64 | network_key, GuardedAnalyst |
| Burst cap | A flood from many askers: a limit on everyone's questions per minute | GuardedAnalyst |
| Daily budget | The total bill: released in 24 hourly shares that roll forward within the UTC day, so a flood spends only what is released; each question reserves its answer budget first, so parallel questions cannot pass the limit together; spending survives restarts | DailyBudget |
| Rounds and queries | Runaway loops: at most max_model_rounds model calls per question, docs searches included, and max_queries reviewed queries |
Limits |
| Answer budget | One expensive answer: it stops before the next call once it cost more than answer_budget_usd |
Limits |
| Prices | Unbounded bills: a model without a known price is refused at start | Models |
| Mode | Data tools in a docs-only chat and the docs search in a data-only one | Domain |
| Statement | Anything but exactly one SELECT (DDL, DML, COPY, ATTACH, SET, INSTALL), from DuckDB's parser |
check_sql |
| Allowlist | Tables outside the file's own (the docs index included), file and system table functions, from DuckDB's syntax tree | check_sql |
| Sandbox | Writes, files, network and further extensions: read-only, enable_external_access=false, 1 GB, 2 threads, locked settings |
connect |
| Runtime | Slow and huge queries: 20 s deadline, 200 rows, 8,000 characters of SQL; at most 5 passages of 2,000 characters per search | run_query, search_docs |
The limits count from the narrowest out (asker, network, everyone), so one asker over their own limit uses up nobody else's share. The reviewer agent catches wrong SQL; the deterministic layers keep the system safe whatever either model does. Docs searches read public pages, so they need no review. An answer may overshoot its budget by the one model round that crossed it; the daily budget books the real cost.
Flow
flowchart LR
Q[Question] --> G{Guards}
G -->|ok| A[Assistant]
A -->|search_docs| P[(Pages, BM25)]
P -->|passages with URLs| A
A -->|list_tables / describe_table| M[(Schema only)]
A -->|run_sql| C{Deterministic checks}
C -->|ok| R[SQL reviewer agent]
R -->|approved| D[(DuckDB, read-only)]
D -->|rows| A
A --> Ans[Answer with its queries and cited pages]
Demo chat
A Chainlit chat over a synthetic database of humans and AI agents racing five RetroSpeedLab
games and two demo pages in data/docs (the levels, rules, runners and runs are invented), with tables and Plotly
charts on request.
make install
cp .env.example .env # set ANTHROPIC_API_KEY
make run # builds data/speedruns.duckdb with the pages, then opens http://localhost:8000
make check # ruff, strict mypy, pytest-bdd; the models are scripted, so no key is needed
Docker: docker build -t agentic-data-analyst . && docker run --rm -p 8000:8000 --env-file .env agentic-data-analyst
Layout
| Module | Responsibility |
|---|---|
agent.py |
Analyst: prompt, tools per mode, the bounded tool loop |
docs.py |
Pages into passages, the full-text index, search_docs |
reviewer.py |
The SQL reviewer agent (structured output) |
database.py |
Sandboxed connection, schema inspection, SQL checks, bounded execution |
guards.py, guarded.py |
The guards and GuardedAnalyst, which puts them in front of an analyst |
config.py, usage.py |
Models, Limits, Domain with its mode; token cost |
charts.py |
Chart requests the analyst may make |
app.py, figures.py, seed.py, settings.py, log.py |
The demo (demo extra) |
Limitations
- BM25 matches words, not meaning: a question worded unlike the pages may find nothing. The assistant then searches once more with other words, or says it does not know.
- Prompt injection can still make the analyst write a different
SELECT; the guards limit any query to reading the allowed tables within the deadline, the row cap and the budget. - Rate limits and the cache live in one process; run one process per database, or put both in a shared store.
- Answer quality and injection resistance against the real models are not covered by the automated tests.
License
MIT. See LICENSE.
Metadata
Release files for agentic-data-analyst 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| agentic_data_analyst-0.4.1.tar.gz | 32.3 kB | With provenance | Details for agentic_data_analyst-0.4.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| agentic_data_analyst-0.4.1-py3-none-any.whl | Python 3 | none | any | Details for agentic_data_analyst-0.4.1-py3-none-any.whl |
Total release size: 69.7 kB
Release files / agentic_data_analyst-0.4.1.tar.gz
| Download URL | agentic_data_analyst-0.4.1.tar.gz |
|---|---|
| Size | 32.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
34b6e661f6bde38f67a77efa0f3c064b9723aa2178934ecaff104d58fd7eb5d4
|
|
BLAKE2b-256 checksum How to use checksums |
7e77e2b1a93a42ce87894bb76ab92805132503c3e6f1390d5adb13d405c99af2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 3, 2026.
Transparency logRelease files / agentic_data_analyst-0.4.1-py3-none-any.whl
| Download URL | agentic_data_analyst-0.4.1-py3-none-any.whl |
|---|---|
| Size | 37.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7cf5def02ef19f235692e523c324a0ade40b5cad33f7150813231e75b898dbe9
|
|
BLAKE2b-256 checksum How to use checksums |
eae21803a4d7da997a07138681f867594baf6b8511c8c653157953af94ecbb84
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 3, 2026.
Transparency logSecurity for agentic-data-analyst 0.4.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for agentic_data_analyst-0.4.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 3, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.