@redis/vercel-ai-sdk
v0.4.0
Published
AI SDK tools for Redis Agent Memory: session history, persistent memories, and semantic search
Readme
Redis Agent Memory for Vercel's AI SDK
Redis Agent Memory, a part of the Iris suite, provides a managed memory layer that gives agents intelligent short-term memory and persistent context across conversations. It allows AI agents to combine session history with long-term memories so they can recall user preferences, retain useful facts, and pick up where they left off.
This integration supports two complementary ways to add Redis Agent Memory to agents built with Vercel's AI SDK: an automatic-memory model wrapper that restores context and records sessions, and ordinary memory tools.
Both are created by the same server-side createRedisAgentMemory(...) factory.
Choose an approach
| | Automatic-memory model | Tools |
| --- | --- | --- |
| Entry point | @redis/vercel-ai-sdk | @redis/vercel-ai-sdk |
| Setup | memory.model({ scope?, model?, ...options }) | memory.tools({ scope? }) |
| Agent integration | Pass a fresh returned model as the request's model | Pass the returned tool map as tools alongside your model |
| Recall and history | Restores the scoped session and injects recalled personal memories before inference by default | The model chooses memory tools; the application manages input history |
| Session recording | Automatic USER recording and successful text-bearing assistant steps by default in Iris-managed mode | Model-selected session tools, or optional application lifecycle hooks |
| Choose when | You want automatic context and continuity without model-selected memory tools | You want explicit operations, selective tools, or model-directed memory management |
| Quick start | Automatic context and recording | Agent-managed recording or recording hooks |
Hooks are an optional recording mode of the tools approach, not a prerequisite for either integration. The approaches may coexist, but do not enable automatic model recording and session-write tools or recording hooks for the same interaction: that can duplicate events.
Installation
Requires Node.js 22.12+, AI SDK 7.0.114+ (7.x), and Zod 4.1.8+ (4.x).
This is a server-side ESM package with TypeScript declarations. CommonJS projects can load it with require().
npm install @redis/vercel-ai-sdkProvision a Redis Agent Memory store in Redis Cloud, then set AGENT_MEMORY_API_KEY, AGENT_MEMORY_STORE_ID, and AGENT_MEMORY_URL in your server environment.
There is no shared default endpoint: each store has its own URL, so use the one shown for your store.
Quick start
These examples use Anthropic directly; install its provider with npm install @ai-sdk/anthropic@^4 and set ANTHROPIC_API_KEY.
Other AI SDK 7-compatible configured models work with the same APIs. Model-name strings are not accepted.
Automatic-memory model
import { anthropic } from '@ai-sdk/anthropic';
import { ToolLoopAgent, isStepCount } from 'ai';
import { createRedisAgentMemory } from '@redis/vercel-ai-sdk';
const memory = createRedisAgentMemory({
serverUrl: process.env.AGENT_MEMORY_URL,
storeId: process.env.AGENT_MEMORY_STORE_ID,
apiKey: process.env.AGENT_MEMORY_API_KEY,
model: anthropic('claude-sonnet-4-5'),
});
async function reply(userId: string, sessionId: string, prompt: string) {
const agent = new ToolLoopAgent({
model: memory.model({
scope: { userId, sessionId },
assistantId: 'assistant',
}),
stopWhen: isStepCount(8),
});
return agent.generate({ prompt });
}Configure the Redis Agent Memory connection and, optionally, a default model once.
Create a fresh request-scoped model with memory.model(...) inside every application request.
The wrapper restores the Iris session, recalls personal long-term memories, injects a bounded untrusted context block, and records the current user input plus every successful text-bearing raw assistant step.
It exposes no memory tools to the model.
When context is nonempty, the wrapper prepends IRIS MEMORY CONTEXT MAY BE STALE.\n; that fixed 34-UTF-16-unit framing sits outside the separate session and long-term-memory character budgets.
A default model is optional because memory.tools() is independent.
If the factory has no model, provide one to each memory.model({ model, ... }) call.
Missing, string-valued, or otherwise invalid models fail synchronously before network calls.
The factory shares its Redis Agent Memory client and configured default model.
Each memory.model() result owns its scope, recall/restoration snapshot, USER-write attempt, recording budget, sequencing, and concurrency/cancellation state.
Reuse that result for steps and retries within one application request, never across independent requests.
Factory, tool, and model construction make no network calls.
Overlapping calls on one request model are rejected; independent request models may run concurrently.
Do not pass model: memory to a singleton agent; automatic agent-level lifecycle integration is not supported.
The default historyMode: 'iris' restores the session and defaults recording to true.
The current USER event is written once before inference; every successful raw model step with generated text is written afterward, including intermediate tool-loop steps.
Only text is restored and recorded; image-only input, reasoning, tool arguments/results, binary data, and system events are omitted.
Redis Agent Memory may extract long-term memories asynchronously from recorded events.
Consent, PII handling, retention, and extraction policy remain application and Redis Agent Memory configuration concerns.
Application-managed mode does not restore session events and defaults recording off, but still recalls long-term memory:
const model = memory.model({
scope: { userId, sessionId },
historyMode: 'application',
recording: false,
});Set includeMemoryProvenance: true to add a sanitized providerMetadata.redisAgentMemory.memories list for memories actually included in the prompt.
Entries contain only id, memoryType, createdAt, and updatedAt.
Provenance is off by default.
Existing underlying metadata is preserved; if it already owns redisAgentMemory, wrapper provenance is omitted.
The wrapper is best effort, not exactly once. The wrapper sends no append idempotency key, so a timed-out POST may have committed. Use collision-resistant session IDs bound to authenticated state and serialize requests by exact session ID. Never combine automatic recording with session hooks or model-callable session-write tools for the same interaction.
For streaming, nonterminal parts pass through as produced.
Recording follows raw provider finish plus normal source close, and the terminal finish is held only for the bounded save.
Cancellation or transport failure before raw completion prevents the assistant write where possible; an accepted POST cannot be rolled back.
Memory failures do not replace successful model output.
Read failures, timeouts, failed or oversized session writes, and assistant scope violations are added to the AI SDK warnings as content-free { type: 'other' } entries. The AI SDK logs warnings to the console unless globalThis.AI_SDK_LOG_WARNINGS is false or a custom logger.
A streamed assistant write finishes after stream-start has been sent, so its failure reaches only onDiagnostic.
The optional onDiagnostic callback receives every outcome, including successes, as a structured, content-free event.
Scope violations remain hard failures.
Runnable demos
demo/provider/demonstrates the automatic-memory model approach.demo/tools/demonstrates the explicit tools and recording-hook approach.
Each demo has independent installation, test, and startup instructions and is excluded from npm releases.
Configuration
const memory = createRedisAgentMemory({
serverUrl,
storeId,
apiKey,
scope,
model,
allowDeletes,
namespaceTools,
routingPolicyWrites,
ingestionTools,
summaryViewTools,
timeoutMs,
});| Factory option | Default / behavior |
| --- | --- |
| serverUrl | AGENT_MEMORY_URL; required here or through the environment, since each store has its own endpoint URL |
| storeId | AGENT_MEMORY_STORE_ID; required here or through the environment |
| apiKey | AGENT_MEMORY_API_KEY; required here or through the environment |
| scope | Optional default { userId?, sessionId?, namespaceId? } |
| model | Optional configured AI SDK model default; never a model-name string |
| allowDeletes | false; exposes deletion tools when true |
| namespaceTools | false; exposes namespace administration and store-wide routing tools when true |
| routingPolicyWrites | false; with namespaceTools, also exposes put_namespace_routing, which replaces the store-wide routing policy |
| ingestionTools | false; exposes document ingestion and ingestion hook status tools when true |
| summaryViewTools | false; exposes Summary View tools when true |
| timeoutMs | 30000 per HTTP request; must be positive and finite |
Scope accepts userId, sessionId, and namespaceId. Any other field, including the removed namespace label, throws instead of being ignored, so an unmigrated scope cannot silently lose its boundary.
Configuration is validated and copied at construction; later mutations of caller-owned objects do not alter factory defaults.
memory.model(...) accepts scope?, model?, assistantId?, historyMode?, recording?, query?, strictReads?, includeMemoryProvenance?, limits?, and onDiagnostic?.
memory.tools({ scope? }) accepts only a scope override.
Omitting an override or passing undefined uses its factory default.
A defined scope override replaces the full default scope; a defined model override replaces the model default.
Inputs, the factory, and previously returned toolsets/models are never mutated.
Automatic-memory models require a resolved scope with userId; Iris-managed history or recording additionally requires sessionId, and recording requires a valid assistantId.
Tools preserve their prior unscoped behavior when no scope is configured. All existing validation and ownership/namespace checks run after default resolution.
Model limits default to 5 recalled memories, 8,000 characters of long-term memory context, 24,000 characters of restored session context, one-second shared read and per-write deadlines, and a 100,000-UTF-16-unit cumulative recording budget.
Override them through limits; diagnostics remain opt-in through onDiagnostic and never include prompt, memory, or credential text.
The factory scope is trusted configuration, not an authorization ceiling.
Resolve scope and overrides from authenticated application state.
During tool execution, the AI SDK abortSignal is forwarded to Iris.
Session hooks use the configured HTTP timeout.
SDK defaults disable automatic retries, avoiding duplicate appends after uncertain outcomes.
Health check
Model and tool calls fail open, so a wrong URL, store ID, or API key otherwise goes unnoticed. Call await memory.verify() at startup or from a readiness probe.
It requests the store's health with the configured connection and resolves with { status, features }, where each status is HEALTHY, DEGRADED, or UNAVAILABLE.
It rejects with a RedisAgentMemoryToolError when the server is unreachable or refuses the store ID or key, for example IRIS_HTTP_401, and does not retry.
Pass { timeoutMs } to bound the request more tightly than the factory's timeoutMs, for example await memory.verify({ timeoutMs: 2_000 }).
Whether DEGRADED should fail readiness is the application's decision.
Development
git clone https://github.com/redis/vercel-ai-sdk.git
cd vercel-ai-sdk
npm ci
npm run check
npm packInstall the resulting tarball in your application with npm install /path/to/redis-vercel-ai-sdk-0.1.0.tgz. It contains the compiled library, declarations, source maps, source files, and package documentation. No demo application or credentials are included.
See CONTRIBUTING.md for the repository layout, commands, and release process. The tests use mocked HTTP and model boundaries and do not require API credentials.
License
MIT.
