Vercel AI SDK: Persistent Memory Across Sessions

Free · Open source (MIT) · Works with LangChain, CrewAI, AutoGen · No signup

Every Next.js developer using the Vercel AI SDK hits this wall: you build a chat interface with conversation memory, deploy to Vercel, and suddenly all chat history vanishes. Each user session starts blank because edge functions are stateless. Your AI loses context between messages, creating a frustrating user experience that breaks the conversational flow.

The Problem: Stateless Edge Functions Kill Chat Memory

The Vercel AI SDK excels at streaming responses and handling AI interactions, but it doesn't solve persistence. Edge functions spin up and down constantly — after deployments, during scaling, or simply due to cold starts. Any in-memory chat history disappears.

Here's what breaks:

// This loses all context on every deployment
let chatHistory = []; // Gone after restart

export async function POST(request) {
  chatHistory.push(userMessage);
  const response = await openai.chat.completions.create({
    messages: chatHistory, // Empty array after deployment
  });
}

Users lose conversation context mid-chat. Your AI assistant forgets what it was helping with. Multi-turn conversations become impossible. The chat feels broken and unprofessional.

This happens because Vercel's edge runtime is ephemeral by design — optimized for speed, not state retention.

The Fix: Persistent Memory with BotWire

BotWire Memory gives your Vercel AI SDK apps persistent state that survives deployments and restarts. Install it and your chat history persists across all sessions:

pip install botwire

Here's the working solution for your Next.js API route:

// app/api/chat/route.js
import { Memory } from 'botwire';

export async function POST(request) {
  const { message, userId } = await request.json();
  const memory = new Memory(`chat-${userId}`);
  
  // Retrieve persistent chat history
  let history = await memory.get('messages') || [];
  
  // Add new message
  history.push({ role: 'user', content: message });
  
  const response = await openai.chat.completions.create({
    messages: history,
    model: 'gpt-3.5-turbo',
  });
  
  // Save updated history
  history.push(response.choices[0].message);
  await memory.set('messages', history);
  
  return Response.json(response);
}

How It Works

BotWire Memory operates as a persistent key-value store that your edge functions can access from anywhere. Each Memory("namespace") creates an isolated storage space — perfect for per-user chat histories.

The memory persists across deployments, server restarts, and geographic regions. When your edge function spins up in a different location, it pulls the same conversation history.

Managing Chat History Length

Real conversations grow long. Manage token limits by keeping recent messages:

// Keep last 20 messages to stay within token limits
if (history.length > 20) {
  history = history.slice(-20);
  await memory.set('messages', history);
}

Cross-Session Access Patterns

Share memory across different parts of your application:

// User preferences
const userMemory = new Memory(`user-${userId}`);
await userMemory.set('theme', 'dark');
await userMemory.set('language', 'en');

// Chat metadata
const chatMemory = new Memory(`chat-${chatId}`);
await chatMemory.set('created_at', new Date().toISOString());
await chatMemory.set('participants', [userId1, userId2]);

// List all stored keys
const keys = await chatMemory.list();
console.log('Stored keys:', keys);

Memory operations are async and return promises. Each namespace gets 1000 writes per day free, with unlimited reads — perfect for chat applications where you read history frequently but only write new messages.

Integration with Vercel Edge Functions

BotWire works seamlessly in Vercel's edge runtime. Here's a complete chat API with conversation memory:

// app/api/chat/route.js
import { Configuration, OpenAIApi } from 'openai';
import { Memory } from 'botwire';

const openai = new OpenAIApi(new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
}));

export async function POST(request) {
  try {
    const { message, sessionId } = await request.json();
    const memory = new Memory(`vercel-chat-${sessionId}`);
    
    // Get conversation history
    const messages = await memory.get('conversation') || [
      { role: 'system', content: 'You are a helpful assistant.' }
    ];
    
    // Add user message
    messages.push({ role: 'user', content: message });
    
    // Get AI response
    const completion = await openai.createChatCompletion({
      model: 'gpt-3.5-turbo',
      messages,
      stream: true,
    });
    
    // Save conversation (you'd collect the full response in a real implementation)
    messages.push({ role: 'assistant', content: completion.data.choices[0].message.content });
    await memory.set('conversation', messages);
    
    return new Response(completion.data);
  } catch (error) {
    return Response.json({ error: error.message }, { status: 500 });
  }
}

This pattern works identically across Vercel's regions — your chat state follows your users.

When NOT to Use BotWire

BotWire isn't the right choice for:

Vector similarity search — It's key-value storage, not a vector database. Use Pinecone or Weaviate for semantic search. • High-throughput applications — 1000 writes/day works for chat apps, not high-frequency trading bots. • Sub-millisecond latency requirements — Network calls add ~50-200ms. Use Redis for microsecond access patterns.

FAQ

Why not just use Redis or Firebase? Redis requires server management and costs money immediately. Firebase has complex pricing and vendor lock-in. BotWire is free for chat use cases and works instantly without configuration.

Is this actually free? Yes — 1000 writes per day per namespace, unlimited reads, forever. Most chat apps write 10-50 messages per user per day. You'd need 20+ daily active users to hit limits.

What about data privacy? Data is stored encrypted. You can self-host the MIT-licensed version on your infrastructure if you need complete control. The hosted version at botwire.dev is for convenience.

BotWire Memory solves persistent state for Vercel AI SDK applications without the complexity of traditional databases. Your chat history survives deployments, your users stay engaged, and your conversational AI actually remembers what it's talking about.

pip install botwire

Get started at botwire.dev.

Install in one command:

pip install botwire

Start free at botwire.dev