Clinical AI Kit home Documentation hub / Memory Layers

Memory Layers

Sources: Antigravity, 2026-07-05 Raw: Memory Layers Source

Memory Layers

Four-layer memory system with automatic PII/PHI redaction before anything persists.

Layer Module Scope Persistence
1. Working Memory context.py Per LLM call Never
2. Session State ADK session.state Per conversation With DB-backed service
3. Long-Term Memory memory.py → MemoryService Cross-session With Vertex AI Memory Bank
4. A2A Context prepare_a2a_context → RemoteA2aAgent Per delegation Never

Layer 1 — Working Memory (context engineering)

context.py assembles the per-call context window. It never calls the LLM — compaction is rule-based (truncation + metadata preservation).

Function Purpose
estimate_tokens(text) chars/4 heuristic for budget decisions (no tokenizer)
build_structured_context(role, environment, task, constraints) XML-delimited sections so the model separates instructions from data
compact_history(turns, keep_recent=10, max_total_tokens=8000) Summarizes old turns to metadata, preserves recent verbatim; append-only (returns new list)
inject_at_boundaries(context, critical_info) Places critical info at start AND end ("lost in the middle" mitigation)

Token pipeline: Collect → Rank → Compress → Budget → Assemble. config.deterministic_json() (sorted keys, compact separators) improves KV-cache hits.

Layer 2 — Session State

Layer 3 — Long-Term Memory

Layer 4 — A2A Context

Only task-relevant, non-sensitive data crosses agent boundaries: no PII/PHI, no secrets, no temp:/user: keys. Used by RemoteA2aAgent delegations — see [[MCP and A2A]].

Related: [[Security Layers]] · [[Testing and Eval]] (test_memory.py, test_context.py)