Skip to content

RetainDB/retaindb-nextjs-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RetainDB × Next.js Starter

A minimal Next.js 15 app with an AI chatbot that remembers users across sessions — powered by RetainDB and the Vercel AI SDK.

Deploy with Vercel


What this does

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:

  1. Retrieve — query the user's memories and inject them as context before each turn
  2. Store — save the user's messages in the background after each turn

Stack

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)

Getting started

1. Clone and install

git clone https://github.com/Alixus/retaindb-nextjs-starter.git
cd retaindb-nextjs-starter
npm install

2. Set environment variables

cp .env.example .env.local

Edit .env.local:

RETAINDB_API_KEY=your_retaindb_api_key   # get at retaindb.com
OPENAI_API_KEY=your_openai_api_key       # get at platform.openai.com

3. Run

npm run dev

Open http://localhost:3000. Tell the AI your name, a preference, anything. Close the tab. Come back. Ask it what it remembers.


How it works

app/api/chat/route.ts — the entire backend

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.

app/page.tsx — the frontend

Uses Vercel AI SDK's useChat hook, passing userId in the request body:

const { messages, input, handleInputChange, handleSubmit } = useChat({
  api: '/api/chat',
  body: { userId },
});

Customisation

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;

Deploy to Vercel

Click the button at the top, or:

npx vercel

Set RETAINDB_API_KEY and OPENAI_API_KEY in the Vercel dashboard under Project → Settings → Environment Variables.


Learn more

About

Next.js + Vercel AI SDK + RetainDB persistent memory starter. Deploy to Vercel in one click.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors