# Cloudflare D1 and AI agents, with guardrails.

> D1 has no database roles, so Osevel enforces every rule itself: AI agents reach D1 only through templates you install, and every change can be undone.

Source: https://osevel.com/integrations/cloudflare-d1/

D1 has no database roles, so OsBase keeps the API token on your machine and enforces every rule itself. Agents reach D1 only through templates you install: named, typed statements, and no free SQL.

## Good to know

- **Templates, not SQL.** An agent’s MCP server offers one tool per template you granted it, with typed arguments, and no SQL tool at all.
- **D1 checks the templates.** Every template is compiled by D1 before it is stored; a template marked “read” whose bytecode writes is refused.
- **Every row, before and after.** Undo restores rows one by one, and refuses when your application changed a row since. An undo can be undone too.
- **Tried with Claude Code.** On a real D1 database it hid two spam comments through a template, and did not act on a comment telling it to delete everything.

## Set it up

```sh
# 1. add the database (the token goes to your OS keychain)
osevel d1 add blog --account <account-id> --database <database-id>
# 2. install the change log
osevel d1 install blog
# 3. give an agent its own key
osevel d1 agent add blog moderator --ide claude-code
```

Pre-release (0.2). Install: `npm i -g @osevel/cli` (the command is `osevel`).

More: [The Cloudflare D1 guide](https://osevel.com/docs/guides/cloudflare-d1/)
