Skip to content

Chatbot Memory

“Remember the last 5 minutes vividly, the last hour hazily, and yesterday as a blur.”

Traditional chatbot implementations face a dilemma:

  • Keep everything: Context window explodes, costs rise, hallucinations increase
  • Hard truncation: Arbitrary cutoff loses important context abruptly
  • Sliding window: Equal weight to stale and fresh messages

What we actually want is biological memory: recent context is vivid, older context fades naturally, but important moments persist.

graph TD
    subgraph "Conversation Flow"
        M1[msg_001<br/>E=0.2] --> M2[msg_002<br/>E=0.4]
        M2 --> M3[msg_003<br/>E=0.6]
        M3 --> M4[msg_004<br/>E=0.8]
        M4 --> M5[msg_005<br/>E=1.0]
    end

    subgraph Legend
        L1[Older = Lower Energy]
        L2[Younger = Higher Energy]
    end

Key insights:

  • Each message is a lineage with initial energy based on recency
  • Synergy bonds chain messages sequentially
  • Decay naturally fades old messages
  • Semantic links can bypass the chain for important associations
import { MindFry } from '@mindfry/client'
class ChatbotMemory {
private brain: MindFry
private lastMessageKey: string | null = null
constructor(brain: MindFry) {
this.brain = brain
}
async addMessage(role: 'user' | 'bot', content: string) {
const key = `msg_${Date.now()}_${role}`
// Create memory with high initial energy
await this.brain.lineage.create({
key,
energy: 1.0,
// Metadata stored as tags (hypothetical extension)
// tags: { role, content: content.slice(0, 100) }
})
// Chain to previous message (synergy)
if (this.lastMessageKey) {
await this.brain.bond.connect({
from: this.lastMessageKey,
to: key,
strength: 0.8,
polarity: 1, // Synergy
})
}
this.lastMessageKey = key
}
async getRelevantContext(triggerWord: string): Promise<string[]> {
// Stimulate the trigger and let propagation find related memories
await this.brain.lineage.stimulate({
key: triggerWord,
delta: 0.5,
})
// Get all messages above consciousness threshold
const stats = await this.brain.system.stats()
// In a full implementation, you'd query for high-energy lineages
// For now, return recent high-energy memories
return [] // Placeholder
}
async triggerDecay() {
// MindFry handles decay automatically based on access patterns
// This method could force a decay cycle if needed
// await this.brain.system.tick(); // Hypothetical
}
}
// Usage
const brain = new MindFry({ host: 'localhost', port: 9527 })
await brain.connect()
const memory = new ChatbotMemory(brain)
await memory.addMessage('user', 'Hello, I need help with my order')
await memory.addMessage('bot', 'Sure! What is your order number?')
await memory.addMessage('user', 'Order #12345')
await memory.addMessage('bot', 'I found order #12345. It shipped yesterday.')
// Hours later... older messages have decayed
// Only "Order #12345" and recent context remain vivid

Create cross-chain bonds for semantic associations:

// User mentions 'refund' which relates to earlier 'complaint'
await brain.bond.connect({
from: 'msg_complaint',
to: 'msg_refund_request',
strength: 0.9,
polarity: 1, // Strong synergy
})
// Now stimulating 'refund' also surfaces 'complaint' context

Adjust how much context surfaces based on “mood”:

// Euphoric: Return more context (lower threshold)
await brain.system.setMood(0.9)
// Focused/Depressive: Return only most relevant (higher threshold)
await brain.system.setMood(0.3)

Next Recipe: PTSD Simulator — Model trauma responses with antagonistic bonds.