# Give your AI agent a Beancount ledger with bea

Hand an external AI agent two bookkeeping jobs and keep every ledger change under your review: import a bank CSV with a preview-first check, then answer a balance question from query and report output.

Install `bea` with the [CLI quick start](https://beancount.io/docs/Basics/bea-cli). Local import, query, and report commands need no Beancount.io account. Your chat agent needs only its own subscription or API key. Only the built-in `bea ask` helper needs hosted credentials, and these recipes do not use it.

Choosing between tools? [Five accounting tools for AI agents](/compare/best-accounting-tools-for-ai-agents) compares Beancount.io, hledger, Xero, QuickBooks Online and Puzzle on the same agent-workflow criteria, with dated sources.

## What the agent does and what bea does

The agent proposes categories and drafts queries. Bea validates the ledger structure and computes the totals. You run every command below yourself. The agent never writes your ledger directly.

## Recipe 1: import a statement with agent-proposed categories

Turn a bank export into validated entries. The agent suggests the categorization rules. Preview and duplicate review decide what gets written.

### Prerequisites

You need `bea` and a fresh demo directory. No account and no Python importer.

### Prepare the inputs

Create the directory and the ledger. Explicit `--file` targets keep every command pointed at the demo books.

```bash
mkdir ai-agent-demo && cd ai-agent-demo
bea --no-input init books --currency USD --date 2026-08-01 --opening-balance "Assets:Checking 1000"
bea --file books/main.bean add open --date 2026-08-01 --account Expenses:Transport:Fuel -c USD
```

Save this sample as `statement.csv`:

```csv
Date,Payee,Narration,Amount
2026-08-02,Whole Foods,groceries,-20.00
2026-08-03,Shell,gas,-40.00
2026-08-04,Unknown Shop,mystery,-9.99
```

Save these categorization rules as `rules.toml`:

```toml
[[rule]]
match = "whole foods|trader joe|corner market"
account = "Expenses:Groceries"

[[rule]]
match = "shell|chevron|exxon"
account = "Expenses:Transport:Fuel"
```

### Copy the task prompt

Paste this prompt to your external agent along with the CSV contents:

```text
Propose Beancount categorization rules for my bank CSV rows.
Suggest one TOML rule per merchant with a match pattern and a
destination expense account. Never change dates, payees, or amounts.
Leave rows you cannot categorize confidently unmatched: they post to
Expenses:Uncategorized with flag ! for my review. I run bea import
preview and apply locally. You do not write my ledger.
```

### Run the CLI equivalents

Preview first, then apply the reviewed entries and verify the result:

```bash
bea --file books/main.bean import statement.csv --csv date=Date,amount=Amount,payee=Payee,narration=Narration --account Assets:Checking --rules rules.toml
bea --file books/main.bean import statement.csv --apply
bea --file books/main.bean check
bea --file books/main.bean list transaction --flag '!'
bea --file books/main.bean query "SELECT account, sum(position) WHERE account = 'Assets:Checking' GROUP BY account"
```

### Check the expected output

The preview reports `3 ready, 0 exact duplicates, 0 possible duplicates` and exits 0. It writes nothing. The apply run reports `Wrote 3 entries`. The check prints nothing and exits 0. The `!` queue lists the one unmatched row from `Unknown Shop` at `-9.99 USD`. The balance query reports `930.01 USD` in checking. Repeating the apply reports `0 ready, 3 exact duplicates` and writes 0 entries.

### Review and correct

Recategorize the `!` row deliberately in your ledger after reading it. A passing check only proves the ledger balances. It says nothing about whether `Expenses:Uncategorized` is the right account. For duplicate policy, conflict handling, and the advanced Python path, follow the [bank CSV walkthrough](https://beancount.io/docs/Solutions/import-bank-exports-cli).

## Recipe 2: answer a balance question without writes

Answer one question from the demo books: how much went to groceries and fuel in August 2026. The agent drafts the query. Read-only commands produce the answer.

### Prerequisites

You need the demo ledger from recipe 1 or your own books. Every command below is read-only.

### Copy the task prompt

Paste this prompt to your external agent:

```text
Draft a Beancount query (BQL) that answers: how much did I spend on
groceries and fuel in August 2026. Use my account names exactly as
listed by bea list open. Propose read-only commands only: bea query
or bea report with an explicit --file target. Do not propose ledger
edits to fix a surprising total. A surprising total means the query
or the books need review, not a compensating entry.
```

### Run the CLI equivalents

Run the drafted query against an explicit ledger target, then cross-check with a report:

```bash
bea --file books/main.bean query "SELECT account, sum(position) WHERE account ~ 'Expenses' GROUP BY account"
bea --file books/main.bean report income-statement --time 2026-08
```

Machine consumers add the global `--json` flag before the subcommand:

```bash
bea --file books/main.bean --json query "SELECT account, sum(position) WHERE account ~ 'Expenses' GROUP BY account"
```

JSON envelopes, exit codes, and global flag placement follow the [CLI reference](https://beancount.io/docs/bea-cli-reference). The [query language guide](https://beancount.io/docs/Basics/beancount-query-language) documents the full BQL surface.

### Check the expected output

The query reports `20.00 USD` for `Expenses:Groceries`, `40.00 USD` for `Expenses:Transport:Fuel`, and `9.99 USD` for `Expenses:Uncategorized`. Groceries plus fuel is `60.00 USD`. The income statement agrees: `69.99 USD` of August expenses with a net profit of `-69.99 USD`. The JSON variant returns the same rows inside `data.rows` with column metadata in `data.columns`.

### Review and correct

A query against a wrong account name prints `(no rows)` on stderr and still exits 0. Compare the name with `bea list open` output and run the query again. Never correct a surprising total by editing the ledger to match the answer. Fix the query or fix the books, then re-run `bea check`.

## First challenge: balance three synthetic rows

A portable first challenge with explicit inputs and an independently derived answer. Download the three files below, run the import, and check the two totals. No account and no product checkout.

Open books on 2026-09-01 in USD with 1000 USD in checking. Post three September rows: a 2000.00 USD client payment, a 29.00 USD hosting bill, and a 12.50 USD cafe visit. Checking ends at `2958.50 USD`, from 1000 + 2000 - 29 - 12.50. September profit is `1958.50 USD`, from 2000 - 29 - 12.50. Both expectations come from that arithmetic, not from report output.

### Prepare the inputs

Create the directory and open the two accounts the template does not carry. `Expenses:Dining` already comes from the CLI template.

```bash
mkdir agent-challenge && cd agent-challenge
bea --no-input init books --currency USD --date 2026-09-01 --opening-balance "Assets:Checking 1000"
bea --file books/main.bean add open --date 2026-09-01 --account Income:Consulting -c USD
bea --file books/main.bean add open --date 2026-09-01 --account Expenses:Software -c USD
```

Those three commands produce the ledger below. Download it as <a href="/downloads/agent-accounting/main.bean" download>main.bean</a> instead of typing it.

```beancount
option "title" "Personal ledger"
option "operating_currency" "USD"

; Add more accounts with bea add open. Amounts on credit accounts are negative.
; bea import books rows it cannot categorize to Expenses:Uncategorized with flag '!'.
2026-09-01 open Assets:Checking USD
2026-09-01 open Assets:Savings USD
2026-09-01 open Assets:Cash USD
2026-09-01 open Liabilities:CreditCard USD
2026-09-01 open Income:Salary USD
2026-09-01 open Income:Interest USD
2026-09-01 open Expenses:Groceries USD
2026-09-01 open Expenses:Dining USD
2026-09-01 open Expenses:Rent USD
2026-09-01 open Expenses:Transport USD
2026-09-01 open Expenses:Utilities USD
2026-09-01 open Expenses:Fees USD
2026-09-01 open Expenses:Uncategorized USD
2026-09-01 open Equity:OpeningBalances USD

2026-09-01 * "Opening balances"
  Assets:Checking          1000 USD
  Equity:OpeningBalances  -1000 USD

2026-09-01 open Income:Consulting USD

2026-09-01 open Expenses:Software USD
```

Save this statement as `statement.csv`, or download it as <a href="/downloads/agent-accounting/statement.csv" download>statement.csv</a>:

```csv
Date,Payee,Amount
2026-09-02,Client,2000.00
2026-09-03,Hosting,-29.00
2026-09-04,Cafe,-12.50
```

Save these rules as `rules.toml`, or download it as <a href="/downloads/agent-accounting/rules.toml" download>rules.toml</a>. Each rule names an account the starter ledger opens.

```toml
[[rule]]
match = "client"
account = "Income:Consulting"

[[rule]]
match = "hosting"
account = "Expenses:Software"

[[rule]]
match = "cafe"
account = "Expenses:Dining"
```

### Run the challenge

Preview first, then apply and verify. Amounts, dates, field names, and account names are identical in every locale. Only the prose around them is translated.

```bash
bea --file books/main.bean import statement.csv --csv date=Date,amount=Amount,payee=Payee --account Assets:Checking --rules rules.toml
bea --file books/main.bean import statement.csv --csv date=Date,amount=Amount,payee=Payee --account Assets:Checking --rules rules.toml --apply
bea --file books/main.bean check
bea --file books/main.bean query "SELECT account, sum(position) WHERE account = 'Assets:Checking' GROUP BY account"
bea --file books/main.bean report income-statement --time 2026-09
```

### Check the expected output

The preview reports `3 ready, 0 exact duplicates, 0 possible duplicates` and exits 0. The apply run reports `Wrote 3 entries`. The check prints nothing and exits 0. The balance query holds `2958.50 USD` in checking. The income statement shows `2000.00 USD` of Consulting income against `41.50 USD` of expenses, for a net profit of `1958.50 USD`. Repeating the apply reports `0 ready, 3 exact duplicates` and writes 0 entries.

## When a recipe becomes a repeat job

The prompts above suit one-off tasks driven by hand. Jobs you repeat weekly suit the versioned skills in the product repository. Each link below opens the skill's current source, which carries its own setup and trigger instructions. This CMS keeps no copy of these files.

- Start a fresh ledger from an empty directory with [beancount-init](https://github.com/bex-co/beancount-io/tree/main/skills/.claude/skills/beancount-init). It creates the ledger file, a Fava runner, and a Python project.
- Turn a bank export into categorized entries with [beancount-import](https://github.com/bex-co/beancount-io/tree/main/skills/.claude/skills/beancount-import). It stages every row, suggests categories from ledger history, skips rows already imported, and writes only after confirmation. Recipe 1 above is the manual form of this job.
- Prove one account matches its statement with [beancount-reconcile](https://github.com/bex-co/beancount-io/tree/main/skills/.claude/skills/beancount-reconcile). It diffs the ledger against the statement and lands a period-end balance assertion after confirmation.
- Answer a question from the books with [beancount-ask](https://github.com/bex-co/beancount-io/tree/main/skills/.claude/skills/beancount-ask). It runs read-only queries and shows each query beside its figure. Recipe 2 above is the manual form of this job.
- Close out a month with [beancount-close](https://github.com/bex-co/beancount-io/tree/main/skills/.claude/skills/beancount-close). It reconciles every active account, checks recurring entries and review flags, and records the close as a commit.

The full collection lives in the [canonical skills tree](https://github.com/bex-co/beancount-io/tree/main/skills/.claude/skills).

## Give a shell-capable agent bea, or load a skill into a client

These are two separate steps. Installing `bea` never installs or configures skills.

A shell-capable agent needs the command on its path. Install it with the [CLI quick start](https://beancount.io/docs/Basics/bea-cli), then the agent runs the same `bea` commands the recipes show. The agent proposes, you review, and `bea` validates.

A skill loads into the agent client itself, through that client's own skill mechanism. The product repository exposes one canonical tree to both supported clients: Claude Code reads it under `.claude/skills` and Codex under `.agents/skills`, as linked directories into the same source. Point the client's skill location at the skill folder you need, and keep reading its current source for trigger phrases and updates.

`bea ask` loads its own skills separately, from a `SKILL.md` beside your ledger — see [Extend bea ask with skills](https://beancount.io/docs/Solutions/bea-ask-skills).

## Ask-like questions without `bea ask`

Recipe 2 answers ledger questions with an external agent plus read-only `bea query` and `bea report` commands. That path is local. It needs no account and no extra install.

`bea ask` is a separate built-in helper. It is neither the recipe above nor the `beancount-ask` skill. It routes the question through the hosted AI service, so it needs the `ask` extra and hosted credentials, as documented in the [CLI reference](https://beancount.io/docs/bea-cli-reference). Install the base CLI first, then add the extra only when you want hosted answers.

## Quoted outputs and CLI verification

Every command above was executed against the published `bea 0.3.1` package in a fresh directory on 2026-11-01. Quoted counts, balances, and messages are CLI verification.

One client has completed the first challenge on a fresh copy of the downloads with no rules supplied in advance. Muse Code 1.1.1 (model `muse-spark-1.3-contributor`) derived its own categorization, previewed 3 ready with nothing written, applied 3 entries, and reported `2958.50 USD` checking with `1958.50 USD` September profit — matching the expectations above. Its one failed apply (a sandboxed cache lock, recovered via `XDG_CACHE_HOME`) is kept in the record. Read the full prompt, tool sequence, and verification in the <a href="/downloads/agent-accounting-run/agent-run.transcript.md" download>run transcript</a>, or replay the working path with the <a href="/downloads/agent-accounting-run/demo-replay.sh" download>demo script</a> and its <a href="/downloads/agent-accounting-run/demo-replay.txt" download>captured output</a>.

That run is CLI-output verification of a single execution, not a model benchmark. It claims nothing about other clients, accounting accuracy in general, or unattended production use.

The full proof story — inputs, methodology, observed results, failures, limitations, and downloads — is published as [Can your agent balance these books?](/bean-labs/research-logs/2026/09/10/can-your-agent-balance-these-books).

Explore the broader research behind these workflows at [Bean Labs](/bean-labs), Beancount.io's accounting automation research hub.
