Reference¶
The library reads no environment and has no hidden defaults: an application passes every setting to the objects below, all importable from agentic_data_analyst. This page is generated from the code.
Analyst¶
Analyst ¶
Analyst(client: AsyncAnthropic, con: DuckDBPyConnection, models: Models, limits: Limits, domain: Domain)
Answers questions from one read-only DuckDB file - its tables, its docs or both, as the domain's mode says - within the given limits.
converse
async
¶
converse(history: list[BetaMessageParam], observe: ToolObserver) -> Answer
Answer the latest user message in history, appending every turn to it (append-only), for a chat.
Repeats analyst -> tools (with the reviewer inside run_sql) until the analyst answers, or the round limit or the cost budget is reached.
Answer
dataclass
¶
Answer(text: str, queries: tuple[Query, ...], passages: tuple[Passage, ...], usage: Usage, complete: bool)
Query
dataclass
¶
Query(sql: str, result: QueryResult)
QueryResult
dataclass
¶
QueryResult(columns: list[str], rows: list[list[Value]], truncated: bool)
Usage
dataclass
¶
Usage(input_tokens: int = 0, output_tokens: int = 0, cache_write_tokens: int = 0, cache_read_tokens: int = 0, cost_usd: float | None = 0.0)
Configuration¶
Models
dataclass
¶
Models(analyst: str, analyst_effort: Effort, reviewer: str, reviewer_effort: Effort)
The analyst writes the SQL; the reviewer only judges one query, so a cheaper model at low effort is enough.
Limits
dataclass
¶
Limits(max_question_chars: int, max_model_rounds: int, max_queries: int, answer_budget_usd: float)
Domain
dataclass
¶
Domain(about: str, mode: Mode, sql_rules: str | None, review_rules: str | None)
What the assistant answers about, from which sources, in the words the analyst and the reviewer need.
Data and docs¶
read_pages ¶
read_pages(folder: Path, url: Callable[[Path], str]) -> list[Page]
Every Markdown file under folder; url maps a file's path relative to the folder to its public URL.
index_docs ¶
index_docs(database: Path, pages: Sequence[Page], stemmer: str) -> int
Write the pages' passages and their full-text index into database; returns the number of passages.
stemmer is the language of the pages for DuckDB's fts, e.g. "porter" for English or "german". Building the index
downloads the fts extension once if it is missing; the read-only connection then only loads it.
Page
dataclass
¶
Page(url: str, markdown: str)
Passage
dataclass
¶
Passage(title: str, heading: str, url: str, text: str)
Guards¶
GuardedAnalyst ¶
GuardedAnalyst(analyst: Analyst, budget: DailyBudget, cache: AnswerCache, burst: RateLimit, networks: RateLimit)
Question check, cache, rate limits per asker and per network, burst cap and budget, in that order, in front of one analyst.
burst caps the questions of everyone together, e.g. RateLimit("20/minute"); networks caps the questions per
network (network_key), e.g. RateLimit("15/hour"). The per-asker rate comes with each question, per front end.
Reply
dataclass
¶
Reply(text: str, answer: Answer | None, outcome: Outcome)
check_question ¶
check_question(text: str, max_chars: int) -> str | None
A reply that rejects the question, or None if it may go to the model.
RateLimit ¶
RateLimit(rate: str)
At most rate questions per asker, e.g. "5/hour"; one limiter per front end, keyed by who asks.
network_key ¶
network_key(address: str) -> str
The network of a client address: its IPv4 /24 or IPv6 /64, e.g. "203.0.113.0/24"; raises ValueError for anything else.
DailyBudget ¶
DailyBudget(path: Path, limit_usd: float, reserve_usd: float, now: Callable[[], datetime])
Spending in USD per UTC day, kept in a JSON file so a restart does not reset it.
The day's budget is released in 24 equal hourly shares, and what an hour leaves unspent rolls forward within the day, so a flood of questions can only spend what has been released so far. Each question reserves the most one answer may cost before it starts, so questions running at the same time cannot together pass the limit; the real cost replaces the reservation when the answer is done.
AnswerCache ¶
AnswerCache(size: int, seconds: float)
Prices¶
Models accepts only models with a price in PRICES_PER_MILLION (USD per million input, output, cache-write and cache-read tokens), so every cost budget can be enforced. Add a model there to use it.
Demo chat settings¶
Only the Chainlit demo reads the environment, from .env (see .env.example). Every variable is required.
| Variable | Meaning |
|---|---|
ANTHROPIC_API_KEY |
The Anthropic API key |
ANALYST_MODEL, ANALYST_EFFORT |
The model that writes the SQL and the answer, and its effort (low to max) |
ANALYST_REVIEWER_MODEL, ANALYST_REVIEWER_EFFORT |
The model that reviews each query, and its effort |
ANALYST_ANSWER_BUDGET_USD |
The most one answer may cost |
ANALYST_SESSION_BUDGET_USD |
The most one chat may cost |
ANALYST_DATABASE |
The DuckDB file that make data writes |
LOG_LEVEL |
The console and log file level, e.g. INFO |