Agent Memory
Agents in Swarms use theConversation class to manage short-term memory and conversation history. This enables context-aware responses and multi-turn interactions.
Memory Architecture
Each agent maintains:- Short-term memory - Current conversation history
- Long-term memory (optional) - RAG-based knowledge retrieval
- System prompt - Persistent instructions
Short-Term Memory
Short-term memory stores the conversation history for the current session.Automatic Memory Management
Memory Configuration
Name used for user messages in conversation history.
Return full conversation history instead of just final response.
Maximum context window size in tokens.
Automatically manage context to prevent overflow.
Accessing Memory
View Conversation History
Get Specific Messages
Memory Persistence
Auto-save Conversations
Manual Save and Load
Configure Save Location
Context Window Management
Dynamic Context Truncation
When conversations exceed the context window, Swarms automatically manages the history:Manual Truncation
Long-Term Memory (RAG)
For knowledge retrieval across sessions, use long-term memory with a vector database.Basic RAG Setup
RAG Configuration
Vector database for long-term knowledge retrieval.
Query RAG on every loop iteration vs. once at start.
Size of memory chunks for RAG retrieval.
RAG Query Examples
Memory Search
Search Conversation History
Message Metadata
Enable Timestamps and IDs
Access Message Metadata
Advanced Memory Patterns
Multi-Turn Conversation
Conversation Export
Clear and Reset
Best Practices
1. Use Dynamic Context Window
2. Enable Autosave for Important Conversations
3. Use RAG for Knowledge-Intensive Tasks
4. Clear Memory Between Unrelated Tasks
Next Steps
Agent Tools
Add tools to extend agent capabilities
Structured Outputs
Get structured responses from agents
Reference
- Conversation class:
swarms/structs/conversation.py:51 - Agent memory initialization:
swarms/structs/agent.py:1060-1082