haiku.rag/docs/capabilities/analysis.md
Yiorgis Gozadinos c137305468
Scope the limit notice and spend the cite window on own turns only
The request-limit notice said only the cite tool remained available, but
chat registers rag and analysis in one agent, so exhausting analysis
claimed rag_search was gone too. Scoped to the capability's own tools.

The cite window was counted over every model request once loaded, so
turns spent on another capability expired it before the model was ever
placed where citing was the obvious move. Count only requests whose
preceding response called one of this capability's tools; engagement is
also the only thing that can loop, which is all the bound guards against.

Also: _count_tool_traffic returns a named tuple rather than four bare
ints, and counts failures only for this capability's tools, so host-tool
retries and output-validation retries no longer read as its failures.
2026-07-30 19:14:14 +03:00

2.7 KiB

Analysis Capability

AnalysisCapability adds search, citations, and sandboxed Python computation over the document corpus. Use it for counts, aggregation, comparison, structural traversal, and section-scoped reading.

It is deferred by default, keeping its substantial instructions and tool schemas out of context until the model chooses to load it.

The default request limit is 30 model requests per question. Override it with create_capability(request_limit=...), or set request_limit=None to disable it. As with the RAG capability, create_capability(vision=...) overrides the image-attachment gate, defaulting to the configured analysis model's vision flag. At the limit, analysis_search and analysis_execute_code are removed while analysis_cite remains for two further requests that call an analysis tool, so the model can register citations before answering from gathered evidence. Requests spent on other capabilities do not count against that window. Other agent and capability tools remain available, and the budget resets for every agent run.

When qa.max_searches or analysis.max_executions runs out, the exhausted tool keeps failing rather than disappearing, and the instructions name it on every following request. Searching from inside analysis_execute_code does not count against qa.max_searches.

Tools

Tool Purpose
analysis_search(query, limit?) Search the corpus for evidence.
analysis_execute_code(code) Run Python against the virtual document filesystem.
analysis_cite(chunk_ids) Register retrieved or filesystem-derived chunk IDs.

The sandbox exposes documents under /documents/{document_id}/ with metadata.json, content.txt, items.jsonl, and toc.json.

Compose with RAG

from pydantic_ai import Agent
from haiku.rag.capabilities.analysis import create_capability as analysis
from haiku.rag.capabilities.rag import create_capability as rag

agent = Agent(
    "openai:gpt-5",
    capabilities=[
        rag(db_path="my.lancedb"),
        analysis(db_path="my.lancedb"),
    ],
)

For the high-level convenience API:

from haiku.rag.client import HaikuRAG

async with HaikuRAG("my.lancedb") as client:
    result = await client.analyze("Which quarter had the highest revenue?")
    print(result.answer)

State

When dependencies expose a state dictionary, AnalysisState is stored under "analysis". It contains the document filter, code execution log, searches, and citations. Per-run searches and executions reset automatically; the filter and citation index persist.

The capability lazily opens both LanceDB and the sandbox only after it is loaded and a tool requires them. Resources close at the end of the agent run.