Guide

BrainBox for analytics agents: context from BrainBox, SQL from your agent

A pattern for an agent that answers business questions over your database: BrainBox supplies the schema, metric definitions and rules over MCP; your agent writes and runs the SQL on its own connection. BrainBox does not run SQL.

Updated

An analytics agent needs two things: to know what your data means, and to run queries. BrainBox does the first. BrainBox does not run SQL. Your agent keeps its own database connection, with the permissions you choose, and executes the queries it writes.

This split keeps database credentials and query results on your side: the CLI and SDK connect to your database locally and upload schema metadata, not credentials or results. Opt-in column profiling (`--profile-data`) also uploads statistics derived from rows, such as enum values; leave it off if that is not acceptable for your data.

What BrainBox supplies

  • Schema: entities, columns with descriptions, keys and join relationships, and which physical table backs each entity, from a database you ingest (Postgres, Trino, Athena) plus SQL files and dbt artifacts.
  • Metric definitions and vocabulary: metrics and business terms such as "net revenue" or "active customer", saved with `save_domain` (SQL select, where and joins, plus the physical table of each concept) and read back with `recall_domain`. You can also keep definitions in a reviewed markdown file that you sync into the Brain; the agent finds those with `recall_wiki`.
  • Rules and corrections: governing rules, and learnings your agent writes back when a user corrects an answer, with review before they take effect.

A cross-table definition such as net revenue (items minus refunds) can be saved as structured data: see Metric definitions agents can query for the call. BrainBox stores the SQL and returns it; your agent runs it.

What your agent does

Per question, the agent works in this order:

  • Once per session, call `get_index` to orient.
  • Call `recall_domain` for the metric, term or rule the question uses, then for the entities involved to get columns, keys and join paths.
  • Call `recall_wiki` for definition prose in your synced docs.
  • Write the SQL and run it with your own read-only tool: a read-only database role, a statement timeout, a row cap, and an EXPLAIN check before execution.
  • Explain the answer and cite the definition used. If the user corrects it, write the correction back with `create_learning`; if the Brain lacked something, call `report_context_gap`.

In TypeScript, `brain.recall.asTools()` and `brain.update.asTools()` from the SDK give your model those tools directly. The TypeScript agent walkthrough shows the loop end to end, and the MCP endpoints work with any client.

Set it up

Ingest the database and your SQL and docs, stopping before publish so you can review what ingestion proposes:

npx -y @brainbox-labs/cli@latest ingest postgres://user@host/db ./sql ./docs --review

Then keep the definitions file in step with the Brain from CI:

npx -y @brainbox-labs/cli@latest sync ./docs/metrics --namespace defs

Re-run `ingest` after schema migrations. BrainBox does not watch your database for drift on its own.

What to build yourself

  • The question-to-SQL loop and the SQL execution tool, including read-only safety.
  • A set of questions with expected results, run on every definition change. This is the highest-value piece.
  • Review of what ingestion and learnings propose before it is published.

Building an analytics agent? Talk to us, or see Data agents and text-to-SQL.

Give your agents a brain.

Grounded answers, not guesses — from a living model of your business.

Book a Demo