Cogitator
Channels

Smart Memory

Three-level memory system with knowledge graph, core facts, and compaction.

Overview

The smart memory system provides three levels of persistent memory for your assistant, from key-value facts in the system prompt to a knowledge graph and the conversation history. RuntimeBuilder sets all three up from cogitator.yml.

Level 1: Core Facts

Key-value pairs that are always included in the system prompt. Store the most important facts about the user here — name, timezone, preferences.

import { CoreFactsStore } from '@cogitator-ai/memory';

const coreFacts = new CoreFactsStore({ path: './memory.db' });
await coreFacts.initialize();

await coreFacts.set('user_name', 'Alex');
await coreFacts.set('timezone', 'Europe/Berlin');

const prompt = await coreFacts.formatForPrompt();
// one "key: value" line per fact

Core facts are injected into the agent instructions automatically by RuntimeBuilder (under "What I know about the user"), re-read for every message, so facts saved during a conversation are visible immediately. The agent saves one with remember({ fact, quote, isCoreFact: true, coreFactKey }), kept only when the user said it (see remember). CoreFactsStore keeps each key as memory items of the slot core:<key>, so a new value supersedes the one before and getHistory(key) gives every value. delete(key) lets the current value go and keeps its history. A memory file written by an earlier version is moved into memory items on initialize(), and its old core_facts and core_facts_history tables are renamed with a _legacy suffix.

Level 2: Knowledge Graph

Knowledge graph with nodes and relationships, stored in SQLite (or Postgres when memory.adapter: postgres). The agent uses remember, recall, and forget tools to interact with it, and they keep memory items in the memory store beside it: remember in the memory of the user the message comes from, or of the thread when there is no user, recall sees the user's items and the thread's, and forget lets go only the run's own.

cogitator.yml
memory:
  knowledgeGraph: true
  autoExtract: true

Memory Tools

When knowledgeGraph is enabled, three tools are added to the agent:

remember - Save a fact to the knowledge graph and as a memory item (optionally also as a core fact), with the user's own words that state it as quote

User: "My dog's name is Luna"
Agent calls: remember({ fact: "User's dog is named Luna", category: "pets", quote: "My dog's name is Luna" })

User: "Call me Alex"
Agent calls: remember({ fact: "Alex", quote: "Call me Alex", isCoreFact: true, coreFactKey: "user_name" })

A fact counts as the user's only when its quote is found word for word in one of the 200 newest entries of the thread's active branch, in a message of the run's own user: a message another user wrote in a shared thread (its metadata.userId) does not count. Then it is a graph node with source: 'user' and the quote as its evidence, an active memory item trusted as the user's, and, with isCoreFact, a core fact. A fact whose quote is missing or not found is kept as a suggestion instead: a graph node with source: 'inferred' and confidence 0.6, a suggested memory item that no context reads, and never a core fact. The result says so (status, a note, and coreFact: false when a core fact was asked for), so a model cannot plant in memory what the user never said.

recall — Search memory by query

User: "What's my dog's name?"
Agent calls: recall({ query: "dog" })
→ Returns: { results: [{ fact: "User's dog is named Luna", ... }], coreFacts: { ... } }

recall searches the memory items of the user and the thread that hold now (up to 10, type: 'memory') and the stored fact text of the graph (up to 10 results), and always returns all core facts. Given the thread store as memory, it ranks the items with the search the hybrid context strategy recalls with: their words by BM25, the entities they name and the graph of items and entities around them, and a query that is only a greeting or a thanks finds no items. Without it an item matches when it holds every word of the query. Pass embeddingFn to createMemoryTools() to switch the graph search to semantic vector search. RuntimeBuilder does not.

forget - Forget the facts that match the query: graph facts whose text matches are deleted, and the run's own memory items (its user's, or its thread's for a run without a user) that hold the query as whole words are let go, past and suggested states included (no longer recalled or used, kept as history, listed in forgotten). A query in a script written without spaces, such as Chinese, Japanese or Thai, is matched as it stands

User: "Forget everything about my old address"
Agent calls: forget({ query: "address" })

Auto-extraction

When autoExtract is enabled, the AutoExtractMiddleware runs in the background for each user message and automatically extracts entities and relationships into the knowledge graph (using the assistant's LLM) — without the agent needing to call remember explicitly. It needs knowledgeGraph: true.

memory:
  autoExtract: true

Level 3: Conversation History

Session-based message history stored in the memory adapter (SQLite or Postgres). Each user gets their own session per channel.

Compaction

When the conversation history grows too long, automatic compaction summarizes older messages to stay within context limits:

cogitator.yml
memory:
  compaction:
    threshold: 50

When a conversation holds at least threshold messages, the gateway compacts it hierarchically:

  1. The newest turns, about 4000 tokens of them, are kept intact, a tool call together with its result
  2. Older messages are read in chapters by the assistant's LLM, which lists what they established (facts, tasks, decisions, constraints, errors, files, open questions), each item checked against the quotes it gives
  3. A handoff of those items replaces the old messages in the conversation history, and the next compaction adds to it instead of summarizing the summary

Owners can also compact on demand with /compact, or every conversation at once with /compact all.

Full Configuration

cogitator.yml
memory:
  adapter: sqlite # sqlite | postgres
  path: ~/.cogitator/memory.db # sqlite file (also holds core facts in postgres mode)
  connectionString: postgres://user:pass@host/db # postgres (or DATABASE_URL env), needs `pg`
  knowledgeGraph: true # enable knowledge graph + memory tools
  autoExtract: true # auto-extract entities from conversations
  compaction:
    threshold: 50 # messages before compaction triggers

Programmatic Setup

If you're not using RuntimeBuilder, you can set up memory manually:

import {
  SQLiteAdapter,
  SQLiteGraphAdapter,
  CoreFactsStore,
  MemoryItems,
} from '@cogitator-ai/memory';
import { Agent, createMemoryTools } from '@cogitator-ai/core';

const memory = new SQLiteAdapter({ provider: 'sqlite', path: './memory.db' });
await memory.connect();

const graph = new SQLiteGraphAdapter({ path: './memory.db' });
await graph.initialize();

const coreFacts = new CoreFactsStore({ path: './memory.db' });
await coreFacts.initialize();

const memoryTools = createMemoryTools({
  graphAdapter: graph,
  coreFacts,
  agentId: 'jarvis',
  memory, // the thread store remember checks quotes against
  items: new MemoryItems({ adapter: memory }), // memory items remember, recall and forget use
});

const agent = new Agent({
  id: 'jarvis',
  name: 'jarvis',
  model: 'google/gemini-3.8-flash',
  tools: [...memoryTools],
  instructions: 'You are a helpful assistant with persistent memory.',
});

Without memory, remember cannot check a quote and keeps every fact as the user's, as before: in the graph, in the core facts and as a memory item trusted as the user's, which stays suggested only because it has no quote that holds. A Cogitator keeps an agent's memory items under its configured id, else its name (memoryAgentId(agent)), so an agent created again with the same name finds them after a restart. Pass the same id as agentId to createMemoryTools.

On this page