Agent memory
Give an AI agent durable, on-device memory — partition per user and session with scope_key, store conversation turns, and recall them on demand.
LLM agents forget everything between calls. Telys gives them durable, on-device memory: store what the user says, then recall the relevant pieces before the next reply. This page builds that loop in a few lines of Python, keeping each user and session isolated with scope_key.
Isolate memory with scope keys
A collection's partition_by field is its physical partition key. Give each user+session its own partition by putting a composed key in that field. scope_key joins its parts into one stable string (with a \x1f separator), so scope_key("alice", "session-42") and scope_key("bob", "session-42") never collide.
from telys import scope_key
print(repr(scope_key("alice", "session-42")))Expected output:
'alice\x1fsession-42'Create the memory collection
Partition by scope — the field that holds each scope_key value.
from telys import Telys, Eq, scope_key
from telys.embedding import AlgentaMultigramEmbedder
t = Telys("./agent-memory")
emb = AlgentaMultigramEmbedder()
memory = t.create_collection(
"chat",
dim=emb.profile.dimension,
partition_by="scope",
embedder=emb,
)Wrap remember and recall
Two helpers are all an agent needs. remember uses upsert_texts, so re-running a turn replaces it instead of raising KeyError. recall scopes the search to one partition with where=Eq("scope", scope), so a user only ever reads their own memory.
def remember(scope, turn_id, text, role="turn"):
memory.upsert_texts(
[text],
ids=[f"{scope}:{turn_id}"],
metadata=[{"scope": scope, "role": role}],
)
def recall(scope, query, k=3):
hits = memory.search_text(
query,
top_k=k,
where=Eq("scope", scope),
)
return hits["ids"]Prefixing each id with its scope keeps ids unique across users in one collection.
Store turns, then recall
Store a few turns for one user+session, persist, then ask a question that should surface the relevant memory.
# one user, one session -> one partition
scope = scope_key("alice", "session-42")
for turn_id, text in [
("t1", "I prefer metric units."),
("t2", "I'm planning a trip to Kyoto in October."),
("t3", "My budget for the trip is 2000 dollars."),
]:
remember(scope, turn_id, text)
memory.save()
# later, before the agent answers, recall the relevant memory
print(recall(scope, "how much can I spend on the trip?"))Expected result:
['alice\x1fsession-42:t3', 'alice\x1fsession-42:t2', 'alice\x1fsession-42:t1']The budget turn (t3) ranks first, the trip turn (t2) next. Feed those recalled strings into the model's prompt as context for the reply.
Update a fact
When the user changes their mind, upsert the same id to replace the stored fact in place. upsert_texts inserts or replaces; save() commits it.
remember(scope, "t1", "I now prefer imperial units.") # replaces t1
memory.save()
print(recall(scope, "which units do I like?", k=1))Expected result:
['alice\x1fsession-42:t1']Recall the same id and you get the updated fact. Because every write and read is scoped, scope_key("bob", "session-42") sees an empty memory — one agent's users never leak into another's.
Next steps
Last updated
Was this helpful?