Deepanshu's Diary

Engineering Durable Context: Proven Patterns for Long-Lived Assistants

--context-engineeringaiassistants

Engineering Durable Context: Proven Patterns for Long-Lived Assistants

A product manager once asked me to build an assistant that 'remembers my preferences across months — but never leaks private notes, and never fabricates facts.' That single ask condenses the core engineering trade-off I face when designing long-lived assistants: continuity versus safety, privacy, and correctness. In this post I break that trade into concrete axes (time horizon, provenance/trust, and operational gate state), show a minimal data-flow and component map you can implement, map patterns to features I’ve shipped in my projects, and enumerate failure modes plus pragmatic mitigations and a checklist to validate your design.

I’ll walk through a practical mental model for context, a concrete data-flow example, what I’ve built that embodies these ideas, where it breaks, and a checklist you can use to validate a design.

Mental model: context as layered, gated, and versioned

Think of context as three orthogonal axes:

- Time/horizon (recent-turn → session → short-term memory → long-lived knowledge). - Provenance and trust (live user utterance, curated tenant KB, scraped web, agent-executed action). - Operational gate state (consented, approval-gated, review-only, encrypted-at-rest).

Concrete rules I use:

- Treat each axis explicitly: label every context item with source, timestamp, and gate state. - Compose context at request time in stable blocks (system instructions, recent-turn window, retrieval results, approved memory snippets). This avoids interleaving that makes provenance unclear. - Enforce TTLs and versioning for long-lived memories so the assistant can prefer newer, reviewed facts.

These practices echo work formalizing protocol for model context and retrieval composition [1][2].

An architecture / data-flow example

Here’s a minimal path for a voice assistant that keeps tenant knowledge and consented memory.

1. Client (voice channel) → STT → intent & canonicalized utterance. 2. Session manager: attach session ID, turn timestamp, and immediate-turn transcript to recent-turn ring buffer. 3. Context assembler collects: system prompt, recent-turn window, retrieval from tenant KB (pgvector), and consented memory records. 4. Retrieval: query vector store with embeddings, apply tenant filters and recency scoring, return top N chunks with citations. 5. Prompt builder: fold context blocks with metadata (source, score, age). Apply token budget and safety blocker. 6. Model responds → response parser. If response requests an action (e.g., outbound message, API call), route to approval gate. 7. Audit logger records the full request/response snapshot, retrieval ids, and decision path (approved/blocked).

Small table: components and responsibilities

| Component | Responsibility | |---|---| | Session manager | manage windows, session TTLs | | Vector store (pgvector) | store chunks + embeddings + tenant scope | | Context assembler | composes stable blocks with provenance | | Approval engine | enforces consent & operator gates | | Audit store | immutable event log for review |

This pipeline enforces provenance and allows operators to replay or redact content if needed.

What I built around this idea

I’ve applied these patterns across several projects in my portfolio; below I map the idea to implemented features and operational gates.

- ARIL local monorepo: tenant-scoped knowledge bases with documents, chunking, embeddings, pgvector retrieval, citations, and evaluation metrics. Implemented: local KBs, chunking, retrieval and protected routes. Gate: real production Postgres migration remains an environment gate [5].

- Amazon Voice Agent: provider-agnostic, mock-first voice runtime with multilingual detection, consented memory, audit events, approval-gated calls, encrypted provider keys, health probes, autonomous test reports, and a full operator UI. Implemented: consented memory, audit trails, approval gates and health probes. Gate: live streaming, barge-in, transfer, and real provider acceptance remain gates (so outbound telephony is currently gated) [5].

- Listenly (meeting copilot): local-first grounded context assembly, session summaries, recent-turn windows, and citation numbering. Implemented: local session assembly and summarization with numbered citations and recent-turn windows. Gate: ingestion of real meeting recordings and durable cloud sessions are still gated [5].

- Google WhatsApp Scraper & Universal Scraper: pipelines that populate review-only growth engines / review packs. Implemented: extraction pipelines, consent/review/approved-send gates, and durable local review-pack storage. Gate: no automated sending — review-only to avoid unsafe live sends [5].

These projects show how retrieval, consent, approval gates, and auditability form the building blocks of long-lived context systems.

Where this breaks (failure modes and mitigations)

1. Retrieval of stale or contradictory information → hallucination. - Mitigation: bias toward recent vetted facts; include provenance in the prompt; explicitly ask the model to cite the chunk id and score. Run periodic re-evaluation of long-lived facts with human-in-the-loop review.

2. Selector drift (scraping or selector changes produce garbage context). - Mitigation: fixture tests for scrapers, synthetic adversarial checks, and fail-closed extraction pipelines with alerts (implemented in Universal Scraper and Google WhatsApp Scraper pipelines) [5].

3. Privacy leakage from long-lived memory (sensitive info retained inadvertently). - Mitigation: explicit consent flags, per-tenant encryption, redaction hooks, and a revoke/forget flow. Enforce approval gates for exporting memories. Amazon Voice Agent has consented memory and encrypted provider keys to reduce this risk [5].

4. Token limits and latency when assembling broad context. - Mitigation: truncate with prioritized blocks (system > recent-turn > high-confidence retrievals), precompute embeddings & summaries for large docs, and maintain a compact “manifest” of high-value facts.

5. Conflicting facts across sources (tenant KB vs scraped web). - Mitigation: conflict resolution policies (prefer tenant-curated over scraped), surface conflicts to the user, and mark conflicted facts as low-confidence.

6. Safety bypass via adversarial inputs in retrieved docs. - Mitigation: adversarial safety suite and reject-list filters; safety scoring before including retrievals (implemented in scraper pipelines) [5].

A practical checklist (5–8 checks)

1. Label everything: every context item must have source, timestamp, and gate state. 2. Define session boundaries and retention policy (how long does session memory persist?). 3. Enforce consent & approval gates for any memory used for actions or external sends. 4. Use provenance in prompts: include chunk IDs, scores, and source names. 5. Precompute embeddings, and test retrievals with unit/fixture tests. 6. Fail-closed on unknown tenants, missing keys, or failed safety checks. 7. Maintain an immutable audit event store for replay and investigation. 8. Run adversarial and smoke tests on scrapers and retrievals.

Conclusion

Long-lived context for assistants is achievable but is an engineering discipline: label and gate context, enforce provenance, prioritize recency and vetting, and treat operational gates (consent, approval, audit) as first-class features. The projects I’ve listed show these practices in code and prototype; operational deployment and some live integrations remain intentional gates until safety, privacy, and provider acceptance are proven.

References

[1] Model Context Protocol announcement — Anthropic: https://www.anthropic.com/news/model-context-protocol

[2] Model Context Protocol introduction: https://modelcontextprotocol.io/introduction

[3] Retrieval-Augmented Generation paper (example RAG literature): https://arxiv.org/abs/2005.11401

[4] Context, evaluation and scaling papers (example related reading): https://arxiv.org/abs/2307.03172

[5] My portfolio (projects referenced above): https://github.com/deepanshuvermaa/my-portfolio

[6] Museum of Failure repo (engineering artifacts and experiments): https://github.com/deepanshuvermaa/museum-of-failure

[7] Go2 GST (invoice extraction example): https://github.com/deepanshuvermaa/go2-gst

[8] Trading Engine (risk-engine patterns referenced): https://github.com/deepanshuvermaa/trading-engine

Copied!
Back to all posts