A minimal Next.js 15 app with an AI chatbot that remembers users across sessions — powered by RetainDB and the Vercel AI SDK.
Every message the user sends is stored as a memory. On the next turn — or the next session — the most relevant memories are retrieved and injected as context before the AI responds. The AI never forgets.
The entire integration is one line:
// app/api/chat/route.ts
const model = withRetainDB(openai('gpt-4o-mini'), { userId });
const result = streamText({ model, system: '...', messages });withRetainDB wraps the model to:
- Retrieve — query the user's memories and inject them as context before each turn
- Store — save the user's messages in the background after each turn
| Framework | Next.js 15 (App Router) |
| AI SDK | Vercel AI SDK (streamText, useChat) |
| Memory | RetainDB (@retaindb/sdk) |
| LLM | OpenAI gpt-4o-mini (swap for any AI SDK provider) |
git clone https://github.com/Alixus/retaindb-nextjs-starter.git
cd retaindb-nextjs-starter
npm installcp .env.example .env.localEdit .env.local:
RETAINDB_API_KEY=your_retaindb_api_key # get at retaindb.com
OPENAI_API_KEY=your_openai_api_key # get at platform.openai.comnpm run devOpen http://localhost:3000. Tell the AI your name, a preference, anything. Close the tab. Come back. Ask it what it remembers.
import { openai } from '@ai-sdk/openai';
import { streamText } from 'ai';
import { withRetainDB } from '@retaindb/sdk/ai-sdk';
export const runtime = 'nodejs';
export async function POST(req: Request) {
const { messages, userId } = await req.json();
const model = withRetainDB(openai('gpt-4o-mini'), { userId });
const result = streamText({
model,
system: 'You are a helpful assistant with persistent memory across conversations.',
messages,
});
return result.toDataStreamResponse();
}That's it. No manual getContext. No manual remember. The adapter handles both.
Uses Vercel AI SDK's useChat hook, passing userId in the request body:
const { messages, input, handleInputChange, handleSubmit } = useChat({
api: '/api/chat',
body: { userId },
});Swap the model — replace openai('gpt-4o-mini') with any Vercel AI SDK provider:
import { anthropic } from '@ai-sdk/anthropic';
const model = withRetainDB(anthropic('claude-sonnet-4-6'), { userId });Scope memories to a session — pass sessionId alongside userId:
const model = withRetainDB(openai('gpt-4o-mini'), { userId, sessionId });Opt out of auto-storage — set remember: false to retrieve-only:
const model = withRetainDB(openai('gpt-4o-mini'), { userId, remember: false });Use real auth — replace the localStorage userId in app/page.tsx with your actual session:
// e.g. with NextAuth
import { getServerSession } from 'next-auth';
const session = await getServerSession();
const userId = session.user.id;Click the button at the top, or:
npx vercelSet RETAINDB_API_KEY and OPENAI_API_KEY in the Vercel dashboard under Project → Settings → Environment Variables.
- RetainDB docs
- RetainDB SDK reference
- Vercel AI SDK docs
- LongMemEval benchmark results — 88% SOTA preference recall