# Store conversation history in a database

By default, the Bub app persists conversation Tapes as local JSONL files under `~/.bub/tapes/`.

By installing [`bub-tapestore-sqlalchemy`](https://github.com/bubbuild/bub-contrib/tree/main/packages/bub-tapestore-sqlalchemy), you can switch seamlessly to SQLite, PostgreSQL, or any SQLAlchemy-supported relational database while keeping `,tape.info`, `,tape.search`, forks, and sidecars unchanged.

:::caution[Existing files are not migrated]
Switching storage backends **does not** import existing local JSONL files automatically (existing files remain in `~/.bub/tapes/`).
:::

## 1. Install the plugin {#install}

Install the store plugin in the environment where Bub runs:

```bash
bub install bub-tapestore-sqlalchemy@main
bub hooks
```

Verify `tapestore-sqlalchemy` appears before `builtin` under `provide_tape_store`:

```text
provide_tape_store: tapestore-sqlalchemy, builtin
```

Higher-priority implementations appear first, so seeing `tapestore-sqlalchemy` confirms the plugin is active and overrides the built-in file store.

## 2. Configure the database URL {#configure}

The plugin defaults to `<BUB_HOME>/tapes.db`. Set `BUB_TAPESTORE_SQLALCHEMY_URL` to any SQLAlchemy connection URL (using a local SQLite file here as an example):

```bash
export BUB_TAPESTORE_SQLALCHEMY_URL="sqlite+pysqlite:///$PWD/bub-tapes.db"
```

Optional settings:

| Environment variable | Default | Description |
| --- | --- | --- |
| `BUB_TAPESTORE_SQLALCHEMY_ECHO` | `false` | Set `true` to log executed SQL statements |
| `BUB_TAPESTORE_SQLALCHEMY_CONNECT_ARGS` | `{}` | JSON object passed to `create_engine(connect_args=...)` (e.g. timeouts) |

To persist these settings across sessions, add them to your workspace `.env` file. Tables are created automatically on first connection.

## 3. Verify writes and reads {#verify}

Run a turn and inspect the Tape:

```bash
bub run "Say hello in one sentence to verify database storage."
bub run ",tape.info"
```

Example output:

```text
name: 86774b31b96845a4__0b871d5e50e7c192
entries: 9
anchors: 1
last_anchor: session/start
entries_since_last_anchor: 8
```

Inspect the SQLite tables directly with `sqlite3`:

```bash
sqlite3 "$PWD/bub-tapes.db" "SELECT name, last_entry_id FROM tapes ORDER BY name;"
sqlite3 "$PWD/bub-tapes.db" "SELECT entry_id, anchor_name, entry_date FROM tape_entries WHERE kind = 'anchor';"
```

The `tapes` table tracks session metadata, while `tape_entries` records each Turn, model reply, and tool call entry by entry.

## 4. Connect PostgreSQL or MySQL {#other-databases}

1. **Install the database driver in Bub's environment** (PostgreSQL example):
   ```bash
   uv add "psycopg[binary]"
   ```
2. **Set the connection URL**:
   ```bash
   export BUB_TAPESTORE_SQLALCHEMY_URL="postgresql+psycopg://user:YOUR_DB_PASSWORD@localhost:5432/bub_db"
   ```

In production, test concurrent writes and connection-pool timeouts ahead of time to keep the service stable.

## Implement a custom store backend {#custom-store}

`core.Agent` and the built-in app depend only on the store protocol, so you can back Tapes with Redis or another service by implementing it (for the difference between Tape history and context projection, see [Tape and model context](/docs/concepts/tape/#history-and-context)):

| Protocol / Hook | Where it is used |
| --- | --- |
| `bub.core.store.TapeStore` | Synchronous store passed to `Agent(store=...)` |
| `bub.core.store.AsyncTapeStore` | Asynchronous store (recommended for databases and network I/O) |
| `provide_tape_store` | App-layer plugin hook replacing the default `FuzzyFileTapeStore` |

See [Python API: `TapeStore`](/docs/reference/python-api/#tape-store) for method signatures and contracts. Forks are built on top of any store by `ForkTapeStore`, so custom backends do not need to implement branching themselves.

## Store resolution in the built-in app {#app-storage}

The built-in app provides `FuzzyFileTapeStore` (`~/.bub/tapes/`) via `provide_tape_store` and hashes the workspace path with the session ID so same-named sessions in different workspaces remain separate.

`BubAgent` selects its store in this order:

1. An explicit `tape_store` passed to `BubAgent(...)`;
2. `framework.get_tape_store()` at instantiation time (must be inside `async with framework.running():`);
3. `InMemoryTapeStore`.

The built-in app restores session-level settings—such as model, reasoning effort, and code mode—from the Tape automatically; persist any other custom state explicitly via `load_state` and `save_state`.

## Troubleshooting {#troubleshooting}

| Symptom | Possible cause | Fix |
| --- | --- | --- |
| `tapestore-sqlalchemy` missing from `bub hooks` | Installed in a different Python environment | Check `command -v bub` and run `bub install` in that environment |
| SQLite `Database is locked` | High multi-process write concurrency | Increase timeout in `BUB_TAPESTORE_SQLALCHEMY_CONNECT_ARGS` or switch to PostgreSQL |
| `ModuleNotFoundError` | Missing database driver | Install the driver in Bub's environment (e.g. `uv add "psycopg[binary]"`) |

## Cleanup {#cleanup}

To switch back to Bub's default local file store, clear the environment variables and remove the test database:

```bash
unset BUB_TAPESTORE_SQLALCHEMY_URL BUB_TAPESTORE_SQLALCHEMY_ECHO BUB_TAPESTORE_SQLALCHEMY_CONNECT_ARGS
rm -f "$PWD/bub-tapes.db"
```