Building Memory v3: Adding Long‑Term Memory to Your Go AI Agent
This guide walks you through creating a Memory v3 module for a Go‑based AI agent, enabling long‑term storage of preferences, tasks, and context so the agent can recall and leverage past interactions for more personalized responses.
Introduction
Memory v3 upgrades an AI agent by providing true long‑term memory, allowing the system to remember user preferences, ongoing tasks, and conversation context. Unlike single‑turn Q&A tools, the agent can accumulate experience and act as a collaborative partner.
Memory Interface (MemoryInterface)
type MemoryMessage struct {
ID string `json:"id"`
Content string `json:"content"`
Type string `json:"type"`
Created int64 `json:"created"`
}
type MemoryInterface interface {
SaveMemory(msg MemoryMessage) error
SearchMemory(query string, limit int) ([]MemoryMessage, error)
DeleteMemory(id string) error
ListMemories(limit int) ([]MemoryMessage, error)
}The interface supports full‑text search, multiple memory types (preferences, tasks, context), and complete CRUD operations.
SimpleMemoryAdapter Implementation
type SimpleMemoryAdapter struct {
mu sync.RWMutex
memories []MemoryMessage
}
func (m *SimpleMemoryAdapter) SaveMemory(msg MemoryMessage) error { /* ... */ }
func (m *SimpleMemoryAdapter) SearchMemory(query string, limit int) ([]MemoryMessage, error) { /* ... */ }
func (m *SimpleMemoryAdapter) DeleteMemory(id string) error { /* ... */ }
func (m *SimpleMemoryAdapter) ListMemories(limit int) ([]MemoryMessage, error) { /* ... */ }This in‑memory adapter stores messages for fast access, supports fuzzy queries for easy history lookup, and can later be swapped for SQLite or a vector database.
Using Memory in an Agent
agent := &Agent{
Memory: NewSimpleMemoryAdapter(),
}Saving a user preference:
agent.Memory.SaveMemory(MemoryMessage{
ID: "001",
Content: "User prefers fsnotify for file monitoring",
Type: "user_pref",
Created: time.Now().Unix(),
})Querying recent memories:
mems, _ := agent.Memory.SearchMemory("fsnotify", 5)The agent can now reference these memories to produce responses that align with the user's habits.
Conclusion and Roadmap
Memory v3 transforms the AI agent from an instant tool into a long‑term collaborator, enabling:
Accumulated experience
Understanding of user preferences
Proactive use of historical information in future answers
Future versions are planned:
Memory v3.5 – vector‑based memory store
Memory v4 – multi‑level memory hierarchy
Memory v5 – reasoning‑chain memory
References
Source code repository:
https://github.com/louis-xie-programmer/easy-agent
Gitee mirror:
https://gitee.com/louis_xie/easy-agent
Code Wrench
Focuses on code debugging, performance optimization, and real-world engineering, sharing efficient development tips and pitfall guides. We break down technical challenges in a down-to-earth style, helping you craft handy tools so every line of code becomes a problem‑solving weapon. 🔧💻
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
