Memory Stream diagramObservation, Rater, Memory stream, Reflector, Retriever, Agent, Action. Step captions follow the diagram.ObservationRaterimportance 1 to 10Memory streamReflectorRetrieverscore: rec + imp + relAgentActionMemory Stream diagramObservation, Rater, Memory stream, Reflector, Retriever, Agent, Action. Step captions follow the diagram.ObservationRaterimportance1 to 10MemorystreamReflectorRetrieverscore: rec+ imp + relAgentAction

All steps

Remember what matters

Reflect

Parts of this pattern
Observation Input / output
What the agent perceives or is asked, such as Isabella inviting Maria to a party, or someone asking Maria about tonight.
Rater LLM call
Rates how important each new observation is, from 1 (mundane) to 10 (poignant).
Memory stream Memory
One flat, timestamped log of every observation and reflection, each with its importance score and embedding.
Reflector LLM call
When enough important things have happened, asks 3 questions about recent memories, retrieves evidence for each, and writes insights back into the stream.
Retriever Evaluator
Scores every memory for the current query by recency, importance and relevance, and returns the top few.
Agent LLM call
Decides what to say or do from the current situation and the memories retrieved for it.
Action Input / output
What the agent says or does next, consistent with what it remembers.
Observation → Rater
Each new observation is sent to be rated before it is stored.Isabella invited me to her Valentine's Day party at Hobbs Cafe, Feb 14, 5pm.
Rater → Memory streamrated memory
The observation is appended to the stream with its timestamp and importance score.{"text": "Isabella invited me to her Valentine's Day party at Hobbs Cafe, Feb 14, 5pm", "t": "Feb 13 09:12", "importance": 7}
Observation → Retriever
The current situation becomes the retrieval query.Klaus asks Maria: 'What are you doing tonight?'
Memory stream → Retriever
Every memory is read with its timestamp, importance and embedding, so it can be scored.412 memories, e.g. 'Ate breakfast' (t=08:01, imp=1), 'Isabella invited me to a party' (t=Feb 13, imp=7)
Retriever → Agenttop 5
The highest-scoring memories, ranked by the sum of the three scores.1. Isabella invited me to her party at 5pm (rec 0.62 + imp 0.70 + rel 0.91 = 2.23) 2. I want to ask Klaus to the party (2.05) ...
Agent → Action
The agent's next action or reply."I'm going to Isabella's Valentine's Day party at Hobbs Cafe at 5. Want to come?"
Memory stream → Reflectorsum > 150
When importance passes the threshold, the 100 most recent memories go to the reflector; then, for each of its 3 questions, the memories retrieved from the whole stream.Q: How does Maria feel about Klaus? -> 12. Maria studied with Klaus at the library 31. Maria laughed at Klaus's joke 47. Maria wants to ask Klaus to the party
Reflector → Memory stream
Each insight is written back as a new memory, pointing to the memories it rests on.{"text": "Maria wants to spend more time with Klaus", "importance": 8, "evidence": [12, 31, 47]}

← → neighboursesc atlas

Knowledge & Memory

Memory Stream

Also known as Generative Agents memory · Observation-reflection memory · Recency-importance-relevance retrieval

Log every observation with an importance score, retrieve by recency, importance and relevance, and periodically write reflections back into the stream.

Introduced by Park et al. (2023) · Stanford University, Google Research and others

Published at UIST 2023. Twenty-five agents in a simulated town use the memory stream to plan days, form relationships and, unprompted, spread invitations to a Valentine's Day party.

Measured over one run

Nodes
7. Boxes in the diagram: models, tools, stores, people and plain code.
LLM calls
3. Model invocations in the first scenario: each time a model or agent node starts thinking, plus each branch it decides.
Tool calls
0. Messages sent to a tool node in the first scenario.
Steps
4. Steps in the first scenario, with repeated steps counted every time they run.
Scenarios
2. Distinct example runs you can switch between on the diagram.
Loop
yes. Whether the flow cycles back on itself until a condition is met.
Parallel
no. Whether two or more messages are in flight at the same time.
Branch
no. Whether the flow takes one path out of several, chosen at run time.
Human in the loop
no. Whether a person appears as a node, to approve, correct or answer.
Autonomy
high. How much the model decides the control flow itself, from fixed code paths (low) to open-ended (high).
Cost
medium. Relative token spend per request.
Latency
medium. Relative wall-clock time per request.

Counted from the first scenario's timeline when the site is built.

Problem

A long-lived agent sees far more than it can keep in its prompt. A simulated townsperson that has lived for two days has hundreds of memories: breakfast, a walk, a chat about the weather, and one invitation to a party on Valentine’s Day. When someone asks “what are you doing tonight?”, the agent needs the invitation, not breakfast. Keeping the latest memories forgets the invitation; similarity search alone may surface a chat about evening plans from last week. And none of the raw memories says what they add up to.

How it works

Every observation goes to a rater model that scores how important it is, from 1 (brushing teeth) to 10 (a break-up). The observation is appended to the memory stream, a single flat log with a timestamp, the score and an embedding. Isabella’s invitation to her party scores 7; breakfast scores 1.

When the agent needs to act, the current situation becomes a query, and the retriever scores every memory on three things. Recency decays exponentially with the hours since the memory was last used. Importance is the stored score. Relevance is the similarity between the memory and the query. The three are normalized and added, and the top few go to the agent. The invitation is older than breakfast, but it wins easily on importance and relevance, so Maria’s reply is “I’m going to Isabella’s party at 5. Want to come?”

The third part is reflection. The agent keeps a running total of the importance of new memories, and when it passes a threshold (150 in the paper), a reflector reads the 100 most recent memories and asks three questions about them, such as “how does Maria feel about Klaus?”. Each question goes through the retriever as a query over the whole stream, so the evidence can include old memories and earlier reflections. From those memories the reflector writes insights, such as “Maria wants to spend more time with Klaus”, back into the stream with pointers to the memories behind them. (The paper’s own example is Klaus concluding that he is dedicated to his research on gentrification.) Reflections are retrieved like any other memory, and later reflections can build on earlier ones, so the agent forms a picture of its world instead of a pile of events.

When to use

  • Long-lived agents or characters need believable continuity across days or weeks of events.
  • What matters is uneven: most events are trivial, a few are decisive, and the agent must tell them apart.
  • You want the agent to form opinions and relationships from experience, not only recall facts.

When to avoid

  • You need exact, structured recall, such as account numbers or the user’s stated preferences. Use a fact store or hierarchical memory.
  • The agent lives for one task or one conversation; the context window is enough.
  • Every model call matters for cost. Each observation costs a rating call, and reflections add more.

Trade-offs

Each observation costs one small rating call and an embedding, and retrieval scores the whole stream, which stays cheap until the stream grows very large. Reflection is the expensive part, but it runs only when enough important things have happened. In exchange, the agent’s behaviour stays consistent with what matters in its past, which the paper’s ablations show depends on all three parts: observation, planning and reflection.

Failure modes

  • Miscalibrated importance: the rater scores everything 5, and the retriever falls back to recency and relevance alone. Give the rater anchored examples at both ends of the scale.
  • Runaway reflection: reflections about reflections drift away from what happened. Keep evidence pointers and weight insights by the observations they cite.
  • Stale favourites: retrieving a memory refreshes its recency, so a few memories keep winning. Decay from the original timestamp as well, or cap how often one memory is returned.
  • Fabricated insight: the reflector infers something the memories do not support. Require citations and drop insights whose evidence is thin.

Build it with your AI

Paste this into Claude Code, Cursor, ChatGPT or any AI coding assistant.

Implement the Memory Stream pattern in this codebase, using the LLM client and language we already use.

## Goal

Give a long-lived agent a memory that logs every observation with an importance score, retrieves the memories that best combine recency, importance and relevance, and periodically distils them into reflections.

## How it works

1. When the agent observes something, call the rater model with the Importance rating prompt to score its importance from 1 to 10, and append it to the stream with its text, timestamp, last-access time, score and embedding.
2. To retrieve for a query, score every memory as the sum of recency (exponential decay since last access, factor 0.995 per hour), importance and relevance (cosine similarity to the query embedding), each normalized to 0..1.
3. Return the top k memories (default 5), update their last-access time, and pass them to the agent model with the current situation.
4. Keep a running total of importance since the last reflection. When it exceeds the threshold (default 150), reflect.
5. To reflect, call the reflector model with the Reflection questions prompt on the 100 most recent memories. Run each of the 3 questions it returns through the retriever as a query over the whole stream, then call the reflector with the Reflection insights prompt on the retrieved memories, numbered, and append each insight to the stream with pointers to the memories it cites.

## Prompts

### Importance rating prompt

```markdown
On a scale of 1 to 10, where 1 is purely mundane (such as brushing teeth
or making the bed) and 10 is extremely poignant (such as a break-up or a
college acceptance), rate the likely poignancy of the following memory
for {{agent_name}}.

<memory>{{observation}}</memory>

Reply with a single integer.
```

### Reflection questions prompt

```markdown
Here are {{agent_name}}'s most recent memories:

<memories>{{recent_memories}}</memories>

Given only the information above, what are the 3 most salient high-level
questions we can answer about the subjects in the statements?
Return one question per line, with no numbering.
```

### Reflection insights prompt

```markdown
Statements about {{agent_name}}, retrieved for the questions
{{questions}}:

<statements>{{numbered_retrieved_memories}}</statements>

What 5 high-level insights can you infer from the above statements?
Cite the statements each one rests on, in the form:
insight (because of 1, 5, 3)
```

### Agent prompt with retrieved memories

```markdown
You are {{agent_name}}. {{agent_summary}}

It is {{time}}. {{situation}}

Relevant memories, most relevant first:
<memories>{{retrieved_memories}}</memories>

What do you do or say next? Stay consistent with your memories.
```

## Stop rule

Retrieval and reflection never end the agent's run; each retrieval stops at k memories, and each reflection at one questions call, one retrieval per question and one insights call. The importance total resets to 0 after every reflection.

## Requirements

- Store reflections in the same stream as observations, so later reflections can build on earlier ones.
- Make the three weights (default 1 each), the decay factor, k and the reflection threshold configurable.
- If the rater's reply is not an integer from 1 to 10, retry once, then store the memory with importance 5 and log it.
- Keep every reflection's evidence pointers, so an insight can be traced to the observations behind it.
- Add a test with a fake clock, a fake embedder and stub importance scores in which an old but important, relevant memory beats a recent trivial one; assert it ranks first, and that crossing the threshold triggers one questions call, exactly 3 retriever calls (one per question) and one insights call.

Prompt templates

Importance rating prompt
On a scale of 1 to 10, where 1 is purely mundane (such as brushing teeth
or making the bed) and 10 is extremely poignant (such as a break-up or a
college acceptance), rate the likely poignancy of the following memory
for {{agent_name}}.
<memory>{{observation}}</memory>
Reply with a single integer.
Reflection questions prompt
Here are {{agent_name}}'s most recent memories:
<memories>{{recent_memories}}</memories>
Given only the information above, what are the 3 most salient high-level
questions we can answer about the subjects in the statements?
Return one question per line, with no numbering.
Reflection insights prompt
Statements about {{agent_name}}, retrieved for the questions
{{questions}}:
<statements>{{numbered_retrieved_memories}}</statements>
What 5 high-level insights can you infer from the above statements?
Cite the statements each one rests on, in the form:
insight (because of 1, 5, 3)
Agent prompt with retrieved memories
You are {{agent_name}}. {{agent_summary}}
It is {{time}}. {{situation}}
Relevant memories, most relevant first:
<memories>{{retrieved_memories}}</memories>
What do you do or say next? Stay consistent with your memories.

Related patterns

Composes well with:

Sources

  1. Generative Agents: Interactive Simulacra of Human Behavior (Park et al., 2023)

From the Agentic Atlas by Joran Kikke · Edit on GitHub · Suggest a correction