Memory Systems

Comprehensive guide on using standard memory systems in BoxLang AI for conversation history and context retention.

Memory systems enable AI to maintain context across multiple interactions, making conversations more coherent and contextually aware. This guide covers standard conversation memory types that store and manage message history.

πŸ“– Looking for Vector Memory? For semantic search and retrieval using embeddings, see the Vector Memory Guide.

πŸ“‹ Table of Contents


πŸ”’ Multi-Tenant Isolation

All memory types support multi-tenant isolation through userId and conversationId parameters:

πŸ—οΈ Multi-Tenant Architecture

  • userId: Isolate conversations per user in multi-user applications

  • conversationId: Separate multiple conversations for the same user

  • Combined: Use both for complete conversation isolation

Multi-tenant support is built into ALL memory types including:

  • Standard memories: Window, Summary, Session, File, Cache, JDBC

  • Vector memories: All 11 vector providers (see Vector Memory Guide)

  • Hybrid memory: Combines recent + semantic with isolation

Basic Multi-Tenant Usage

Accessing Tenant Identifiers

For advanced patterns including security considerations, filtering strategies, and enterprise multi-tenancy, see the Multi-Tenant Memory Guide.


πŸ“– Overview

Memory in AI systems allows for:

  • Context retention across multiple turns

  • Conversation history management

  • State persistence in long-running applications

  • Flexible storage options (memory, session, file, database)

Without memory, each AI call is independent with no knowledge of previous interactions. Standard memory types focus on managing conversation messages chronologically, while Vector Memory provides semantic search capabilities.


πŸ—‚οΈ Memory Types

BoxLang AI provides several standard memory implementations for conversation history management:

πŸ”„ Memory Type Decision Flow

Memory Type Comparison

Choose the right memory type for your use case:

Feature
Windowed
Summary
Session
File
Cache
JDBC

Context Preservation

Recent only

Full (compressed)

Recent only

All messages

Recent only

All messages

Old Messages

Discarded

Summarized

Discarded

Kept

Expired

Kept

Token Usage

Low

Moderate

Low-Moderate

High

Low

Medium-High

Memory Loss

High

Low

Medium

None

Medium

None

Multi-Tenant

βœ…

βœ…

βœ…

βœ…

βœ…

βœ…

Best For

Quick chats

Long conversations

Web apps

Audit trails

Distributed apps

Enterprise systems

Setup Complexity

Simple

Moderate

Simple

Simple

Moderate

Complex

Cost

Lowest

Low-Medium

Low

Low

Medium

Medium-High

Historical Awareness

None

Excellent

Limited

Perfect

None

Perfect

Persistence

None

None

Session scope

File system

Cache provider

Database

Need Semantic Search? Check out Vector Memory for embedding-based retrieval including BoxVector (in-memory), ChromaDB, PostgreSQL pgvector, Pinecone, Qdrant, Weaviate, Milvus, and Hybrid memory combining recent + semantic.

Windowed Memory

Maintains the most recent N messages, automatically discarding older messages when the limit is reached.

Best for:

  • Short conversations

  • Real-time chat applications

  • Cost-conscious applications

  • Simple context requirements

Limitations:

  • Loses all context from discarded messages

  • No awareness of earlier conversation history

  • Can lose important facts mentioned earlier

Summary Memory

Automatically summarizes older messages when the limit is reached, keeping summaries + recent messages. This provides the best of both worlds: full context awareness with controlled token usage.

How it works:

  1. Messages accumulate normally until maxMessages is reached

  2. When threshold exceeded, older messages are summarized

  3. Summary is kept as a special assistant message

  4. Recent messages (last N) remain unmodified

  5. Progressive summarization: new summaries build on previous ones

Best for:

  • Long conversations with important history

  • Complex context requirements

  • Applications needing historical awareness

  • Customer support systems

  • Research or analysis tasks

  • Multi-session interactions

Advantages:

  • Preserves key facts and decisions from old messages

  • Maintains full conversation awareness

  • Moderate token usage (lower than keeping all messages)

  • No sudden context loss

  • AI can reference earlier conversation points

Session Memory

Persists conversation history in the web session scope, surviving page refreshes. Session memory automatically creates a composite key combining key + userId + conversationId to ensure complete isolation.

Best for:

  • Web applications

  • Multi-page conversations

  • User-specific context

  • Persistent chat interfaces

  • Multi-user web apps

File Memory

Stores conversation history in files for long-term persistence.

Best for:

  • Long-term storage

  • Audit trails

  • Offline analysis

  • Cross-session continuity

Cache Memory

Stores conversation history in CacheBox for distributed applications. Cache memory automatically creates a composite cache key combining cacheKey + userId + conversationId for isolation.

Best for:

  • Distributed applications

  • Load-balanced environments

  • Applications with existing CacheBox

  • Scalable session management

  • Multi-user distributed systems

Features:

  • Integrates with any CacheBox provider (Redis, Memcached, Couchbase, etc.)

  • Automatic expiration policies

  • Distributed cache support

  • High-performance access

JDBC Memory

Stores conversation history in a database using JDBC for enterprise persistence. JDBC memory includes userId and conversationId columns for automatic multi-tenant isolation.

Best for:

  • Enterprise applications

  • Multi-user systems

  • Compliance requirements

  • Centralized storage

  • Cross-platform access

  • Advanced querying and reporting

Features:

  • Works with any JDBC-compatible database

  • Automatic table creation with multi-tenant columns

  • Query conversation history by user/conversation

  • Full ACID compliance

  • Supports PostgreSQL, MySQL, SQL Server, Oracle, etc.

Table Structure:

Query Examples:


Creating Memory

Basic Memory Creation

Memory with Configuration

Pre-populated Memory


Using Memory in Pipelines

Window Memory-Enabled Chat

Memory in Model Pipelines

Summary Memory in Long Conversations

Streaming with Memory


Memory Patterns

Pattern 1: Conversation Manager

Encapsulate memory logic in a reusable component:

Pattern 2: Multi-User Memory

Modern approach using built-in userId and conversationId parameters:

Pattern 3: Contextual Memory Switching

Switch memory contexts based on conversation topics:

Pattern 4: Memory with Metadata

Track additional context with metadata:

Pattern 5: Memory Summarization

Explicitly summarize conversation history:


Best Practices

1. Choose the Right Memory Type

2. Set Appropriate Limits

3. Always Include System Messages

4. Handle Memory Lifecycle

5. Monitor Token Usage

6. Implement Memory Persistence

7. Handle Edge Cases

8. Multi-Tenant Security Considerations


Advanced Examples

Example 1: RAG with Memory

Combine retrieval-augmented generation with conversation memory:

Example 2: Multi-Stage Memory Pipeline

Example 3: Adaptive Memory

Adjust memory size based on conversation complexity:


Advanced Memory

Vector Memory

For semantic search and retrieval using embeddings, see the comprehensive Vector Memory Guide which covers:

  • BoxVectorMemory - In-memory vector storage for development

  • HybridMemory - Combines recent messages with semantic search

  • ChromaVectorMemory - ChromaDB integration

  • PostgresVectorMemory - PostgreSQL pgvector extension

  • PineconeVectorMemory - Pinecone cloud vector database

  • QdrantVectorMemory - Qdrant vector search engine

  • WeaviateVectorMemory - Weaviate knowledge graph

  • MilvusVectorMemory - Milvus vector database

Vector memory enables finding relevant past conversations based on meaning rather than recency.

Custom Memory

You can create custom memory implementations for specialized requirements:

See the Custom Memory Guide for complete examples and patterns.


See Also


Next Steps: Learn about Vector Memory for semantic search or streaming in pipelines for real-time responses.

Last updated