Q&A-1Explain the agent memory lifecycle.
Answer
What a strong candidate says first (60–120s). Assume a multi-tenant agent where the application owns persistence and the model only ever sees what gets injected into its context window. The lifecycle is six stages (capture, admit, store, retrieve, inject, forget) and the model owns none of them. A post-turn hook or tool call captures candidate memories; an admission check validates purpose, consent, source and sensitivity; the store writes a versioned, tenant-scoped record plus a vector index; retrieval generates candidates under the caller's identity and task, ranks them, and injects the top few as attributed untrusted data inside a token budget; the effect is logged; and the record is later expired, superseded or deleted with propagation. The decision that shapes every other stage is where you filter: at write time or at read time.
Mechanism, stage by stage.
Capture and admit. Extraction runs after the turn, not during it, so a slow extractor never blocks the response. Write-time filtering (drop anything without a stated purpose or consent) keeps the store small and the index clean, but it can't know future tasks, so it discards things you'll want later. Read-time filtering keeps everything and pays at query time with a larger index and a harder authorization check. Most production systems do both: admit on sensitivity and consent, defer relevance to read time.
Store. A row per memory: tenant_id, content, embedding, ttl, supersedes_id, created_at. With text-embedding-3-small the vector is 1536 float32 dimensions: about 6 KB per memory raw. A million memories is ~6 GB of vectors before index overhead; an HNSW index typically adds roughly 1.5×, so budget ~9–10 GB. That number is why you expire aggressively.
Retrieve. Candidate generation is an approximate-nearest-neighbour search, then ranking. Pure cosine similarity surfaces stale facts, so rank on a blend: similarity, recency decay, and a pinned importance score. The tenant filter must run before the ANN scan, not after: filtering after means you fetched other tenants' rows.
-- pgvector: <=> is cosine distance
SELECT id, content, 1 - (embedding <=> %s) AS score
FROM memories
WHERE tenant_id = %s AND ttl > now()
ORDER BY embedding <=> %s
LIMIT 20;Inject. Assemble under a fixed budget: say 8k tokens of memory inside a 128k-token window. Each injected memory is labelled with its source and timestamp and marked untrusted, so the model treats it as data, not instructions.
Forget. TTL expiry handles the common case. Contradiction handling supersedes: write the new record with supersedes_id pointing at the old one, then stop returning the old one. Hard delete must propagate to the vector index, any cached summaries, and derived records: a delete that only touches the primary row leaves the fact retrievable.
Failure modes and how you catch them.
| Failure | Cause | Detection |
|---|---|---|
| Stale fact wins | similarity-only ranking | eval set of contradiction pairs |
| Cross-tenant leak | filter applied after ANN | tenant-isolation test per query path |
| Budget blowout | unbounded injection | log injected token count per turn |
| Zombie memory | delete skips index/cache | delete-then-query test |
Trade-off to name. Recency-weighted ranking answers "what did they just say" well and "what did they say in March" badly; importance-weighted ranking does the reverse. Pick per task type, and log which memories were injected so you can attribute a bad answer to a bad retrieval rather than a bad model.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.