◈AI Codex
Developer PathStep 16 of 21
← Prev·Next →
Infrastructure & DeploymentHow It Works

Database-backed conversation history with Supabase and Claude

In brief

In-memory arrays disappear on page reload. How to persist conversation history to Supabase, load it back on session resume, and prune context intelligently.

8 min read·

Contents

♡Sign in to save

Every Claude integration starts with an in-memory array of messages. This works for demos. It breaks the moment a user refreshes the page, closes a tab, or comes back the next day.

Proper conversation persistence means:

  • Messages survive page reloads and deployments
  • Users can resume where they left off
  • You can inspect conversations for debugging
  • You can build features like conversation search, sharing, or branching

Here is the complete implementation with Supabase.

Schema design

Two tables: conversations (the session) and messages (the turns).

-- Conversations table
create table conversations (
  id uuid primary key default gen_random_uuid(),
  user_id uuid references auth.users(id) on delete cascade,
  title text,
  created_at timestamptz default now(),
  updated_at timestamptz default now()
);

-- Messages table
create table messages (
  id uuid primary key default gen_random_uuid(),
  conversation_id uuid references conversations(id) on delete cascade,
  role text not null check (role in ('user', 'assistant')),
  content text not null,
  tokens_used integer,
  created_at timestamptz default now()
);

-- Indexes for fast lookups
create index on conversations(user_id, updated_at desc);
create index on messages(conversation_id, created_at asc);

-- Row-level security
alter table conversations enable row level security;
alter table messages enable row level security;

create policy "Users see own conversations"
  on conversations for all using (auth.uid() = user_id);

create policy "Users see own messages"
  on messages for all
  using (
    exists (
      select 1 from conversations
      where conversations.id = messages.conversation_id
      and conversations.user_id = auth.uid()
    )
  );

Loading a conversation

// lib/conversations.ts
import { createClient } from '@/lib/supabase/server'

export type Message = {
  id: string
  role: 'user' | 'assistant'
  content: string
  created_at: string
}

export async function getConversation(conversationId: string): Promise<Message[]> {
  const supabase = await createClient()

  const { data, error } = await supabase
    .from('messages')
    .select('id, role, content, created_at')
    .eq('conversation_id', conversationId)
    .order('created_at', { ascending: true })

  if (error) {
    console.error('getConversation error:', error)
    return []
  }

  return data ?? []
}

export async function getUserConversations(userId: string) {
  const supabase = await createClient()

  const { data } = await supabase
    .from('conversations')
    .select('id, title, created_at, updated_at')
    .eq('user_id', userId)
    .order('updated_at', { ascending: false })
    .limit(20)

  return data ?? []
}

Saving messages

export async function saveMessage(
  conversationId: string,
  role: 'user' | 'assistant',
  content: string,
  tokensUsed?: number
) {
  const supabase = await createClient()

  const [_, updateResult] = await Promise.all([
    supabase.from('messages').insert({
      conversation_id: conversationId,
      role,
      content,
      tokens_used: tokensUsed ?? null,
    }),
    supabase
      .from('conversations')
      .update({ updated_at: new Date().toISOString() })
      .eq('id', conversationId),
  ])

  return updateResult
}

export async function createConversation(userId: string, firstMessage: string) {
  const supabase = await createClient()

  // Auto-generate title from first message (truncated)
  const title = firstMessage.length > 60
    ? firstMessage.slice(0, 57) + '...'
    : firstMessage

  const { data } = await supabase
    .from('conversations')
    .insert({ user_id: userId, title })
    .select('id')
    .single()

  return data?.id ?? null
}

Wiring it into your API route

// app/api/chat/route.ts
import { getServerSession } from 'next-auth'
import { authOptions } from '@/lib/auth'
import { getConversation, saveMessage, createConversation } from '@/lib/conversations'
import Anthropic from '@anthropic-ai/sdk'

export async function POST(request: Request) {
  const session = await getServerSession(authOptions)
  if (!session?.user?.id) {
    return Response.json({ error: 'Unauthorized' }, { status: 401 })
  }

  const { userMessage, conversationId: existingId } = await request.json()
  const userId = session.user.id

  // Get or create conversation
  let conversationId = existingId
  if (!conversationId) {
    conversationId = await createConversation(userId, userMessage)
    if (!conversationId) {
      return Response.json({ error: 'Failed to create conversation' }, { status: 500 })
    }
  }

  // Load message history
  const history = await getConversation(conversationId)

  // Build messages array for Claude
  const messages = [
    ...history.map(m => ({ role: m.role as 'user' | 'assistant', content: m.content })),
    { role: 'user' as const, content: userMessage },
  ]

  // Save user message before calling Claude
  await saveMessage(conversationId, 'user', userMessage)

  const anthropic = new Anthropic()
  const response = await anthropic.messages.create({
    model: 'claude-opus-5-5',
    max_tokens: 1024,
    messages,
  })

  const assistantContent = response.content[0].type === 'text'
    ? response.content[0].text
    : ''

  // Save assistant response with token count
  const totalTokens = response.usage.input_tokens + response.usage.output_tokens
  await saveMessage(conversationId, 'assistant', assistantContent, totalTokens)

  return Response.json({
    content: assistantContent,
    conversationId,
  })
}

Context window management

Claude has a finite context window. Long conversations eventually exceed it. Prune old messages when the conversation grows:

const MAX_MESSAGES_IN_CONTEXT = 20

function pruneHistory(history: Message[]): Message[] {
  if (history.length <= MAX_MESSAGES_IN_CONTEXT) return history

  // Always keep the first message (often sets important context)
  // Then keep the most recent N-1 messages
  const first = history[0]
  const recent = history.slice(-(MAX_MESSAGES_IN_CONTEXT - 1))
  return [first, ...recent]
}

// Use in route handler:
const prunedHistory = pruneHistory(history)
const messages = [
  ...prunedHistory.map(m => ({ role: m.role, content: m.content })),
  { role: 'user', content: userMessage },
]

A more sophisticated approach counts tokens instead of message count. The usage field returned by Claude tells you how many tokens were used — track this to build a soft limit.

Loading conversation in the UI

// app/chat/[conversationId]/page.tsx
import { getConversation } from '@/lib/conversations'

export default async function ChatPage({ params }: { params: { conversationId: string } }) {
  const messages = await getConversation(params.conversationId)

  return (
    <div>
      {messages.map(msg => (
        <div key={msg.id} className={`message message--${msg.role}`}>
          {msg.content}
        </div>
      ))}
      {/* ChatInput component here */}
    </div>
  )
}

The URL-based approach (/chat/[conversationId]) means users can bookmark, share, or return to any conversation. It is also debuggable — you can look up the conversation ID in Supabase directly.

Do message threads mean I can skip storing history?

No. Keep this table as your source of truth. Message threads, an API beta Anthropic released on November 2, 2026, let you send only the new message on each turn instead of the whole conversation. Replies in long conversations start faster. But a thread is short-lived: it expires 5 minutes after its last request, you cannot read it back, and once it is gone the API returns a 404 with thread_not_found. When that happens you resend the full conversation from your own copy, which is what this table is for.

What changes and what does not:

  • Speed, not cost. Claude still reads the whole conversation every turn, so token usage and price stay the same. The gain is time to first token, and it grows with conversation size.
  • Best for long, busy sessions. Agent loops and long working sessions benefit most. In a chat app where people pause for minutes between messages, many turns will find the thread already expired.
  • Caching is still on you. A thread does not cache your prompt. Keep sending cache_control if you use prompt caching.
  • Not everywhere. Threads are not available with zero data retention, for HIPAA-ready organizations, or in the Batch API.

The shape of a call (beta header message-threads-2026-08-12 on every request):

// First turn: full history, system prompt and tools, plus thread: create
const first = await anthropic.beta.messages.create({
  model: 'claude-opus-5-5',
  max_tokens: 1024,
  system: SYSTEM_PROMPT,
  messages: fullHistory,
  thread: { type: 'create' },
  betas: ['message-threads-2026-08-12'],
})

// Later turns: only the new message and the previous response id.
// Leave out system and tools.
const next = await anthropic.beta.messages.create({
  model: 'claude-opus-5-5',
  max_tokens: 1024,
  messages: [{ role: 'user', content: userMessage }],
  thread: { type: 'continue', previous_message_id: first.id },
  betas: ['message-threads-2026-08-12'],
})

Save every message to Supabase as before. If a continue call fails with thread_not_found, load the history and start a new thread with type: 'create'.

What not to do

  • Do not store raw HTML in the content column — store markdown and render it on the client
  • Do not lazy-load message history — load it server-side before rendering to avoid layout shift
  • Do not skip the updated_at update — it is how you sort conversations by recency
  • Do not trust the client to pass conversationId without verifying ownership — always check the conversation belongs to the authenticated user

Further reading

  • Models overview — current Claude model IDs and context window limits
  • Message threads — the beta for sending only new messages each turn, with its limits
  • Prompt caching — how to cache conversation history to cut costs on long persistent conversations
  • Token counting — measuring context window usage precisely before you hit limits

Related tools

Next in Developer Path · Step 17 of 21

Continue to the next article in the learning path

Next article →

Weekly brief

For people actually using Claude at work.

Each week: one thing Claude can do in your work that most people haven't figured out yet — plus the failure modes to avoid. No tutorials. No hype.

No spam. Unsubscribe anytime.

What to read next

All articles →