Chapter 13 — Agent Memory
TL;DR: Memory allows an agent to remember who you are, what you said before, and your patterns — without storing everything in the context window.
Three layers of memory
Section titled “Three layers of memory”- Short-term — current conversation. In the prompt each turn.
- Long-term — structured data about the user (preferences, history).
- Episodic — summary of past conversations, indexed by embedding.
interface AgentMemory { shortTerm: Message[]; // current conversation longTerm: UserProfile; // structured data episodic: EpisodeVectors[]; // summaries from past}
async function generateWithMemory(userId: string, newMessage: string) { const shortTerm = await getConversation(userId, limit: 5); const longTerm = await getUserProfile(userId); const relevant = await semanticSearch( `user: ${userId} past: ${newMessage}` );
return await llm.generate({ messages: [...shortTerm, {role: "user", content: newMessage}], system: `Profile: ${JSON.stringify(longTerm)}\nContext: ${relevant}` });}