Skip to content

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.

answer async

answer(question: str) -> Answer

One question, answered on its own.

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)

text instance-attribute

text: str

queries instance-attribute

queries: tuple[Query, ...]

passages instance-attribute

passages: tuple[Passage, ...]

usage instance-attribute

usage: Usage

complete instance-attribute

complete: bool

Query dataclass

Query(sql: str, result: QueryResult)

sql instance-attribute

sql: str

result instance-attribute

result: QueryResult

QueryResult dataclass

QueryResult(columns: list[str], rows: list[list[Value]], truncated: bool)

columns instance-attribute

columns: list[str]

rows instance-attribute

rows: list[list[Value]]

truncated instance-attribute

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)

input_tokens class-attribute instance-attribute

input_tokens: int = 0

output_tokens class-attribute instance-attribute

output_tokens: int = 0

cache_write_tokens class-attribute instance-attribute

cache_write_tokens: int = 0

cache_read_tokens class-attribute instance-attribute

cache_read_tokens: int = 0

cost_usd class-attribute instance-attribute

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.

analyst instance-attribute

analyst: str

analyst_effort instance-attribute

analyst_effort: Effort

reviewer instance-attribute

reviewer: str

reviewer_effort instance-attribute

reviewer_effort: Effort

Limits dataclass

Limits(max_question_chars: int, max_model_rounds: int, max_queries: int, answer_budget_usd: float)

Hard bounds per question; they hold whatever the model does.

max_question_chars instance-attribute

max_question_chars: int

max_model_rounds instance-attribute

max_model_rounds: int

max_queries instance-attribute

max_queries: int

answer_budget_usd instance-attribute

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.

about instance-attribute

about: str

mode instance-attribute

mode: Mode

sql_rules instance-attribute

sql_rules: str | None

review_rules instance-attribute

review_rules: str | None

data property

data: bool

docs property

docs: bool

Mode module-attribute

Mode = Literal['data', 'docs', 'data_and_docs']

Data and docs

connect

connect(path: Path) -> duckdb.DuckDBPyConnection

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)

url instance-attribute

url: str

markdown instance-attribute

markdown: str

Passage dataclass

Passage(title: str, heading: str, url: str, text: str)

title instance-attribute

title: str

heading instance-attribute

heading: str

url instance-attribute

url: str

text instance-attribute

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.

ask async

ask(question: str, rate: RateLimit, asker: str, network: str | None) -> Reply

network is the asker's network_key, or None where askers have no address, such as chat users.

renew

renew(analyst: Analyst) -> None

Answer from new data from now on; answers from the old data are dropped.

Reply dataclass

Reply(text: str, answer: Answer | None, outcome: Outcome)

text instance-attribute

text: str

answer instance-attribute

answer: Answer | None

outcome instance-attribute

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.

item instance-attribute

item: RateLimitItem = parse(rate)

limiter instance-attribute

limiter = MovingWindowRateLimiter(MemoryStorage())

allow

allow(asker: str) -> bool

minutes_left

minutes_left(asker: str) -> int

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.

reserve

reserve() -> Reservation

Reserve one answer's budget, unless the day's budget or the share released so far cannot cover it.

settle

settle(cost_usd: float | None) -> None

Replace one reservation with the real cost; an unknown cost counts as the whole reservation.

AnswerCache

AnswerCache(size: int, seconds: float)

Complete answers by question, ignoring case and spacing; cleared when the data changes.

get

get(question: str) -> Answer | None

put

put(question: str, answer: Answer) -> None

clear

clear() -> None

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