By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
The memory lesson used trim_messages to keep a conversation inside a token budget. That is short-term memory: a view over the current thread, rebuilt on every call. A customer profile is the other kind of memory: a durable record that outlives the thread and changes what the agent should do next time.
Harbor Helper needs both. 'The customer gave me the order id two turns ago' is a context problem. 'This customer prefers email and is on the plus plan' is a profile problem. Mixing them is how a support agent forgets a preference on every new ticket, or stores something it was never allowed to store.
| Question | Short-term memory | Long-term profile |
|---|---|---|
| Lifetime | The current thread | Months, until the customer deletes it |
| Owner | The agent loop | The customer, through an auditable write path |
| Shape | The message list or its summary | A set of small keyed claims |
| Failure if lost | The agent re-asks a question | The agent repeats its last mistake |
The profile is a set of claims, not a document blob. Each claim has a value, a source, a kind (explicit or inferred), a timestamp and, for inferred claims, a confidence. That structure is what makes the write path reviewable and the read path selective.
The snippet handles the two hardest cases in one pass: a newer inference arriving after an explicit statement, and an update that tries to store a forbidden attribute. Both are governed by rules in ordinary Python, not by the model's judgment about what is appropriate.
"""Merge profile updates with provenance, precedence and a forbidden-key guard."""
from copy import deepcopy
FORBIDDEN_KEYS = {"health", "religion", "political_view"}
PROFILE = {
"preferred_channel": {
"value": "email",
"kind": "inferred",
"source": "triage-v2",
"updated_on": "2026-05-01",
},
"plan": {
"value": "plus",
"kind": "explicit",
"source": "ticket 8891",
"updated_on": "2026-04-18",
},
}
UPDATES = [
{
"key": "preferred_channel",
"value": "chat",
"kind": "explicit",
"source": "ticket 9004",
"updated_on": "2026-05-12",
},
{
"key": "language",
"value": "en",
"kind": "explicit",
"source": "locale header",
"updated_on": "2026-05-12",
},
{
"key": "preferred_channel",
"value": "sms",
"kind": "inferred",
"source": "triage-v3",
"updated_on": "2026-05-13",
},
{
"key": "health",
"value": "allergy",
"kind": "inferred",
"source": "conversation",
"updated_on": "2026-05-13",
},
]
def merge_profile(profile, updates):
merged = deepcopy(profile)
rejected = []
for update in updates:
key = update["key"]
if key in FORBIDDEN_KEYS:
rejected.append((key, "forbidden"))
continue
claim = {k: v for k, v in update.items() if k != "key"}
current = merged.get(key)
if current is None:
merged[key] = claim
elif update["kind"] == "explicit" and current["kind"] != "explicit":
merged[key] = claim
elif update["kind"] == current["kind"] and (
update["updated_on"] > current["updated_on"]
):
merged[key] = claim
else:
rejected.append((key, "lower precedence: " + update["kind"]))
return merged, rejected
merged, rejected = merge_profile(PROFILE, UPDATES)
for key in sorted(merged):
claim = merged[key]
print(
key,
"=",
claim["value"],
f"({claim['kind']} from {claim['source']})",
)
print("rejected:", rejected)preferred_channel stays chat even though a newer sms inference arrived, because an explicit statement beats an inference regardless of timestamp. The forbidden health claim is rejected rather than dropped silently: the rejection list is logged so the data path can be fixed. language is created because nothing contradicted it, and plan is untouched because no update addressed it.
Writing safely is half the job. The read path decides what the agent can act on. Load the profile only for the tenant and customer on the current ticket, pass a small rendered summary into the prompt, and never let the model read the raw store. A useful shape is a bounded block of at most a dozen claims with their sources, so the model can say 'the customer said on ticket 9004 that they prefer chat' instead of asserting a preference with no origin.
Not every stored claim belongs in every prompt. Relevance is a routing decision: billing questions need the plan and the last order, delivery questions need the address region and the order, account questions need the locale. Sending the whole profile is a token cost and a privacy cost, so select claims by the same route that chose the worker. If the customer says 'forget my preferences', delete the claims and write the deletion to the audit log; do not override the statement with a summary generated from the old profile.
The test for a profile implementation is a small set of deterministic cases: an explicit claim overriding an inference, an inference that may only fill an empty slot, a forbidden key, an expired inference, a cross-tenant lookup and a delete that leaves nothing behind. None of them needs a model call, which means the profile contract can be protected in CI.
Key takeaways
Short-term memory is a view over the thread; a long-term profile is a durable set of keyed claims with sources.
Store claims, not a blob: value, kind (explicit or inferred), source, timestamp and confidence.
Explicit statements always beat inferences, forbidden keys are rejected by allowlist, and inferred claims expire.
Load only the claims the current route needs, scoped by the request's tenant and never by a model-supplied id.
Give the customer a read and delete path; memory without consent is a liability, not a feature.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.