Advanced Memory Systems
Go beyond simple chat history. Implement long-term persistence, vector retrieval, and episodic memory.
Memory Types#
Short-Term
The immediate context window. Stores recent messages. Cleared when session ends.
Long-Term (Vector)
Semantic search over documents. "Read-only" knowledge base for the agent.
Episodic
Remembering past interactions with this specific user. "You mentioned you like Python last week."
Implementing Persistent Memory#
By default, `InMemoryStore` forgets everything on restart. To build a real app, use a database adapter (e.g., Redis, Postgres).
redis-memory.ts
import { MemoryStore, Message } from '@akios/sdk'
import { Redis } from 'ioredis'
export class RedisMemory implements MemoryStore {
private client: Redis
constructor(url: string) {
this.client = new Redis(url)
}
async getMessages(sessionId: string): Promise<Message[]> {
const data = await this.client.lrange(`session:${sessionId}`, 0, -1)
return data.map(d => JSON.parse(d))
}
async addMessage(sessionId: string, message: Message): Promise<void> {
await this.client.rpush(`session:${sessionId}`, JSON.stringify(message))
// Optional: Set TTL to expire old sessions
await this.client.expire(`session:${sessionId}`, 86400 * 7)
}
async clear(sessionId: string): Promise<void> {
await this.client.del(`session:${sessionId}`)
}
}Episodic Memory (The 'Profile' Pattern)#
To make agents feel personal, they need to update a "User Profile" based on conversations. This is often done with a background "Summarizer" agent.
Architecture Pattern
Don't stuff every past conversation into the context window. Instead, have a background job summarize key facts into a `user_profile` table, and inject that profile into the System Prompt.
profile-injection.ts
// 1. Fetch Profile
const profile = await db.users.find(userId)
// 2. Inject into System Prompt
const agent = new Agent({
systemPrompt: `You are a helpful assistant.
User Profile:
- Name: ${profile.name}
- Technical Level: ${profile.techLevel}
- Preferred Language: ${profile.language}
`,
// ...
})