This example demonstrates how to wrap any agent with long-term memory for multi-turn conversations with context awareness.
The Conversational Agent pattern adds semantic memory to any agent (ReAct, RAG, Supervisor, etc.) enabling:
- Context recall: Automatically recalls relevant past messages before the agent runs
- Conversation storage: Stores the exchange after each interaction
- Session isolation: Each user/session has its own memory context
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Conversational Agent β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β START β Memory Recall β Wrapped Agent β Memory Store β END β
β β β
β ReAct/RAG/ β
β Supervisor/etc. β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export OPENAI_API_KEY=your-key-here
go run main.goEach conversation needs a session ID to isolate memory:
chatAgent.Run(ctx, messages,
graph.WithInitialValue(agent.SessionIDKey, "user-123-session"),
)The conversational agent uses a dual-memory approach combining:
- Short-term memory: Recent N messages (recency-based)
- Long-term memory: Semantically similar messages from history (relevance-based)
agent.NewConversational(reactAgent, mem,
agent.WithShortTermMessages(5), // Last 5 messages for immediate context
agent.WithLongTermMessages(5), // 5 semantically similar messages
agent.WithMinSimilarityScore(0.5), // Threshold for long-term recall
agent.WithFailOnStoreError(false), // Don't fail if storage fails
)| Option | Description | Default |
|---|---|---|
WithShortTermMessages(n) |
Number of recent messages to always include | 5 |
WithLongTermMessages(n) |
Number of semantically similar messages | 5 |
WithMinSimilarityScore(s) |
Minimum similarity for long-term recall | 0.5 |
WithFailOnStoreError(b) |
Fail if memory storage fails | false |
- VectorMemory: Semantic search using embeddings (recommended for production)
- SimpleMemory: Basic FIFO storage without semantic search (good for testing)
=== Turn 1: Setting up context ===
Agent: Nice to meet you, Alice! San Francisco is a wonderful city...
=== Turn 2: Using tool ===
Agent: Let me check the weather in San Francisco for you...
Agent: The weather in San Francisco is currently sunny at 72Β°F.
=== Turn 3: Recalling from memory ===
Agent: Your name is Alice and you live in San Francisco.
=== Conversation complete! ===
The agent remembered context from earlier turns using semantic memory.
- Memory Guide - Understanding memory types
- Agents Documentation - Full API reference