Open History to see the last commit
1---
2title: Persistence Controls
3id: controls
4---
5
6Pass only the stores you use. Messages can live in your database while approvals live somewhere else.
7
8Persistence has no feature flags. The stores that the backend provides decide what is persisted. Use `composePersistence` to mix backends per store, and give only the stores your app needs.
9
10## Swap one store
11
12`composePersistence` takes the base backend first and an overrides object second. This example starts from the in-memory backend and replaces the `interrupts` and `runs` stores:
13
14```ts
15import { composePersistence, memoryPersistence } from '@tanstack/ai-persistence'
16// Your own implementations of the InterruptStore and RunStore contracts.
17import { interruptStore, runStore } from './stores'
18
19const persistence = composePersistence(memoryPersistence(), {
20 overrides: {
21 interrupts: interruptStore,
22 runs: runStore,
23 },
24})
25```
26
27## Remove one store
28
29Set the store to `false`:
30
31```ts
32import { composePersistence, memoryPersistence } from '@tanstack/ai-persistence'
33
34// Drop metadata. The resulting type has no `metadata` key.
35const withoutMetadata = composePersistence(memoryPersistence(), {
36 overrides: { metadata: false },
37})
38```
39
40Each override is independent:
41
42| Override value | Result |
43| --- | --- |
44| Key omitted | Keep the base store. |
45| `undefined` | Keep the base store. |
46| A store object | Replace that store only. |
47| `false` | Remove that store. |
48
49Unknown store names fail the type check. They also fail at runtime when the values come from untyped JavaScript.
50
51To define a partial backend directly, without a base, use `defineAIPersistence({ stores: { ... } })` and pass only the stores you have. The [store reference](./store-reference) has the store contracts.
52
53## What each store gives you
54
55| You want | Store |
56| --- | --- |
57| A server transcript that is the true copy | `messages` (**required** by `withPersistence` and `reconstructChat`) |
58| Run status and usage | `runs` (required on `ChatPersistence`, and required when `interrupts` is set) |
59| Durable approvals or human input | `interrupts` (needs `runs`) |
60| App or integration checkpoints | `metadata` (always optional) |
61
62`withPersistence(persistence)` looks at the stores that are present. A present store turns its feature on.
63
64## Valid store combinations
65
66- `withPersistence` needs `messages`.
67- `interrupts` needs `runs`, because an interrupt record belongs to a run.
68- `withGenerationPersistence` needs `generationRuns`.
69- Portable sandbox snapshots need `messages`, `artifacts`, and `blobs` on the same persistence object. See [Keep Files After Reload](../sandbox/portable-snapshots-configure).
70
71## Advanced
72
73### Named shapes
74
75Use these types for a persistence object. They are better than a sparse map:
76
77| Type | Required stores | Use |
78| --- | --- | --- |
79| `ChatTranscriptStores` / `ChatTranscriptPersistence` | `messages` (`runs`, `interrupts`, and `metadata` optional) | The minimum for `withPersistence` and `reconstructChat` |
80| `ChatPersistenceStores` / `ChatPersistence` | `messages`, `runs`, `interrupts`, and `metadata` | Packaged backends (`memoryPersistence`, Drizzle, Prisma, D1) |
81| `ChatWithInterruptsStores` / `ChatWithInterruptsPersistence` | `messages`, `runs`, and `interrupts` | Human-in-the-loop without `metadata` |
82
83There is no public sparse `AIPersistenceStores` export. For a custom map, use a named shape or `AIPersistence<{ messages: MessageStore, ... }>`. `defineAIPersistence` and `composePersistence` still accept sparse maps by inference.
84
85### Entrypoint requirements
86
87| Entrypoint | Shape | Notes |
88| --- | --- | --- |
89| `withPersistence` | At least `ChatTranscriptStores` | `interrupts` needs `runs` |
90| `reconstructChat` | `ChatTranscriptStores` | `runs` and `interrupts` add data to the response when present |
91| Packaged `*Persistence()` | `ChatPersistence` | `messages` and `runs`, plus `interrupts` and `metadata` |
92| `defineAIPersistence` / `composePersistence` | Sparse, by inference | Use a named shape for the result |
93
94### Locks
95
96Locks coordinate work across server instances, like a distributed mutex. They live in `@tanstack/ai/locks`, and you add them as their own middleware with `withLocks`, next to `withPersistence`. Full guide: [Locks](../advanced/locks).
97
98```ts
99import { withLocks, InMemoryLockStore } from '@tanstack/ai/locks'
100import { withPersistence, memoryPersistence } from '@tanstack/ai-persistence'
101
102const middleware = [
103 withPersistence(memoryPersistence()),
104 withLocks(new InMemoryLockStore()), // For many instances, use a distributed LockStore.
105]
106```