Forge
1.x
Docs/Forge/Memory
Open

Reading3 min
Updated5 Aug 2026
Sourcev1/ai-sdk/memory.mdx

The MemoryManager provides a four-tier memory system inspired by human cognition. Memories are stored with importance scores, automatically consolidated between tiers, and recalled via semantic search.

Memory Tiers01

TierRetentionPurpose
WorkingCurrent sessionActive context and recent interactions
Short-termHours to daysRecent interactions consolidated from working memory
Long-termPersistentImportant facts and learned patterns
EpisodicPersistentEvent-based memories with temporal context

Creating a Memory Manager02

import sdk "github.com/xraph/ai-sdk"

memoryManager := sdk.NewMemoryManager(
    stateStore,     // For persistence
    vectorStore,    // For semantic search
    logger,
    metrics,
    &sdk.MemoryOptions{
        WorkingMemoryLimit:      100,
        ShortTermRetention:      24 * time.Hour,
        ConsolidationInterval:   10 * time.Minute,
        ImportanceThreshold:     0.5,
        SemanticSearchLimit:     10,
    },
)

Storing Memories03

entry, err := memoryManager.Store(ctx, "The user prefers Go over Python", 0.8)
// importance: 0.0 (trivial) to 1.0 (critical)

Recalling Memories04

Recall by semantic similarity within a specific tier:

memories, err := memoryManager.Recall(ctx, "programming language preferences", sdk.MemoryTierLongTerm, 5)
for _, m := range memories {
    fmt.Printf("[%.2f] %s\n", m.Importance, m.Content)
}

Promoting Memories05

Move a memory to a higher tier:

// Promote from working to short-term, or short-term to long-term
err := memoryManager.Promote(ctx, memoryID)

Automatic Consolidation06

The memory manager periodically consolidates memories:

  1. Working to Short-term -- after the consolidation interval, working memories above the importance threshold are promoted.

  2. Short-term to Long-term -- memories that are frequently accessed or have high importance are promoted.

This runs automatically based on ConsolidationInterval.

Episodic Memory07

Episodic memories record events with temporal context:

episode := &sdk.EpisodicMemory{
    Event:       "User completed onboarding",
    Context:     "First session, asked about Go channels",
    Outcome:     "Successfully explained channels with examples",
    Timestamp:   time.Now(),
    Importance:  0.9,
    Participants: []string{"user", "assistant"},
}

err := memoryManager.StoreEpisode(ctx, episode)

Recall episodes by semantic similarity:

episodes, err := memoryManager.RecallEpisodes(ctx, "onboarding experience", 5)

Memory Entry08

type MemoryEntry struct {
    ID         string
    Content    string
    Tier       MemoryTier
    Importance float64
    CreatedAt  time.Time
    AccessedAt time.Time
    AccessCount int
    Metadata   map[string]any
    Embedding  []float64
}

Using Memory with Agents09

agent, _ := sdk.NewReactAgentBuilder("assistant").
    WithLLMManager(llmManager).
    WithMemoryManager(memoryManager).
    WithTools(tools...).
    Build()

The agent automatically:

  • Recalls relevant memories before reasoning

  • Stores important observations from tool results

  • Builds context from episodic memories for multi-turn conversations

Configuration10

OptionDefaultDescription
WorkingMemoryLimit100Max entries in working memory
ShortTermRetention24hHow long short-term memories are kept
ConsolidationInterval10mHow often to run consolidation
ImportanceThreshold0.5Min importance for promotion
SemanticSearchLimit10Max results from semantic recall