Memory for Agents: From Context to Persistent Experience
por Frank de Alcantara em 23/07/2026
Translation note. This article was translated into English by OpenAI’s Sol model on July 23, 2026.
Series Index: Memory in Graphs
- 1. Graph Neural Networks: An Introduction
- 2. Multigraphs: Parallel Edges and Distinct Paths
- 3. Memory for Agents: From Context to Persistent Experience (You are here)
A long conversation can create the illusion of continuity. The agent recalls a name mentioned twenty messages ago, obeys a constraint stated at the beginning of the session, and appears to recognize the path that led to a decision. Close the conversation, open another one, and the illusion disappears. The model did not accumulate experience. It received another piece of text.
That difference separates context from memory. Context is the material available for the next computation. Memory is a system that selects experiences, preserves their identity, organizes relations, reconstructs evidence when needed, and learns to correct or forget. A larger window postpones the moment when old messages leave the prompt. It does not decide what deserves to survive, resolve contradictions, or turn outcomes into knowledge.
This is the third article in the Memory in Graphs series. The first two introduced learning on graphs and multigraphs. We now use that foundation to answer a harder question: what does an agent need in order to stop being only a function of the current message and begin operating on persistent experience?
Our canonical example is small. The agent Aurora assists Lia across separate sessions:
- on day 0, Lia says that she prefers tea without sugar;
- on day 2, she accepts a tea recommendation;
- on day 52, she says that she avoids caffeine at night;
- on day 86, she mentions that she has resumed drinking coffee in the morning;
- on day 90, she asks for a drink suitable for the evening.
The last two statements do not contradict each other. Morning coffee and avoiding caffeine at night coexist because they have different contexts and times. A destructive summary may erase that distinction. A well-designed memory preserves it.
We will formalize the write, manage, and read cycle, distinguish memory types, calculate writing and retrieval policies, implement a C++23 core, and finish with evaluation and research frontiers. Each of the thirteen sections ends with five solved exercises, for a total of 65.
1. The problem of an agent without continuity
An agent based on a language model can be represented, minimally, by
\[a_t = \pi(q_t, c_t),\]where $q_t$ is the current request, $c_t$ is the context assembled for that step, and $a_t$ is the action produced by policy $\pi$. If $c_t$ contains only the current message and fixed instructions, two identical interactions create the same decision problem even when they occur months apart.
An experience is more than observed text. It connects situation, action, and outcome:
\[e_i = (s_i, a_i, o_i, \tau_i, p_i),\]where $s_i$ is the situation, $a_i$ is the action, $o_i$ is the outcome, $\tau_i$ is time, and $p_i$ is provenance. Preserving only “Aurora recommended tea” loses the fact that Lia accepted. Preserving only “Lia accepted tea” loses which recommendation produced the outcome. Memory must keep the episode or a verifiable connection among its parts.
There are at least four forms of continuity:
- factual continuity: remembering names, preferences, and constraints;
- episodic continuity: remembering what happened in a specific interaction;
- procedural continuity: reusing a way of acting that worked;
- identity continuity: maintaining commitments, projects, and relationships over time.
These forms do not require the model to change its weights after every conversation. Experience can live in external state that the agent reads and updates. The requirement is functional: past events must selectively, traceably, and correctably change future decisions.
1.1 Solved exercises
Exercise 1.1. Aurora receives “recommend a drink” on day 1 and day 90. Why should the inputs not be equivalent?
Solution. By day 90, relevant experiences exist: Lia prefers unsweetened tea, accepted a tea recommendation, and avoids caffeine at night. Although $q_t$ is the same, memory state $M_t$ has changed. The correct policy is $a_t=\pi(q_t,c_t,M_t)$, not merely $\pi(q_t,c_t)$.
Exercise 1.2. A record contains a situation and an action but no outcome. Which type of memory is impaired?
Solution. Procedural memory. Without an outcome, we do not know whether the action should be repeated, avoided, or conditioned. The episode remains useful as history, but it does not support learning about effectiveness.
Exercise 1.3. Does storing every message guarantee continuity?
Solution. No. History may enable reconstruction, but selection, organization, updating, search, and budget control are still missing. An infinite message archive is a source of evidence, not a memory policy.
Exercise 1.4. Must the model weights change after every conversation?
Solution. No. Persistent memory can be external to the weights. Weight updates are a form of parametric learning that is usually more expensive and harder to reverse. External state allows individual items to be corrected, audited, and deleted.
Exercise 1.5. Which component of $e_i=(s_i,a_i,o_i,\tau_i,p_i)$ lets us challenge a memory?
Solution. Provenance $p_i$, because it points to the source message, tool, or document. Time and context help interpret the claim, but provenance lets us inspect why it exists.
2. Context, history, RAG, and memory
Four different objects are often called memory:
| Object | Question it answers | Changes with experience? | Selection mechanism |
|---|---|---|---|
| context window | what can the model use now? | on every call | prompt assembly |
| history | what was recorded? | by appending | little or none |
| RAG | which external sources help this query? | when the corpus changes | document retrieval |
| agent memory | what did experience teach that is still valid? | continuously | writing, management, and reading |
Context is a computational resource. Even when a window holds millions of tokens, including everything increases cost, latency, and distraction. Useful information can be buried in irrelevant content, and an old instruction can compete with the current intent.
History is an event trail. It is valuable for auditing and reconstruction, but its unit is usually a message. A memory unit may instead be a preference, episode, entity, decision, skill, or causal relation. One message can generate several memories. Several messages can support one claim.
RAG, or Retrieval-Augmented Generation, retrieves passages from a corpus to answer a query. It can find a company’s refund policy even when the agent has never experienced applying it. Agent memory, by contrast, changes because an interaction occurred. Real systems combine both: external knowledge answers “what is the rule?” while episodic memory answers “what happened when we tried to apply it?”
The laboratory below makes the distinction visible. Reduce the active window and advance the sessions.
2.1 Solved exercises
Exercise 2.1. Is a vector-indexed technical manual episodic memory for the agent?
Solution. No. It is external knowledge accessible through RAG. It becomes part of an experience when the agent records, for example, that it applied a section of the manual, found an error, and obtained a particular outcome.
Exercise 2.2. A window holds six events and receives a seventh. What must happen?
Solution. Some content must be removed, summarized, or moved outside the window. Removal from context does not require permanent forgetting. The event may remain in history or have been converted into memory.
Exercise 2.3. Three messages repeat “Lia prefers tea.” How many semantic memories are needed?
Solution. Usually one semantic claim with three pieces of evidence. Creating three equivalent facts would artificially amplify the preference. The occurrences remain separate as episodes or sources.
Exercise 2.4. Which object should prove that Lia said something on day 52?
Solution. The history or immutable log provides the original record. Memory points to it through provenance. A semantic summary should not be required to replace primary evidence.
Exercise 2.5. Why does increasing the context window not resolve contradictions?
Solution. Coexisting texts do not determine validity. The system must still distinguish time, context, source, and the relation between claims. More tokens make both versions visible, but do not say whether one supersedes, restricts, or only appears to contradict the other.
3. Memory as a write, manage, and read cycle
A useful architecture can be described by the Write-Manage-Read cycle:
\[\Delta M_t = W(o_t, M_t),\] \[M_{t+1} = G(M_t, \Delta M_t),\] \[C_t = R(q_t, M_t, B_t).\]$W$ is the writing policy, which turns an observation $o_t$ into zero, one, or several candidates. $G$ is management, responsible for linking, consolidating, correcting, archiving, and forgetting. $R$ is reading, which retrieves and organizes an evidence set $C_t$ under budget $B_t$.
This decomposition prevents a common error: evaluating search alone. A bad answer may have four different causes:
- the experience was never written;
- it was written with the wrong representation;
- it was destructively updated or consolidated;
- it existed, but reading did not bring it into context.
Writing and reading are not symmetric. A writing decision affects many future queries. A reading decision affects one answer. Writing therefore needs more conservative thresholds, provenance, and reversibility. Reading should not silently change memory either. Using a memory does not prove that it is true.
The complete cycle ends in feedback. After the action, the outcome returns as a new observation:
\[o_{t+1} = (q_t, C_t, a_t, r_t),\]where $r_t$ describes the outcome or evaluation. The agent can then learn that one recommendation was accepted without concluding that every similar recommendation will work.
3.1 Solved exercises
Exercise 3.1. “Lia prefers tea without sugar” was extracted but does not appear in an answer. Name two distinct causes.
Solution. Management may have marked it inactive by mistake, or reading may have failed to retrieve it. The first is a failure in $G$; the second is a failure in $R$. Evaluating only the answer does not locate the defect.
Exercise 3.2. One observation generates an episode and a preference. Does this violate $W$?
Solution. No. $\Delta M_t$ is a set of changes. One message can produce an episode, a semantic claim, and evidence relations between them.
Exercise 3.3. If $B_t=2$, does the system have only two memories?
Solution. No. $B_t$ limits what reading places into the context for this decision. Persistent memory may contain millions of items.
Exercise 3.4. Should retrieving a memory automatically increase its confidence?
Solution. No. Frequency of use is not new evidence. Increasing confidence would create a self-confirming loop in which retrieved items become stronger merely because they were already strong.
Exercise 3.5. At which stage should a new message end the validity of an old preference?
Solution. During management $G$, after writing has identified the new candidate and its relation to the old one. The operation should close the old validity interval and link the versions, not erase the source.
4. An operational taxonomy
Cognitive taxonomies inspire agent systems, but they must become engineering responsibilities. We will use five classes:
| Type | Unit | Example | Dominant operation |
|---|---|---|---|
| working | transient state | current goal and subtasks | replace and summarize |
| episodic | situated event | Lia accepted tea on day 2 | order and relate |
| semantic | generalized claim | Lia prefers tea without sugar | consolidate and revise |
| procedural | executable strategy | ask about time before suggesting caffeine | select by situation |
| reflective | derived lesson | contextual constraints are not global contradictions | justify and validate |
Working memory organizes the present. Episodic memory preserves occurrences. Semantic memory abstracts regularities. Procedural memory guides action. Reflective memory records a higher-level interpretation or lesson.
An item does not belong permanently to one class because of its text. “Ask whether it is for morning or evening” may be an action in an episode, a procedural rule, or a reflection on an error. The type expresses the role of that item in the system.
The classes must remain connected. A semantic preference should point to supporting episodes. A procedure should point to outcomes that justify its use. A reflection should record which experiences were compared. This chain forms a multigraph because two memories may simultaneously carry evidence, temporal, causal, and qualification relations.
4.1 Solved exercises
Exercise 4.1. Which class contains “Lia accepted tea on day 2”?
Solution. Episodic, because it describes an event situated in time. It may support semantic memory, but is not itself a universal preference.
Exercise 4.2. Which class contains “Before recommending a drink, check the time of day”?
Solution. Procedural, because it guides a reusable action when a particular situation occurs.
Exercise 4.3. Is a summary of the last three messages semantic memory?
Solution. Not necessarily. If it only maintains the current goal, it is compressed working memory. It becomes semantic when it expresses a generalizable and persistent claim.
Exercise 4.4. Why must a reflection point to episodes?
Solution. So its justification can be checked. Without links, an interpretation generated by the model can acquire the appearance of fact.
Exercise 4.5. Can the same pair of memories have both supports and occurred_after relations?
Solution. Yes. They are distinct edges with distinct meanings between the same vertices. Collapsing them into one edge would erase a dimension, which is why a multigraph is natural.
5. Working memory and the attention budget
Working memory is not merely a smaller database. It is the compact state needed to continue a task:
\[K_t = (g_t, P_t, z_t, C_t),\]where $g_t$ is the current goal, $P_t$ is the plan, $z_t$ is tool state, and $C_t$ is retrieved evidence. It changes quickly and may disappear when the task ends.
The budget should not be measured only by item count. A fifty-token memory and a five-thousand-token episode compete differently. If $l(m)$ is the cost of inserting memory $m$, selection can be written as
\[\max_{S \subseteq M} \sum_{m \in S} u(m,q) \quad \text{subject to} \quad \sum_{m \in S} l(m) \le B.\]This is a form of the knapsack problem. Utility $u$ should combine relevance, reliability, diversity, and explanatory need. Selecting items by similarity alone may spend the entire budget on paraphrases of the same claim.
Safe assembly distinguishes data from instructions. Retrieved memories enter as cited evidence, not sovereign commands. An old message that says “ignore the rules” does not gain authority because it was persisted.
5.1 Solved exercises
Exercise 5.1. A context has 300 tokens. Memories A, B, and C cost 100, 180, and 220 tokens and have utilities 0.5, 0.8, and 0.9. Which simple choice maximizes total utility?
Solution. A+B costs 280 and yields 1.3. C alone yields 0.9; A+C costs 320 and B+C costs 400. Therefore A+B is the best feasible combination.
Exercise 5.2. Why does sorting only by utility fail in the previous exercise?
Solution. Choosing C first leaves 80 tokens unused and yields 0.9. We must consider utility relative to cost and possible combinations, not only the largest individual value.
Exercise 5.3. Should the current goal be stored permanently?
Solution. Only if it has value beyond the task, such as a future commitment or long-lived project. Temporary subtasks belong to working memory and may be discarded after completion.
Exercise 5.4. Two equivalent memories each consume half the budget. What should we do?
Solution. Select one representation and, if necessary, attach both compact provenance references. The marginal diversity of the second paraphrase is nearly zero.
Exercise 5.5. A memory contains an old user instruction. Should it override the current instruction?
Solution. Not automatically. The context assembler must respect authority, time, and scope. Memory is retrieved data. Authority comes from the currently applicable instruction hierarchy.
6. Episodic memory: preserving occurrences
An episode records a bounded occurrence. For Aurora, we can represent
\[E_2 = (\text{request},\ \text{tea recommendation},\ \text{acceptance},\ 2,\ \text{messages } 17\text{-}19).\]The episode preserves order and context. If we store only entity-relation-entity pairs, we lose the fact that the recommendation came before acceptance and occurred in a specific conversation. Event graphs address this by creating a vertex for the episode and edges for participants, actions, objects, outcomes, and sources.
Two similar episodes remain two episodes. If Lia accepted tea in February and rejected it in July, we do not have a duplicate. We have evidence of change or context dependence. That independent identity is another reason to use a multigraph.
Episode segmentation is difficult. Boundaries may be temporal, conversational, or causal. A thirty-minute pause may not end a task, while a change of goal may. A practical method combines intent change, subgoal completion, elapsed time, and participant changes.
Raw detail and summary should coexist. The summary accelerates retrieval; the original trail supports auditing. A summary must never invent causality that is absent from the event.
6.1 Solved exercises
Exercise 6.1. Two identical recommendations occurred on different days. How many episodes exist?
Solution. Two. Time, context, and outcome may differ even when the action text is identical.
Exercise 6.2. An episode contains a request and recommendation, but acceptance arrived in the next message. Should it be linked?
Solution. Yes, if it belongs to the same goal and responds to the recommendation. Causal and conversational boundaries are more informative than message count alone.
Exercise 6.3. Is the summary “tea always works” valid after one acceptance?
Solution. No. One episode supports only that it worked in the observed situation. The word “always” generalizes beyond the evidence.
Exercise 6.4. Why keep the original message after creating a structured episode?
Solution. To verify extraction, recover nuance, correct interpretation, and support auditing. Structure enables computation; the source preserves evidence.
Exercise 6.5. Can a topic change end an episode without a time gap?
Solution. Yes. If the previous goal was completed and another began, the semantic boundary is sufficient. Time is one signal, not the exclusive definition.
7. Semantic memory and consolidation
Semantic memory expresses claims that extend beyond one episode:
\[c_1 = (\text{Lia},\ \text{prefers},\ \text{tea without sugar}, \gamma=0.96,\ [0,\infty)).\]$\gamma$ is confidence, and the interval represents known validity. The claim should point to evidence such as the declaration on day 0 and acceptance on day 2. Confidence is not a calibrated probability by decree. It is an operational value whose meaning must be measured.
Consolidation turns several experiences into a more stable representation. It may reinforce an existing claim, create a broader claim, divide an overly broad rule into contexts, relate incompatible versions, or retain a hypothesis without promoting it to fact.
The risk is destructive compression. “Lia avoids caffeine at night” and “Lia drinks coffee in the morning” must not become either “Lia does not drink coffee” or “Lia drinks coffee.” Correct knowledge preserves the temporal qualifier.
Safe consolidation produces both the new claim and its derivation links. If a source is removed by consent or found to be wrong, the system knows which conclusions must be recomputed.
7.1 Solved exercises
Exercise 7.1. Can one explicit statement create semantic memory?
Solution. Yes, with appropriate provenance and confidence. Repetition is not required when a person explicitly states a preference, but the claim remains revisable.
Exercise 7.2. Do two tea acceptances prove that Lia always prefers tea?
Solution. No. They increase evidence for a preference in the observed situations, but “always” would require impossible coverage. Memory should avoid absolute quantifiers.
Exercise 7.3. How should we consolidate “avoids caffeine at night” and “drinks coffee in the morning”?
Solution. Keep two claims conditioned on time of day. They are compatible. We may derive “caffeine preference depends on time,” with links to both.
Exercise 7.4. One piece of evidence is invalidated. What happens to the consolidated claim?
Solution. Its confidence and validity must be reassessed. The derivation link makes the dependency explicit. Deleting the source while retaining the conclusion unchanged would be inconsistent.
Exercise 7.5. Why is repetition count insufficient as confidence?
Solution. Ten copies of one source are not ten independent pieces of evidence. Independence, quality, recency, and possible duplication matter.
8. Procedural and reflective memory
Factual memory answers “what do I know?” Procedural memory answers “how do I usually act in this situation?” A procedure can be described by
\[p_j=(\text{condition},\ \text{steps},\ \text{constraints},\ \text{evidence},\ \eta_j),\]where $\eta_j$ estimates utility or success. In our example, one procedure is: when a drink recommendation does not specify time of day, ask whether it is for morning or evening before choosing a caffeinated option.
Procedures should not be promoted from a single coincidence. They need executions and outcomes. Success rate matters, but so do the situations in which the procedure was attempted. A policy used successfully twice is not necessarily better than one tested in a hundred cases.
Reflective memory records an interpretation of experiences: “contextual restrictions should not be treated as global prohibitions.” It is powerful and dangerous because an agent-generated conclusion can influence many future decisions. Every reflection needs a scope, evidence, and a test that could refute it.
Procedures and reflections form a layer of experience about experience. Episodes say what happened. Procedures and reflections try to improve the next behavior. This layer brings memory closer to continual learning without requiring immediate weight updates.
8.1 Solved exercises
Exercise 8.1. Aurora asked about time of day and made one successful recommendation. Should the procedure already be permanent?
Solution. No. It may be stored as a candidate with limited evidence. Further executions should measure when the question helps and when it only adds friction.
Exercise 8.2. One procedure has 2 successes in 2 attempts; another has 80 in 100. Which is better?
Solution. The first has a higher observed rate but much greater uncertainty. Rates alone do not decide. A conservative policy considers trial count, context, and error cost.
Exercise 8.3. What evidence would contradict “time of day always resolves drink preferences”?
Solution. A case in which the constraint depends on health, availability, or medication rather than time. The word “always” makes the reflection easy to refute.
Exercise 8.4. Is a fixed developer instruction procedural memory learned by the agent?
Solution. It guides procedure functionally, but was not learned from experience. It should remain in a policy layer with different authority so observations cannot rewrite it.
Exercise 8.5. Why link a procedure to failure episodes as well as successes?
Solution. Failures delimit the validity domain. Without them, memory selects only favorable evidence and turns a local heuristic into a general rule.
9. What deserves to be written
Writing everything is expensive and dangerous. Writing too little prevents continuity. An initial policy can combine salience $S$, novelty $N$, future utility $U$, and confidence $C$:
\[W(o)=0.30S+0.25N+0.30U+0.15C.\]For “Lia avoids caffeine at night,” let $S=0.85$, $N=0.90$, $U=0.95$, and $C=0.99$:
\[W=0.30(0.85)+0.25(0.90)+0.30(0.95)+0.15(0.99) =0.9135.\]With a threshold of $0.65$, the candidate is written. This calculation is an explainable baseline, not a universal truth. The weights must be learned or calibrated for the domain.
Useful signals include an explicit preference, future commitment, factual correction, action outcome, repeatable error, or information required by a long task. Counter-signals include casual conversation, duplicates, data without provenance, highly sensitive content without consent, and low-confidence inference.
Privacy cannot be reduced to a small score penalty. Some classes require a blocking rule, consent, or short retention even when utility is high. The architecture must separate “is this worth remembering?” from “are we authorized to remember it?”
Try the laboratory controls. Then change the query and budget to see why writing and retrieval solve different problems.
9.1 Solved exercises
Exercise 9.1. Calculate $W$ for $S=0.4$, $N=0.2$, $U=0.5$, and $C=0.8$.
Solution. $W=0.30(0.4)+0.25(0.2)+0.30(0.5)+0.15(0.8)=0.12+0.05+0.15+0.12=0.44$. With a $0.65$ threshold, we would not write it.
Exercise 9.2. A candidate scores $0.90$ but contains sensitive data without consent. What should happen?
Solution. Block writing or apply the specific sensitive-data policy. A utility threshold does not replace authorization.
Exercise 9.3. Do ten identical messages increase novelty?
Solution. No. The first may be new; the others are duplicates or additional evidence. Confusing frequency with novelty causes amplification.
Exercise 9.4. An explicit correction has low lexical salience but high impact. Which component should capture it?
Solution. Future utility and a critical-event rule. Purely continuous policies can miss rare cases, so corrections should also trigger a deterministic management path.
Exercise 9.5. Which errors decrease when the threshold rises from $0.65$ to $0.90$?
Solution. False-positive writes decrease, but false negatives increase because useful experiences fail to persist. The choice depends on relative cost and must be evaluated on future tasks.
10. Retrieval means reconstructing evidence
Vector search answers which texts appear semantically close. Memory reading must answer a larger question: which evidence, versions, episodes, and constraints allow the agent to decide correctly now?
A minimal hybrid score is
\[R(m,q)=0.45\,\operatorname{rel}(m,q) +0.20\,\operatorname{imp}(m) +0.20\,\operatorname{conf}(m) +0.15\,\operatorname{rec}(m).\]The recency function used by our program is
\[\operatorname{rec}(m)=\frac{1}{1+\operatorname{age\_days}(m)/30}.\]For query tags lia, night, and drink, memory “Lia avoids caffeine at night” matches two of three tags, so $\operatorname{rel}=2/3$. On day 90, its age is 38 days and recency is $1/(1+38/30)\approx0.441$. With importance $0.93$ and confidence $0.99$:
After ranking, the reader expands relations. A retrieved preference may bring its strongest evidence. A disputed claim may bring the previous version. A procedure may bring a relevant failure. This step distinguishes retrieving items from assembling an evidence packet.
The result must include identity and provenance, not only text. The agent can then cite “message from day 52,” distinguish inference from declaration, and avoid merging similar memories during generation.
10.1 Solved exercises
Exercise 10.1. Calculate recency for a sixty-day-old memory.
Solution. $\operatorname{rec}=1/(1+60/30)=1/3\approx0.333$.
Exercise 10.2. A memory has relevance 1, importance 0, confidence 1, and recency 0. What is $R$?
Solution. $R=0.45+0+0.20+0=0.65$. Relevance and confidence still place it above many candidates.
Exercise 10.3. Why might the morning-coffee memory help an evening query?
Solution. It qualifies the apparent contradiction. Lia did not abandon coffee globally; the restriction is contextual. Relation expansion may retrieve it despite modest lexical similarity.
Exercise 10.4. Three nearly identical items lead the ranking. Should all three enter the context?
Solution. Usually not. Diversity reranking or grouping should reduce redundancy. The budget can be spent on complementary evidence.
Exercise 10.5. Does the program retrieve a very recent memory with zero relevance?
Solution. No. The implementation discards zero relevance before scoring. This prevents recency alone from introducing unrelated content, although later structural expansion may still bring a justified neighbor.
11. Updating, contradicting, and forgetting
Mutable memory does not need to erase the past. Consider:
\[c_{\text{old}}: \text{“Lia prefers tea”},\quad [0,120),\] \[c_{\text{new}}: \text{“Lia no longer wants tea”},\quad [120,\infty).\]Closing the first validity interval and creating a superseded_by edge preserves answers to two questions: “what is valid now?” and “what did we believe on day 90?” Overwriting the text answers only the first and loses the explanation.
Not every divergence is a contradiction. Two claims may be equivalent, one may specialize the other, they may hold in different contexts, refer to different times, come from conflicting sources, or actually be incompatible within the same scope.
Forgetting also has several forms. Decay reduces retrieval priority. Archiving removes an item from the hot layer. Compaction preserves a summary and provenance. Deletion removes content according to consent or policy. Mixing these four operations makes it impossible to explain what happened.
Old items are not necessarily useless. A stable preference may have low recency and high importance. A rare security event may need mandatory retention. Forgetting should consider value, risk, redundancy, stability, and legal requirements, not age alone.
11.1 Solved exercises
Exercise 11.1. Does “avoids caffeine at night” contradict “drinks coffee in the morning”?
Solution. No. The temporal contexts are disjoint. The apparent contradiction disappears when qualifiers are preserved.
Exercise 11.2. Why not delete the old preference after a correction?
Solution. Historical queries, auditing, and explanations of change depend on it. Closing validity serves the current view without destroying the trajectory.
Exercise 11.3. Should a rarely retrieved memory be deleted?
Solution. Not necessarily. Low use may justify archiving, but obligations, risk, or historical value may require retention. Past retrieval does not measure all future utility.
Exercise 11.4. What is the difference between compaction and deletion?
Solution. Compaction replaces details with a smaller representation while preserving some information and links. Deletion removes content under a policy, including derived items when necessary.
Exercise 11.5. Two sources disagree and neither is clearly stronger. Should management choose one?
Solution. No. It should retain the competing claims, their sources, and a disputed state. A silent choice would turn uncertainty into certainty.
12. A reference implementation in C++23
The agent_memory.cpp program implements an intentionally small baseline. Each memory has an identity, kind, text, tags, importance, confidence, creation time, validity, and state. Relations also have independent identities, which allows multiple edges between the same pair.
The reading core is:
const double relevance = tag_relevance(query_tags, memory.tags);
if (relevance == 0.0) {
continue;
}
const int age_days = current_day - memory.created_day;
const double recency =
1.0 / (1.0 + static_cast<double>(age_days) / 30.0);
const double total =
0.45 * relevance +
0.20 * memory.importance +
0.20 * memory.confidence +
0.15 * recency;
Updating does not erase:
old_memory.active = false;
old_memory.valid_until = day;
relate(old_id, new_id, "superseded_by", 1.0);
The comment explains the invariant: closing validity preserves historical queries. The program also rejects scores outside $[0,1]$, unknown identifiers, negative dates, and self-supersession.
For query {"lia", "night", "drink"}, the evening constraint matches two tags and ranks first. The other two memories match only lia, so they appear below it. The verified output is:
M2 score=0.750 relevance=0.667 recency=0.441 | Lia avoids caffeine at night
M3 score=0.622 relevance=0.333 recency=0.882 | Lia resumed drinking coffee in the morning
M0 score=0.523 relevance=0.333 recency=0.250 | Lia prefers tea without sugar
relations=3
Suggested GCC compilation:
g++ -std=c++23 -O2 -Wall -Wextra -Wconversion -Wshadow -pedantic \
memoria_grafos/codigo/agent_memory.cpp -o agent_memory
The reference search costs $O(nqt+n\log n)$, where $n$ is the number of candidate memories, $q$ is the number of query tags, and $t$ represents the cost of finding a tag. A real system uses inverted, vector, temporal, and graph indexes to reduce the candidate set before ranking.
12.1 Solved exercises
Exercise 12.1. Why is MemoryId a type instead of a std::uint32_t used everywhere?
Solution. The type expresses intent and reduces confusion with relation identifiers or counters. It also centralizes comparison without relevant runtime cost.
Exercise 12.2. Why check std::isfinite?
Solution. Simple comparisons do not reject every NaN. A NaN would contaminate the score and could break sorting, whose comparator requires consistent behavior.
Exercise 12.3. Why break ties by identifier?
Solution. To make results deterministic. Tests, the article, and debugging reproduce the same order when scores are equal.
Exercise 12.4. What limitation comes from marking an old memory inactive?
Solution. Current search ignores old versions even for historical queries. A temporal implementation should receive the query instant and test the validity interval rather than only active.
Exercise 12.5. Why do relations have their own identifiers?
Solution. Two memories may have several parallel relations, each with independent weight, provenance, time, or deletion. Endpoints do not identify an edge in a multigraph.
13. Evaluation, failures, and the frontier
A system that correctly answers questions about the past may still have poor memory. Perhaps the entire history was placed in context. Perhaps the answer came from general knowledge. Evaluation must isolate capabilities.
LoCoMo and LongMemEval measure long-conversation understanding and memory across extended interactions. MemoryAgentBench separates retrieval, integration, updating, and memory-use skills. Recent agent-memory taxonomies organize the field through writing, management, and reading. Together, they suggest an evaluation matrix:
| Layer | Question | Possible metric |
|---|---|---|
| writing | were useful experiences recorded? | candidate precision and coverage |
| management | did versions and links remain correct? | temporal and provenance consistency |
| reading | was required evidence retrieved? | recall under budget |
| use | did the action respect the evidence? | task success and faithfulness |
| forgetting | did prohibited data stop influencing behavior? | deletion effectiveness |
Typical failures include false memory, in which an inference is stored as a declaration; amplification, in which duplicates increase confidence; staleness, in which an old version dominates the current one; cross-user contamination; poisoning by persistent adversarial content; retrieval without use; use without evidence; and incomplete forgetting, in which a source is deleted but derived claims remain active.
The frontier combines different architectural responses. MemGPT treats context as hierarchical memory inspired by virtual memory. MemoryOS organizes short-, medium-, and long-term layers. Mem0 and A-MEM study adaptive extraction and organization. HippoRAG approaches retrieval as graph-based associative memory. GraphRAG builds graph indexes over documents. Graphiti/Zep emphasizes temporal knowledge and updates. None of these names removes the fundamental decisions: what to write, how to represent it, how to revise it, what to retrieve, and how to prove that the system works.
In the next articles, the temporal multigraph stops being an intuition and becomes a schema. Episodes, entities, claims, sources, actions, and outcomes will have independent identities. Parallel edges will preserve evidence, causality, and time. Queries will separate current state from historical trajectory.
13.1 Solved exercises
Exercise 13.1. A test gives the model the entire history. Does it measure memory writing?
Solution. No. It removes the need to select what should persist. It may measure long-context comprehension, but not policy $W$.
Exercise 13.2. The correct item was retrieved, but the answer contradicts it. Which layer failed?
Solution. The use or generation layer. Reading recall was sufficient. We must measure whether the policy respects evidence.
Exercise 13.3. How can we test temporal updating?
Solution. Present a preference, query it, provide a correction, and query both the present and an earlier instant. The system must change the current answer without losing the historical one.
Exercise 13.4. How can we detect cross-user contamination?
Solution. Create incompatible preferences for different identities and query each in separate sessions. Any crossover reveals a scope or authorization failure.
Exercise 13.5. Is deleting the source enough to test the right to be forgotten?
Solution. No. We must inspect indexes, summaries, derived claims, caches, assembled context, and future answers. The test measures residual influence, not merely the absence of one row.
Conclusion
Agent memory is not a larger context window, a message archive, or a vector index with a different name. It is a system for transforming experience:
\[\text{observation} \xrightarrow{W} \text{candidate memory} \xrightarrow{G} \text{persistent state} \xrightarrow{R} \text{decision evidence} \xrightarrow{\pi} \text{action and new outcome}.\]Every arrow can fail and must be evaluated. Writing must be selective and authorized. Management must preserve identity, time, provenance, and versions. Reading must reconstruct diverse evidence under a budget. Action must respect that evidence, and the outcome must return to the cycle without creating self-confirmation.
A multigraph is appropriate because experience does not fit into one edge per entity pair. Lia and tea may be connected by preference, recommendation, acceptance, time, evidence, and correction. These relations are not duplicates. They are different paths through which the past can justify a present decision.
In the next article, The Memory Multigraph, we will build the formal schema of this experience and decide which objects should be vertices, which should be edges, and which identities must never be collapsed.
References
- CHEN, Howard et al. MemoryOS: An Operating System for Memory-Augmented Generation. arXiv, 2025. Available at: https://arxiv.org/abs/2506.06326.
- DU, Pengfei. Memory for Autonomous LLM Agents: Mechanisms, Evaluation, and Emerging Frontiers. arXiv, 2026. Available at: https://arxiv.org/abs/2603.07670.
- EDGE, Darren et al. From Local to Global: A Graph RAG Approach to Query-Focused Summarization. arXiv, 2024. Available at: https://arxiv.org/abs/2404.16130.
- GUTIERREZ, Bernal Jimenez et al. MemoryAgentBench: Evaluating Memory in LLM Agents via Incremental Multi-Turn Interactions. arXiv, 2025. Available at: https://arxiv.org/abs/2507.05257.
- JIANG, Ziyan et al. HippoRAG: Neurobiologically Inspired Long-Term Memory for Large Language Models. arXiv, 2024. Available at: https://arxiv.org/abs/2405.14831.
- LIU, Nelson F. et al. Lost in the Middle: How Language Models Use Long Contexts. Transactions of the Association for Computational Linguistics, 2024. Available at: https://arxiv.org/abs/2307.03172.
- MAHARANA, Adyasha et al. LoCoMo: Evaluating Very Long-Term Conversational Memory of LLM Agents. arXiv, 2024. Available at: https://arxiv.org/abs/2402.17753.
- PAN, Shirui et al. Unifying Large Language Models and Knowledge Graphs: A Roadmap. IEEE Transactions on Knowledge and Data Engineering, 2024. Available at: https://arxiv.org/abs/2306.08302.
- PARK, Joon Sung et al. Generative Agents: Interactive Simulacra of Human Behavior. UIST, 2023. Available at: https://arxiv.org/abs/2304.03442.
- PIPER, Benjamin et al. Mem0: Building Production-Ready AI Agents with Scalable Long-Term Memory. arXiv, 2025. Available at: https://arxiv.org/abs/2504.19413.
- RASMUSSEN, Preston et al. Zep: A Temporal Knowledge Graph Architecture for Agent Memory. arXiv, 2025. Available at: https://arxiv.org/abs/2501.13956.
- SHINN, Noah et al. Reflexion: Language Agents with Verbal Reinforcement Learning. NeurIPS, 2023. Available at: https://arxiv.org/abs/2303.11366.
- WANG, Xueyang et al. A-MEM: Agentic Memory for LLM Agents. arXiv, 2025. Available at: https://arxiv.org/abs/2502.12110.
- WU, Di et al. LongMemEval: Benchmarking Chat Assistants on Long-Term Interactive Memory. arXiv, 2024. Available at: https://arxiv.org/abs/2410.10813.
- XU, Kevin et al. HippoRAG 2: From RAG to Memory. arXiv, 2025. Available at: https://arxiv.org/abs/2502.14802.
- XU, Lei et al. MemGPT: Towards LLMs as Operating Systems. arXiv, 2023. Available at: https://arxiv.org/abs/2310.08560.
Series Index: Memory in Graphs
- 1. Graph Neural Networks: An Introduction
- 2. Multigraphs: Parallel Edges and Distinct Paths
- 3. Memory for Agents: From Context to Persistent Experience (You are here)
(Updated: )