Open History to see the last commit
1---
2title: Adapters
3id: memory-adapters
4order: 3
5description: "Every built-in and vendor memory adapter in @tanstack/ai-memory, with all of their options and an example of each: inMemory, redis, hindsight, mem0, honcho."
6keywords:
7 - tanstack ai
8 - memory
9 - adapters
10 - inMemory
11 - redis
12 - hindsight
13 - mem0
14 - honcho
15 - options
16---
17
18Every adapter implements the same `recall`/`save` contract, so they're interchangeable
19in `memoryMiddleware`. This page is the full option reference: each adapter's options with
20an example of each.
21
22- [Common options](#common-options), shared by `inMemory()` and `redis()`
23- Adapters: [`inMemory()`](#inmemory), [`redis()`](#redis), [`hindsight()`](#hindsight), [`mem0()`](#mem0), [`honcho()`](#honcho)
24
25## Common options
26
27`inMemory()` and `redis()` are both client-side rankers built on the same pipeline, so
28they share these options.
29
30| Option | Type | Default | Purpose |
31|--------|------|---------|---------|
32| `topK` | `number` | `6` | Max hits returned by `recall`. |
33| `minScore` | `number` | `0.15` | Drop hits scoring below this. |
34| `kinds` | `Array<MemoryKind>` | all | Restrict recall to these record kinds (`'message'`, `'summary'`, `'fact'`, `'preference'`). |
35| `embedder` | `{ embed(text): Promise<number[]> }` | none | Enable semantic scoring (embeds on both `recall` and `save`). |
36| `extract` | `(turn, scope) => ExtractedFact[]` | none | Persist derived facts on `save`, alongside the raw turn. |
37| `render` | `(hits) => string` | built-in | Replace the prompt renderer. |
38
39Every option, in one adapter:
40
41```ts
42import { inMemory } from '@tanstack/ai-memory/in-memory'
43
44// `embedText` stands in for your embedding client (OpenAI, Cohere, a local model).
45declare function embedText(text: string): Promise<Array<number>>
46
47const memory = inMemory({
48 topK: 8, // return up to 8 hits
49 minScore: 0.2, // ignore weak matches
50 kinds: ['message', 'fact', 'preference'], // skip summaries
51 embedder: { embed: embedText }, // semantic + lexical scoring
52 extract: (turn) => [
53 // store a derived fact in addition to the raw turn
54 { text: `User said: ${turn.user}`, kind: 'fact', importance: 0.8 },
55 ],
56 render: (hits) =>
57 // custom prompt block instead of the default renderer
58 `What I remember:\n${hits.map((h) => `- ${h.record.text}`).join('\n')}`,
59})
60```
61
62**`extract`** returns `ExtractedFact[]` (`{ text, kind?, importance?, metadata? }`). Return
63`undefined` for a no-op. It's where an LLM-based fact extractor plugs in without the
64adapter taking a hard dependency on any model.
65
66**`embedder`** is invoked on the recall path (to embed the query) and again on save (to
67embed stored text). Without it, scoring is lexical + recency only.
68
69## `inMemory()`
70
71Zero-dependency, `Map`-backed. Takes only the [common options](#common-options) above.
72Records vanish on restart, so use it for dev, tests, and single-process demos.
73
74```ts
75import { inMemory } from '@tanstack/ai-memory/in-memory'
76
77const memory = inMemory() // all options are optional
78```
79
80## `redis()`
81
82Plain-Redis adapter. Adds two options to the [common options](#common-options), and
83requires a client.
84
85| Option | Type | Default | Purpose |
86|--------|------|---------|---------|
87| `redis` | `RedisLike` | (required) | Your Redis client (`ioredis`, or node-redis via `fromNodeRedis`). |
88| `prefix` | `string` | `'tanstack-ai:memory'` | Key namespace. |
89
90```ts
91import Redis from 'ioredis'
92import { redis } from '@tanstack/ai-memory/redis'
93
94const memory = redis({
95 redis: new Redis(process.env.REDIS_URL ?? 'redis://localhost:6379'), // required
96 prefix: 'myapp:memory', // key namespace
97 topK: 8, // common options apply here too
98 minScore: 0.2,
99})
100```
101
102Using **node-redis** (`redis` package) instead of `ioredis`? Its camelCase API doesn't
103match `RedisLike`, so wrap it with `fromNodeRedis`:
104
105```ts
106import { createClient } from 'redis'
107import { redis, fromNodeRedis } from '@tanstack/ai-memory/redis'
108
109const client = createClient({ url: process.env.REDIS_URL })
110await client.connect()
111
112const memory = redis({ redis: fromNodeRedis(client) })
113```
114
115`ioredis` and `redis` are both optional peer dependencies. Install whichever you use.
116
117**Scope fields:** index keys are `{prefix}:index:{tenantId|_}:{userId|_}:{threadId}`
118(each segment escaped so `:`, `\`, and `_` in values cannot collide). Missing optional
119dims become `_`, so a write with `tenantId` and a read without it hit **different** keys
120— always pass the same dims you wrote with. There is no dual-read of older layouts; if
121you previously wrote under a different index shape, reindex or wipe.
122
123## Which `Scope` fields each adapter honors
124
125| Adapter | `threadId` | `userId` | `tenantId` | `namespace` |
126|---------|------------|----------|------------|-------------|
127| `inMemory()` | yes (exact) | yes (exact) | yes (exact) | ignored |
128| `redis()` | yes (key segment) | yes (key segment) | yes (key segment) | ignored |
129| `hindsight()` | yes (bank id) | yes (bank id / `user` option) | yes (bank prefix; unset → `_`) | ignored |
130| `mem0()` | yes (`run_id`) | yes (`user_id` / `user` option) | **no** | ignored |
131| `honcho()` | yes (session key) | yes (peer id / `user` option) | yes (session/peer prefix) | ignored |
132
133Optional dims are exact-match (omit ≠ match any). mem0 does not model tenants —
134encode multi-tenant isolation into `user` if needed.
135
136## `hindsight()`
137
138Hosted adapter backed by Hindsight. Owns extraction/ranking server-side and exposes
139`retain`/`recall`/`reflect` LLM tools through `recall`. `@vectorize-io/hindsight-client`
140is an optional peer, loaded lazily.
141
142| Option | Type | Default | Purpose |
143|--------|------|---------|---------|
144| `user` | `string` | `scope.userId` | Durable user id used in the bank key (`{tenant\|_}__{user}__{threadId}`). |
145| `baseUrl` | `string` | `HINDSIGHT_URL` / `http://localhost:8888` | Server URL. |
146| `budget` | `'low' \| 'mid' \| 'high'` | `'mid'` | Recall budget. |
147| `onToolRetain` | `(receipt) => void` | none | Fired when the model calls `hindsight_retain`. |
148| `onToolRecall` | `(query, result) => void` | none | Fired when the model calls `hindsight_recall`. |
149
150```ts
151import { hindsight } from '@tanstack/ai-memory/hindsight'
152
153const memory = hindsight({
154 user: 'alice', // bank = {_}__alice__{threadId} (or tenant__alice__{threadId})
155 baseUrl: 'https://hindsight.internal', // default: HINDSIGHT_URL
156 budget: 'high', // deeper recall
157 onToolRetain: (receipt) => console.log('model retained', receipt.ok),
158 onToolRecall: (query, result) =>
159 console.log('model recalled', query, result.fragments?.length),
160})
161```
162
163Bank id is `{tenantId|_}__{user}__{threadId}`.
164
165## `mem0()`
166
167Hosted adapter backed by a mem0 server, over plain HTTP (no SDK peer). Requires a running
168mem0 server.
169
170| Option | Type | Default | Purpose |
171|--------|------|---------|---------|
172| `user` | `string` | `scope.userId` / `'demo-user'` | mem0 `user_id`. |
173| `baseUrl` | `string` | `MEM0_URL` / `http://localhost:8000` | Server URL. |
174| `apiKey` | `string` | `MEM0_ADMIN_API_KEY` | Bearer token. |
175| `rerank` | `boolean` | `true` | Ask mem0 to rerank search results. |
176| `threshold` | `number` | `0.1` | Minimum search score. |
177
178```ts
179import { mem0 } from '@tanstack/ai-memory/mem0'
180
181const memory = mem0({
182 user: 'alice', // mem0 user_id
183 baseUrl: 'https://mem0.internal', // default: MEM0_URL
184 apiKey: process.env.MEM0_ADMIN_API_KEY, // bearer token
185 rerank: true, // rerank results
186 threshold: 0.2, // stricter score floor
187})
188```
189
190mem0 requests send `user_id` and `run_id` (`threadId`). `tenantId` is not sent.
191
192## `honcho()`
193
194Hosted adapter backed by Honcho. `recall` returns a synthesized dialectic answer over the
195user's representation (no discrete fragments). `@honcho-ai/sdk` is an optional peer,
196loaded lazily.
197
198| Option | Type | Default | Purpose |
199|--------|------|---------|---------|
200| `user` | `string` | `scope.userId` / `'demo-user'` | User peer id. |
201| `baseURL` | `string` | `HONCHO_URL` / `http://localhost:8001` | Server URL. |
202| `workspaceId` | `string` | `HONCHO_APP_NAME` / `'ai-memory'` | Workspace id. |
203| `apiKey` | `string` | `HONCHO_API_KEY` / `'dev-no-auth'` | API key. |
204| `assistantId` | `string` | `'assistant'` | Assistant peer id. |
205
206```ts
207import { honcho } from '@tanstack/ai-memory/honcho'
208
209const memory = honcho({
210 user: 'alice', // user peer
211 baseURL: 'https://honcho.internal', // default: HONCHO_URL
212 workspaceId: 'my-app', // default: HONCHO_APP_NAME
213 apiKey: process.env.HONCHO_API_KEY, // default: 'dev-no-auth'
214 assistantId: 'support-bot', // default: 'assistant'
215})
216```
217
218Honcho session key is `{tenantId|_}__{threadId}`; peers are `{tenantId}__{user}` when
219`tenantId` is set, otherwise the bare user id.
220
221## Where to go next
222
223- [Overview](./overview): the `recall`/`save` contract and how a turn flows
224- [Quickstart](./quickstart): wire an adapter into a real `chat()` call
225- [Operating memory](./operating): options, telemetry, devtools events, and failures
226- [Custom Adapter](./custom-adapter): implement `recall`/`save` for a backend that isn't shipped