Skip to content

Getting started

You need Python 3.12 or newer, uv and an Anthropic API key in ANTHROPIC_API_KEY.

Try the demo chat

The repository ships a Chainlit chat over a synthetic database of humans and AI agents racing five RetroSpeedLab games, plus two demo pages:

git clone https://github.com/datenwissenschaften/agentic-data-analyst
cd agentic-data-analyst
make install
cp .env.example .env   # set ANTHROPIC_API_KEY
make run               # builds data/speedruns.duckdb, then opens http://localhost:8000

Ask "How does the speedrun search work?" for a docs answer, "Which game has the fastest AI record?" for a data answer, or both at once. .env.example lists the demo's models and budgets.

Use it in your application

1. Install

uv add agentic-data-analyst

The docs search uses DuckDB's fts extension. The sandboxed connection only loads it, so install it once where the app runs, for example when you build the image:

python -c "import duckdb; duckdb.install_extension('fts')"

2. Build the DuckDB file

Write your data tables into the main schema of one DuckDB file with your own pipeline. Comments on tables and columns (COMMENT ON) and primary and foreign keys are what the analyst reads to understand them. Then index your Markdown pages into the same file, once per snapshot:

from pathlib import Path

from agentic_data_analyst import index_docs, read_pages

pages = read_pages(Path("docs/pages"), lambda page: f"https://docs.example.com/{page.with_suffix('.html').as_posix()}")
index_docs(Path("company.duckdb"), pages, "porter")  # the stemmer for the pages' language, e.g. "german"

Pages built some other way go in as Page(url, markdown).

3. Describe the domain

The mode is set, never guessed. With data, give the analyst the pitfalls of your tables and the reviewer the mistakes it should reject:

from agentic_data_analyst import Domain

domain = Domain(
    about="Example Ltd., a bakery chain with 40 shops",
    mode="data_and_docs",
    sql_rules="- Revenue is in cents; divide by 100 for euros.\n- Closed shops have closed_on set.",
    review_rules="- revenue shown in cents;\n- closed shops counted as open.",
)

4. Answer questions

import anthropic
from agentic_data_analyst import Analyst, Limits, Models, connect

analyst = Analyst(
    anthropic.AsyncAnthropic(),
    connect(Path("company.duckdb")),
    Models(analyst="claude-haiku-5-5", analyst_effort="low", reviewer="claude-haiku-5-5", reviewer_effort="low"),
    Limits(max_question_chars=300, max_model_rounds=6, max_queries=3, answer_budget_usd=0.05),
    domain,
)
answer = await analyst.answer("Which shop sold the most last month, and how do I order a cake?")

answer.queries holds each SQL query with its rows, answer.passages the pages it found, answer.usage the tokens and cost, and answer.complete whether it finished within its limits. For a chat UI, Analyst.converse(history, observe) answers within a conversation and reports each tool step.

5. Put the guards in front

Anything open to the public goes through GuardedAnalyst, which checks the question, the cache, the rate limits and the budget before the model may cost anything:

from datetime import UTC, datetime

from agentic_data_analyst import AnswerCache, DailyBudget, GuardedAnalyst, RateLimit, network_key

guarded = GuardedAnalyst(
    analyst,
    DailyBudget(Path("state/budget.json"), 2.00, 0.05, lambda: datetime.now(UTC)),
    AnswerCache(500, 6 * 3600),
    burst=RateLimit("20/minute"),
    networks=RateLimit("15/hour"),
)
visitors = RateLimit("5/hour")
reply = await guarded.ask(question, visitors, f"web:{address}", network_key(address))

reply.outcome says what happened (answered, cached, rate_limited, budget, ...), and reply.text is always ready to show. When the data changes, guarded.renew(new_analyst) switches to it and clears the cache.

Next: how it works.