# Agent memory with the AI SDK

`@hatchmd/ai-sdk` gives an agent built on Vercel's AI SDK a Hatch vault as its memory. The agent searches the vault before it answers and files what's worth keeping as Markdown notes. Your team reads and edits those notes in Hatch, Obsidian or any editor. When the agent and the people share the same files (a folder, or a vault synced to the bucket), the agent sees their edits on its next call.

```ts
import { hatchTools } from '@hatchmd/ai-sdk'
import { NodeFileSystem } from '@hatchmd/vault/node'
import { stepCountIs, streamText } from 'ai'

const hatch = await hatchTools({ fs: new NodeFileSystem('./vault') })

const result = streamText({
  model: 'anthropic/claude-sonnet-5.5',
  instructions: hatch.instructions,
  tools: hatch.tools,
  stopWhen: stepCountIs(8),
  prompt: 'What did we decide about Acme’s renewal? Log today’s call too.',
})
```

## The tools

They're the same tools the MCP server gives Claude Code and Cursor (see [mcp.md](https://hatch-docs.pages.dev/using-hatch/connect-an-agent/)), so an agent you build files notes the way those do:

| Tool | What it does |
|---|---|
| `vault_guide` | How this vault is laid out: folders, tags, properties, templates, daily notes and the owner's rules |
| `search_notes`, `list_notes`, `query_notes`, `list_tags` | Find notes by text, folder, tag or property |
| `read_note` | Read a note by path or name. Paths still work after a note is renamed or moved in Hatch |
| `create_note`, `append_to_note`, `add_to_section`, `replace_in_note`, `add_to_daily_note`, `move_note` | Write. Edits are merged with whatever a person changed meanwhile |
| `components`, `slides`, `site_look`, ... | Charts, slides and site styling, for agents that make pages |

`hatch.instructions` tells the model how to use them. Pass it as `instructions` (with your own after it).

## Options

| Option | |
|---|---|
| `fs` | Where the vault is. Any `FileSystem`: a folder (`NodeFileSystem`), memory (`MemoryFileSystem`), an object store (below) |
| `name` | The vault's name in the instructions. Default "Vault" |
| `readOnly` | Only the tools that read |
| `only` | Keep only these tools, like `['search_notes', 'read_note']` |

`hatch.vault` is the vault itself, for your own reads, and `hatch.close()` lets the tools go.

## A vault in an object store

Serverless hosts have no lasting disk, so keep the vault in a bucket. Each note is one object.

- **Cloudflare R2:** `r2FileSystem(env.VAULT, { prefix: 'acme' })` from `@hatchmd/ai-sdk/r2`, with the Worker's bucket binding.
- **Anything else:** `objectStoreFileSystem(store)`, where `store` answers `list(prefix)`, `get(key)`, `put(key, bytes)` and `delete(keys)`. The Vercel template has one for Vercel Blob in about 30 lines.

The tools read the bucket again before each call, but only notes that changed, so keep one `hatchTools` per warm instance rather than one per request. Opening the vault reads every note once, which counts against a Worker's subrequest limit: fine for hundreds of notes, too many for tens of thousands. An empty folder lasts only while the instance that made it runs, since a bucket only knows keys.

## Notes are input too

Anything in the vault reaches the model, including instructions someone wrote into a note. The templates pass `only` so the agent can read and add, but not rewrite, move or delete notes. Widen that list only for agents that need it.

## Templates

In `examples/agent-memory`:

- `vercel`: a Vercel Function at `/api/chat`, with the vault in private Vercel Blob and the model through Vercel's AI Gateway.
- `cloudflare`: a Worker at `/chat`, with the vault in R2, each conversation's turns in D1, and Anthropic's API.

Both answer only callers that send `AGENT_TOKEN` as a Bearer token. To run Hatch itself (the app and the server) on your own machine with Coolify, see [deploy.md](https://hatch-docs.pages.dev/running-hatch/deploying-hatch/).
