Skip to main content

AI Agents with Memory

PraisonAI provides comprehensive memory capabilities for AI agents, from simple file-based storage to advanced multi-agent RAG systems.
FeatureKnowledgeMemory
When UsedPre-loaded before agent executionCreated and updated during runtime
PurposeProvide static reference informationStore dynamic context and interactions
StorageRead-only knowledge baseRead-write memory store
PersistencePermanent until explicitly changedCan be temporary (STM) or persistent (LTM)
UpdatesManual updates through knowledge filesAutomatic updates during agent execution

Memory Types

Short-term Memory

Rolling buffer of recent context. Auto-expires when limit reached. High-importance items can be auto-promoted to long-term.

Long-term Memory

Persistent important facts sorted by importance score. Supports semantic search with RAG.

Entity Memory

Named entities (people, places, organizations) with attributes and relationships.

Episodic Memory

Date-based interaction history. Configurable retention period with automatic cleanup.

Storage Providers

ProviderDependenciesDescription
memory=TrueNoneFile-based JSON storage (default)
memory="file"NoneExplicit file-based storage
memory="sqlite"Built-inSQLite with indexing
memory="chromadb"chromadbVector/semantic search
memory="mem0"mem0aiGraph memory, cloud
memory="rag"chromadbRAG-based semantic search

Quick Start - Single Agent (Zero Dependencies)

Enable persistent memory for agents without any extra packages. Memory is automatically injected into conversations.
from praisonaiagents import Agent

# Enable memory with a single parameter
agent = Agent(
    name="Personal Assistant",
    instructions="You are a helpful assistant that remembers user preferences.",
    memory=True  # Enables file-based memory (no extra deps!)
)

# Memory is automatically injected into conversations
result = agent.start("My name is John and I prefer dark mode")
result = agent.start("What's my name?")  # Agent recalls: "John"

Single Agent Configuration

from praisonaiagents import Agent

# Full configuration
agent = Agent(
    name="Assistant",
    memory={
        "provider": "file",           # Storage provider
        "user_id": "user_123",        # User isolation
        "short_term_limit": 100,      # Max short-term items
        "long_term_limit": 1000,      # Max long-term items
        "importance_threshold": 0.7,  # Min importance for auto-promotion
        "auto_promote": True,         # Auto-promote high-importance items
        "episodic_retention_days": 30 # Days to keep episodic memories
    }
)

Storage Structure

Memory is stored in JSON files under .praison/memory/{user_id}/:
.praison/memory/user_123/
├── config.json        # Memory configuration
├── short_term.json    # Rolling buffer (recent context)
├── long_term.json     # Persistent facts
├── entities.json      # Named entities
├── episodic/          # Date-based memories
│   ├── 2024-12-15.json
│   └── 2024-12-14.json
└── summaries.json     # LLM-generated summaries

Quick Start - Multi-Agent Memory

For multi-agent workflows using PraisonAIAgents, memory enables agents to share information and maintain context across tasks.
1

Install Package

pip install "praisonaiagents[memory]" duckduckgo_search 
2

Set API Key

export OPENAI_API_KEY=your_api_key_here
3

Create a file

Create app.py:
from praisonaiagents import Agent, Task, PraisonAIAgents
from praisonaiagents.tools import duckduckgo

# Create research agent with memory
research_agent = Agent(
    role="Research Analyst",
    goal="Research and document key information about topics",
    backstory="Expert at analyzing and storing information in memory",
    llm="gpt-4o-mini",
    tools=[duckduckgo]
)

# Create blog writer agent
blog_agent = Agent(
    role="Blog Writer",
    goal="Write a blog post about the research",
    backstory="Expert at writing blog posts",
    llm="gpt-4o-mini"
)

# Create tasks
research_task = Task(
    description="Research and document key information about AI trends",
    expected_output="Detailed research findings about AI trends",
    agent=research_agent
)

blog_task = Task(
    description="Write a blog post about the research findings",
    expected_output="Well-written blog post based on research",
    agent=blog_agent
)

# Create and start the agents with memory enabled
agents = PraisonAIAgents(
    agents=[research_agent, blog_agent],
    tasks=[research_task, blog_task],
    memory=True
)   

result = agents.start()
print(result)
4

Run

python app.py

Multi-Agent Memory Configuration

# Create agents with advanced memory configuration
agents = PraisonAIAgents(
    agents=[agent],
    tasks=[task],
    memory=True,
    memory_config={
        "provider": "rag",  # Use RAG for semantic search
        "use_embedding": True,  # Enable embeddings for better search
        "short_db": ".praison/short_term.db",  # Path for short-term memory
        "long_db": ".praison/long_term.db",    # Path for long-term memory
        "rag_db_path": ".praison/chroma_db",   # Path for vector database
        "ttl": 3600,  # Time to live for memory items (in seconds)
    }
)

Memory Methods

Store Memory

# Short-term (recent context)
agent.store_memory("User asked about Python", memory_type="short_term")

# Long-term (important facts)
agent.store_memory("User's name is Alice", memory_type="long_term", importance=0.95)

# Entity
agent._memory_instance.add_entity(
    name="Alice",
    entity_type="person",
    attributes={"role": "developer", "company": "Acme"}
)

# Episodic (date-based)
agent._memory_instance.add_episodic("Had a meeting about project X")

Retrieve Memory

# Get memory context for prompts
context = agent.get_memory_context(query="user preferences")

# Search memories
results = agent._memory_instance.search("Python", limit=10)

# Get specific memory types
short_term = agent._memory_instance.get_short_term(limit=10)
long_term = agent._memory_instance.get_long_term(limit=10)
entities = agent._memory_instance.get_all_entities(entity_type="person")

Memory Management

# Clear short-term memory
agent._memory_instance.clear_short_term()

# Clear all memory
agent._memory_instance.clear_all()

# Get statistics
stats = agent._memory_instance.get_stats()
print(stats)
# {'user_id': 'user_123', 'short_term_count': 5, 'long_term_count': 10, ...}

# Export/Import
data = agent._memory_instance.export()
agent._memory_instance.import_data(data)

Memory Quality Control (Multi-Agent)

# Store with quality metrics
agents.memory.store_long_term(
    text="Important information to remember",
    metadata={
        "task_id": "task_123",
        "agent": "research_agent"
    },
    completeness=0.9,
    relevance=0.85,
    clarity=0.95,
    accuracy=0.9,
    weights={
        "completeness": 0.3,
        "relevance": 0.3,
        "clarity": 0.2,
        "accuracy": 0.2
    }
)

# Search with quality filter
results = agents.memory.search_long_term(
    query="search query",
    min_quality=0.8,
    limit=5
)

Session Save/Resume

Save and resume conversation sessions for later continuation:
from praisonaiagents.memory import FileMemory

memory = FileMemory(user_id="user_123")

# Add context during conversation
memory.add_short_term("User is working on ML project")
memory.add_long_term("User prefers Python", importance=0.9)

# Save session with conversation history
conversation = [
    {"role": "user", "content": "Help me with ML"},
    {"role": "assistant", "content": "I'd be happy to help..."}
]
memory.save_session("ml_project", conversation_history=conversation)

# Later: Resume the session
session_data = memory.resume_session("ml_project")

# List all saved sessions
sessions = memory.list_sessions()
for s in sessions:
    print(f"{s['name']} - saved at {s['saved_at']}")

# Delete a session
memory.delete_session("old_session")

Context Compression

Compress short-term memory to save context window space:
from praisonaiagents.memory import FileMemory

memory = FileMemory(user_id="user_123", config={"short_term_limit": 100})

# Add many items during conversation
for i in range(50):
    memory.add_short_term(f"Discussion point {i}")

# Manual compression with LLM summarization
def llm_summarize(prompt):
    return agent.chat(prompt)

summary = memory.compress(llm_func=llm_summarize, max_items=10)
# Compresses older items into a summary, keeps recent 10

# Auto-compress when memory gets full (70% threshold)
memory.auto_compress_if_needed(threshold_percent=0.7, llm_func=llm_summarize)

Checkpointing

Create checkpoints before risky operations and restore if needed:
from praisonaiagents.memory import FileMemory

memory = FileMemory(user_id="user_123")

# Create checkpoint before making changes
checkpoint_id = memory.create_checkpoint("before_refactor")

# Optionally include file snapshots
checkpoint_id = memory.create_checkpoint(
    "before_refactor",
    include_files=["main.py", "config.yaml"]
)

# Make changes...
memory.clear_all()
# Something went wrong!

# Restore from checkpoint
memory.restore_checkpoint(checkpoint_id)

# Restore with file snapshots
memory.restore_checkpoint(checkpoint_id, restore_files=True)

# List all checkpoints
checkpoints = memory.list_checkpoints()

# Delete old checkpoint
memory.delete_checkpoint("old_checkpoint")

Memory Slash Commands

Handle memory commands programmatically (useful for CLI/chat interfaces):
from praisonaiagents.memory import FileMemory

memory = FileMemory(user_id="user_123")

# Available commands
result = memory.handle_command("/memory show")      # Display stats
result = memory.handle_command("/memory add User likes coffee")  # Add memory
result = memory.handle_command("/memory search Python")  # Search
result = memory.handle_command("/memory clear short")    # Clear short-term
result = memory.handle_command("/memory save my_session")  # Save session
result = memory.handle_command("/memory resume my_session")  # Resume
result = memory.handle_command("/memory sessions")   # List sessions
result = memory.handle_command("/memory compress")   # Compress
result = memory.handle_command("/memory checkpoint") # Create checkpoint
result = memory.handle_command("/memory restore cp_123")  # Restore
result = memory.handle_command("/memory checkpoints")  # List checkpoints
result = memory.handle_command("/memory refresh")    # Reload from disk
result = memory.handle_command("/memory help")       # Show all commands
CommandDescription
/memory showDisplay memory stats and recent items
/memory add <content>Add to long-term memory
/memory clear [short|all]Clear memory
/memory search <query>Search memories
/memory save <name>Save session
/memory resume <name>Resume session
/memory sessionsList saved sessions
/memory compressCompress short-term memory
/memory checkpoint [name]Create checkpoint
/memory restore <id>Restore checkpoint
/memory checkpointsList checkpoints
/memory refreshReload from disk

Auto-Generated Memories

Automatically extract and store memories from conversations without manual intervention:
from praisonaiagents.memory import FileMemory, AutoMemory

# Create base memory
memory = FileMemory(user_id="user123")

# Wrap with auto-generation
auto = AutoMemory(memory, enabled=True)

# Process interactions - memories are automatically extracted
memories = auto.process_interaction(
    user_message="My name is John and I prefer Python for backend work",
    assistant_response="Nice to meet you, John! Python is great for backend."
)

# Extracted memories:
# - name: "John" (entity)
# - preference: "Python for backend work" (long-term)

print(f"Extracted {len(memories)} memories automatically")

Pattern-Based Extraction

AutoMemory uses fast pattern matching (no LLM calls) to extract:
TypeExamplesImportance
Name”My name is John”, “I’m Alice”0.95
Role”I’m a developer”, “I work as an engineer”0.85
Preference”I prefer Python”, “I like dark mode”0.70
Project”Working on ML project”, “Building an app”0.75
Technology”Using Python”, “prefer TypeScript”0.70
Location”I live in NYC”, “Based in London”0.60

LLM-Enhanced Extraction

For better accuracy, enable LLM-based extraction:
from praisonaiagents.memory import AutoMemory, AutoMemoryExtractor

# Create extractor with LLM
extractor = AutoMemoryExtractor(
    min_importance=0.6,
    use_llm=True,
    llm_func=lambda prompt: agent.chat(prompt)
)

# Use with AutoMemory
auto = AutoMemory(
    memory=memory,
    extractor=extractor,
    enabled=True
)

Direct FileMemory Usage

from praisonaiagents.memory import FileMemory, create_memory

# Create memory instance directly
memory = FileMemory(
    user_id="user_123",
    base_path=".praison/memory",
    config={"short_term_limit": 50},
    verbose=1
)

# Use all memory features
memory.add_short_term("Recent interaction")
memory.add_long_term("Important fact", importance=0.9)
memory.add_entity("John", "person", {"role": "manager"})
memory.add_episodic("Meeting notes")

# Search and retrieve
results = memory.search("John")
context = memory.get_context(query="manager")

# Convenience function
memory = create_memory(user_id="user_456")

How Memory Injection Works

When memory=True, the agent automatically:
  1. Loads existing memories from storage on initialization
  2. Builds a memory context string with important facts, entities, and recent context
  3. Injects the context into the system prompt before each LLM call
  4. Persists new memories to storage after interactions
# System prompt with memory injection looks like:
"""
You are a helpful assistant.

Your Role: Assistant
Your Goal: Help users with their tasks

## Memory (Information you remember about the user)
## Important Facts
- User's name is Alice
- User works as a software engineer
## Known Entities
- Alice (person): role=developer, company=Acme
## Recent Context
- User prefers detailed explanations
"""

Best Practices

Set higher importance (0.8-1.0) for critical facts like user names, preferences, and key information. Lower importance (0.3-0.5) for transient context.
Always set user_id when building multi-user applications to prevent memory leakage between users.
Call cleanup_episodic() periodically to remove old date-based memories and save storage space.
Store people, places, and organizations as entities with attributes rather than plain text for better retrieval.
Use file-based memory for simple single-agent apps, RAG for multi-agent semantic search, and graph memory for complex relationships.

Troubleshooting

Memory Issues

If memory isn’t working as expected:
  • Check memory configuration
  • Enable verbose mode for debugging
  • Verify memory provider settings
  • Check file permissions for storage path

Context Flow

If context isn’t being maintained:
  • Review task dependencies
  • Check memory configuration
  • Verify agent communication
  • Ensure user_id is consistent across sessions

Next Steps

For optimal results, configure memory settings based on your specific use case requirements and expected interaction patterns.