Guides

Building a ChatBot with Memory

A complete example using Databaset and Anthropic Claude.

Full example

import { Memory } from '@databaset/sdk'
import Anthropic from '@anthropic-ai/sdk'

const memory = new Memory()
const anthropic = new Anthropic()

async function chat(userId: string, userMessage: string) {
  const context = await memory.recall({
    userId,
    query: userMessage,
    limit: 5
  })

  const systemPrompt = context
    ? `You are a helpful assistant. Here's what you know about this user:\n${context}`
    : 'You are a helpful assistant.'

  const response = await anthropic.messages.create({
    model: 'claude-sonnet-4-6',
    max_tokens: 1024,
    system: systemPrompt,
    messages: [{ role: 'user', content: userMessage }]
  })

  const assistantMessage = response.content[0].text

  await memory.store({
    userId,
    text: `User said: ${userMessage}`
  })

  return assistantMessage
}

How it works

  1. Recall: fetch relevant memories for the user's message
  2. Prompt: inject context into the system prompt
  3. Respond: get an AI reply with personalized context
  4. Store: save the conversation for future recall

Usage

const reply = await chat('user_123', 'I prefer dark mode in all apps')
console.log(reply)

Next time

When the same user asks about UI preferences, Databaset recalls the dark mode preference automatically.