brainMemory Management

Agent memory management — from simple window memory to vector-backed RAG, per-call identity routing, multi-tenant tracking, and suspend/resume.

Agents automatically maintain conversation history through their memory system. Memory is retrieved before each model call and stored after each response.

🔄 Memory Flow

spinner

Basic Memory (Window Memory)

// Agent with conversation memory
agent = aiAgent(
    name       : "ChatBot",
    description: "A conversational assistant",
    memory     : aiMemory( "window" )
)

// First interaction
agent.run( "My name is Luis" )

// Second interaction — agent remembers
agent.run( "What's my name?" )
// → "Your name is Luis"

// Access memory messages
messages = agent.getMemoryMessages()

// Clear memory when needed
agent.clearMemory()

Multiple Memory Systems

Agents can use multiple memory instances simultaneously — useful for combining conversation history with vector/semantic memory:

Per-Call Identity Routing (v3.0+)

In v3.0, you can route memory operations to specific users and conversations by passing userId and conversationId per call — one agent instance can serve multiple users without sharing state:

The underlying memory operations (add, getAll, clear, trim, seed) all accept userId and conversationId as optional per-call overrides, so routing happens transparently.

Suspend & Resume (v3.0+)

Agents can be suspended mid-run (e.g., by HumanInTheLoopMiddleware) and resumed later. A checkpointer memory backend stores the agent's state:

For streaming agents, use resumeStream() with the same parameters.

🏢 Multi-Tenant Usage Tracking

Track AI usage per tenant for billing and cost allocation:

Usage data (tokens, model, tenant) is fired as an onAITokenCount event on every call:

See Multi-Tenant Memory Guide for a comprehensive multi-tenancy setup.

Memory Types Quick Reference

Type
Best For

window

Simple session conversations (in-memory, lost on restart)

cache

Persistent across requests within cache TTL

file

Long-lived conversations stored to disk as JSON

session

Web applications tied to HTTP session

summary

Long conversations (older messages are AI-summarized)

jdbc

Full database persistence with SQL queries

hybrid

Recent window + semantic retrieval combined

box / chroma / pinecone / etc.

Vector-backed semantic memory

See Memory Systems for complete configuration options.

Last updated