Conversation Memory

easy5 minconversation-memory.ts

Your chatbot forgets the user’s name between messages. You want every turn of a thread remembered.

What it shows

  • Enable memory on Cogitator with a context-builder budget
  • Pass the same threadId on every run
  • Read the stored messages back from cog.memory

The code

conversation-memory.ts
import { Agent, Cogitator } from '@cogitator-ai/core';
import { unwrap } from '@cogitator-ai/memory';

const apiKey = process.env.GOOGLE_API_KEY;
if (!apiKey) throw new Error('Set GOOGLE_API_KEY');

const cog = new Cogitator({
  llm: { providers: { google: { apiKey } } },
  memory: {
    adapter: 'memory',
    contextBuilder: { maxTokens: 4000, strategy: 'recent' },
  },
});

const agent = new Agent({
  name: 'memory-bot',
  model: 'google/gemini-3.5-flash-lite',
  instructions: 'You are a helpful assistant that remembers what the user tells you. Be concise.',
  temperature: 0.3,
});

const threadId = 'user-42';

for (const input of [
  'My name is Alex and I live in Berlin.',
  'I have two cats named Luna and Mochi.',
  'What are my cats called and where do I live?',
]) {
  const result = await cog.run(agent, { input, threadId });
  console.log(`User: ${input}\nAssistant: ${result.output}\n`);
}

if (cog.memory) {
  const entries = unwrap(await cog.memory.getEntries({ threadId }));
  console.log(`Thread "${threadId}" holds ${entries.length} messages`);
}

await cog.close();

Run it

install · bash
pnpm add @cogitator-ai/core @cogitator-ai/memory
EnvironmentGOOGLE_API_KEY
run · bash
GOOGLE_API_KEY=your-key npx tsx conversation-memory.ts

Or run the full example from a clone of the repository (keys in .env):

repository · bash
npx tsx examples/memory/01-basic-memory.ts

Learn more