Context vs Memory

Understanding the key differences between context management and memory systems in PraisonAI agents.

Context vs Memory

PraisonAI provides two distinct systems for managing information flow between agents: Context and Memory. Understanding when to use each is crucial for building efficient multi-agent workflows.

Diagram

Memory (Persistent)

Store

Recall

Recall

Agent 1

Memory Store

Agent 2

Agent 3

Context (Ephemeral)

Pass output

Pass output

Agent 1

Agent 2

Agent 3

Quick Comparison

AspectContextMemory
LifetimeSingle sessionPersists across sessions
StorageIn-memory onlyFile/Database
ScopeCurrent workflowAll workflows
Use CasePassing data between agentsLearning & remembering
PerformanceFast (no I/O)Slower (disk/network)
DependenciesNoneOptional (chromadb, etc.)

Context: Ephemeral Data Flow

Context is the default way agents share information within a single workflow execution. Data flows from one agent to the next and is lost when the session ends.

Diagram
Agent 3Agent 2Agent 1UserAgent 3Agent 2Agent 1UserContext lost after session endsTask inputProcessOutput as contextProcess with contextOutput as contextFinal result

How Context Works

  1. Agent Receives Input

    First agent receives the user’s task and any initial context.

  2. Output Becomes Context

    Agent’s output is automatically passed as context to the next agent via {{previous_output}}.

  3. Context Manager Optimizes

    The Context Manager handles token limits, deduplication, and summarization.

  4. Session Ends

    When workflow completes, all context is discarded.

Context Configuration

yaml
# In agents.yaml or recipe
context:
  enabled: true
  auto_compact: true        # Auto-compress when near limit
  compact_threshold: 0.8    # Compress at 80% capacity
  strategy: smart           # smart, aggressive, conservative
  llm_summarize: true       # Use LLM for smart summarization
  max_tool_output_tokens: 5000

Context Code Example

python
from praisonaiagents import Agent, AgentTeam

# Agents share context automatically within a session
researcher = Agent(
    name="Researcher",
    instructions="Research the topic and provide findings"
)

writer = Agent(
    name="Writer", 
    instructions="Write an article based on the research"
)

# Context flows: User → Researcher → Writer → Output
team = AgentTeam(
    agents=[researcher, writer],
    process="sequential"
)

result = team.start("Write about AI trends")
# Context is lost after this completes

Memory: Persistent Knowledge

Memory allows agents to store and recall information across sessions. Unlike context, memory persists to disk and can be accessed by any agent at any time.

Diagram

Storage

Session 2 (Later)

Session 1

Learn

Important

Recall

New learning

Agent

Short-term Memory

Long-term Memory

Agent

Short-term Memory

Persistent Store

Memory Types

Memory Code Example

python
from praisonaiagents import Agent

# Enable memory - persists across sessions
agent = Agent(
    name="Assistant",
    instructions="Help the user and remember their preferences",
    memory=True  # Uses FileMemory (zero dependencies)
)

# Session 1: Learn user preference
agent.start("I prefer dark mode")
# Memory stored in .praison/memory/

# Session 2 (later): Recall preference
agent.start("What are my preferences?")
# Agent recalls: "User prefers dark mode"

Memory Configuration

python
from praisonaiagents import Agent

# Advanced memory configuration
agent = Agent(
    name="Assistant",
    memory={
        "provider": "file",      # file, sqlite, chromadb, mem0
        "user_id": "user123",    # Isolate memory per user
        "config": {
            "short_term_limit": 100,
            "long_term_limit": 1000,
            "auto_promote": True,
            "importance_threshold": 0.7
        }
    }
)

When to Use Each

Diagram

No

Yes

No

Yes

Need data after
session ends?

Use Context

Cross-session
learning?

Use Memory

✓ Fast execution

✓ No dependencies

✓ Auto-cleanup

✓ Persistent knowledge

✓ User preferences

✓ Learning over time

Use Context When:

  • Single workflow execution – Data only needed during current run
  • Agent-to-agent handoffs – Passing results between sequential agents
  • Performance critical – No disk I/O overhead
  • Stateless operations – Each run is independent

Use Memory When:

  • User preferences – Remember settings across sessions
  • Learning systems – Build knowledge over time
  • Conversation history – Multi-turn interactions
  • Entity tracking – Track people, places, concepts

Using Both Together

The most powerful pattern combines both: Context for workflow data flow + Memory for persistent learning.

python
from praisonaiagents import Agent, AgentTeam

# Agent with both context flow AND persistent memory
researcher = Agent(
    name="Researcher",
    instructions="Research topics. Remember what you've researched before.",
    memory=True  # Persistent memory
)

writer = Agent(
    name="Writer",
    instructions="Write articles. Remember user's style preferences.",
    memory=True  # Persistent memory
)

# Sequential workflow with context passing
team = AgentTeam(
    agents=[researcher, writer],
    process="sequential"  # Context flows between agents
)

# Run 1: Research and write, learn preferences
team.start("Write about AI in formal style")

# Run 2: Memory recalls style preference
team.start("Write about robotics")
# Writer remembers: "User prefers formal style"
Diagram

Persistent (Memory)

Workflow (Context)

context

store

store

recall

recall

Researcher

Writer

Research Memory

Style Memory


Performance Comparison

OperationContextMemory (File)Memory (ChromaDB)
Read~0ms~1-5ms~10-50ms
Write~0ms~5-10ms~50-100ms
SearchN/A~10ms~20-100ms
DependenciesNoneNonechromadb

Summary

Rule of thumb: Start with context (default). Add memory only when you need cross-session persistence.