# What is the database toolkit, and how does it work?

**A database is a private SQL database in the cloud, one per app. Create it and give it tables from Irene, then attach it to an agent you build. That agent can look things up and record things (leads, orders, bookings) for everyone who uses it. The engine is walled in so that one query can't reach another database, run away, or drop your tables from a public agent.**

---

## What makes it different

| what it does | the primitive behind it | what that buys you |
|---|---|---|
| **No destructive actions on a deployed agent** | Attached **bound**, the tool has only `query` (read) and `execute` (write rows, create tables). No delete, no migration | A visitor can't talk your agent into deleting the database, because that action doesn't exist for it |
| **Only what you attached** | Database ids are fixed at attach time; `which` can only pick among them | A prompt can't point the agent at another database |
| **Read-only by key** | Attach as `database:<id>:read` and the write key never leaves your computer | A lookup agent can't change a row |
| **A private copy per end user** | `multitenant` gives each signed-in user their own copy, seeded from your tables and rows on first use | Every customer gets the same starting schema, and their own data |
| **A walled engine** | Only one statement per call; reads run in read-only mode; linking another database file is blocked; extensions are off; a timer stops runaway queries; results stream up to a cap | One bad query can't reach other data or hang the server |
| **Try a change before keeping it** | `branch` opens a scratch copy, `apply` keeps it, `discard` throws it away | Schema changes have an undo |

---

## How it works

### Two ways in

| you are… | you get | actions |
|---|---|---|
| **the owner** | the full `database` tool | `create` · `list` · `status` · `query` · `execute` · `branch` · `apply` · `discard` · `delete` |
| **a deployed agent** | the bound tool | `query` · `execute` (or only `query` if read-only) |

### Building it

1. **Create** with a name and an access mode: `auth` (default, read with the key), `open` (anyone may read) or `multitenant` (a copy per end user).
2. **Make tables** with `execute` (`CREATE TABLE`, `INSERT`…). There's no fixed schema: the schema is whatever you create.
3. **Attach** it to your agent and describe the tables in the agent's instructions, so it knows what to query.

### Under the hood

- Each database is one SQLite file kept in cloud storage. It's loaded onto the database server while in use and saved back **every 3 seconds** after a write. Pass `durable` to save before the call returns.
- The database server runs as **one machine on purpose.** SQLite has one writer, and two machines would overwrite each other's changes.
- All calls to one database run one at a time.

**Why SQLite:** it's fast, simple and cheap to keep, up to the plan size cap. The cap is also the point where an app should move to a full database server.

### Branches

`branch` copies the live database into a scratch copy, `apply` swaps the copy in as the live database, and `discard` deletes it. **Today the database tool can open and apply a branch, but not write into one,** so use branches only as a snapshot you can return to. Changes made with `execute` go to the live database.

---

## How agents are told to use it

- Reads are `query`, writes are `execute`, and the action always says which.
- The owner describes the tables in the agent's instructions. A real example on this machine: a sales desk agent with a `leads` table (name, email, company, stage, deal value, last contact), plus a policies store as its only source of truth.

---

## Approvals

| action | in auto mode |
|---|---|
| `list` · `status` | runs |
| `query` · `execute` | sorted by the SQL itself: a `SELECT` reads; `UPDATE` or `DELETE` without `WHERE`, and `DROP`, `ALTER` and similar, are **always held** |
| `create` · `branch` | treated as sending |
| `discard` | treated as deleting |
| `apply` · `delete` | **always held:** applying a branch is a migration, deleting is a drop |

On a deployed agent, reads never wait for approval, and the dangerous actions don't exist.

→ [Approvals & auto mode](https://mycelen.com/manual/how-it-works/approvals-and-auto-mode)

---

## Where you see it

- **The Agent Builder** creates, fills and attaches databases.
- **On a deployed agent,** queries show as ordinary tool cards.
- Your databases are recorded in `~/.irene/host_studio/databases.json`.

---

## Limits

| plan | size per database |
|---|---|
| Free | 50 MB |
| Starter | 250 MB |
| Pro | 500 MB |

- **Power and Ultra currently fall back to the Free cap,** which is a gap in the plan table.
- **Over the cap,** writes are refused with an upgrade note.
- **Up to 1,000 rows** come back per query; more is marked truncated.
- **15 seconds** per statement.
- **One statement per call.**
- **Branches can't be written to** through the tool yet.

---

## What it costs

- **Queries and writes** use no plan usage. The limit is size.
- **Holding more databases than your plan includes** costs $1 a month each.
- **The agent's own model usage** is billed as normal, at what we pay + 20%.

---

## Related

- [store](https://mycelen.com/manual/reference/toolkits/store) · [agent_memory](https://mycelen.com/manual/reference/toolkits/agent-memory)
- [Agent Builder](https://mycelen.com/manual/reference/builders/agent-builder) · [Deploy to the cloud](https://mycelen.com/manual/reference/builders/cloud-deploy)

[Ask Irene about this]
