Engineer First Token Budgeted Context Memory for Australian Chatbots
Engineer first guide to building application layer, token budgeted contextual memory for Australian chatbots. Practical patterns, checklist, and a hosted...
Contextual memory is the persistent, retrievable state a chatbot keeps about a user across turns and sessions, distinct from the temporary context window an LLM sees per call. The most reliable way to build it is a structured application-layer dual-memory architecture that separates working, episodic, and semantic layers rather than dumping raw conversation history back into the prompt. Done well, this cuts token costs sharply while keeping multi-session behaviour coherent, which is the whole point of building a memory system in the first place.
TL;DR:
- Structured separation of working, episodic, and profile memory layers improves retrieval speed and reduces token costs compared to undifferentiated vector indices.
- Application-layer dual memory patterns, like in Cloudflare’s Agent Memory, allow memory management outside the model, simplifying reasoning and enhancing consistency.
- Using compressed semantic triples and summaries, as in Memori, achieves high accuracy with significantly fewer tokens than full history injection.
- Rank and inject only relevant memory based on a token budget to prevent prompt bloat and maintain high retrieval precision as memory scales.
- Prioritize fact accuracy, retrieval latency under 100ms, strict data retention policies, and audit trails before deploying to ensure production reliability and privacy compliance.
Table of Contents
- What contextual memory actually means for a chatbot
- Architecture patterns that actually hold up in production
- Key technical components and implementation choices
- The engineer’s recipe: ingestion, indexing, retrieval, injection
- Where memory systems break, and how to stop it
- Best-practice checklist before you ship
- How this maps to an Australia-hosted platform
- What I’d actually build first
- Get a contextual memory architecture that fits your systems
- Sources
What contextual memory actually means for a chatbot
Contextual memory chatbots differ from stateless ones in one specific way: they retain facts, preferences, and history beyond the current session, and they can retrieve the right slice of that history on demand. A stateless chatbot forgets everything the moment the context window closes. A memory-enhanced chatbot writes selected information to durable storage and pulls it back when relevant, which is what lets a support agent remember that a customer already escalated a billing dispute three weeks ago.
Engineers building persistent memory ai systems generally split storage into three layers, sometimes four when profile data gets its own store:
- Working memory: the active session’s turns, held in the context window itself, cheap but ephemeral.
- Episodic memory: vectorised records of past conversations, retrieved by semantic similarity when a new query touches related ground.
- Semantic or profile memory: structured facts about the user (name, role, preferences, account status), usually stored as key value pairs or a small relational schema rather than free text.
Separating these layers matters because each has different retrieval characteristics. Working memory needs no retrieval at all, it is just there. Episodic memory needs a vector search that trades off recall against latency. Profile memory needs exact lookups, which vector search handles poorly and a SQL query handles instantly. A memory-enhanced chatbot that tries to store everything in one undifferentiated vector index ends up paying for slow, imprecise retrieval on facts that a simple WHERE user_id = ? query would have answered in microseconds. Layer separation also caps token cost, because you only pull the layer that answers the current question, instead of re-injecting an entire history.
Architecture patterns that actually hold up in production
Three patterns dominate real deployments, and each makes a different bet on where complexity lives.
Application-layer dual memory keeps memory logic outside the model entirely. The chatbot application owns a service that writes episodic vectors and semantic facts, then decides what to inject into the prompt before each call. This is the pattern Cloudflare’s engineering team describes in its Agent Memory design: memories get preserved at compaction time, deduplicated, and exposed to the model through a narrow “remember, recall, forget” tool surface rather than leaving the model to manage ad-hoc persistence inside its own reasoning. That constraint matters more than it sounds. A model that has to decide, turn by turn, whether and how to persist information adds reasoning overhead and inconsistent results. A model that just calls recall(topic) does not.
Memori-style structuring takes a more aggressive approach to token economy. Instead of storing raw transcripts, it converts dialogue into semantic triples and rolling conversation summaries, then retrieves against that compressed structure. The architecture paper behind this pattern reports 81.95% accuracy on the LoCoMo benchmark while using roughly 1,294 tokens per query, a fraction of what full-context injection would cost at equivalent conversation length.
CALMem’s dual memory with token-budgeted injection adds a mechanism most architectures skip: an injector that scales how much memory gets pulled into the prompt based on how much context pressure already exists. The CALMem architecture pairs episodic vector retrieval with agent-writable semantic facts, then uses a module the paper calls MOIM to recover compacted turns during the same session rather than losing them the moment the context window rolls over.
Token footprint comparison: Full-history injection scales linearly with conversation length. Memori’s triple-and-summary approach held accuracy near LoCoMo state-of-the-art while using around 1,294 tokens per query, roughly five percent of a full-context equivalent.
None of these are free. Application-layer memory means you own the storage and retrieval logic yourself, which is more upfront engineering than calling a managed API. Triple extraction adds a preprocessing step that can misfire on ambiguous phrasing. Token-budgeted injection needs tuning per use case, and getting the ranking wrong either starves the model of context or reintroduces the bloat you were trying to avoid.
Key technical components and implementation choices
The architecture decisions above rest on a handful of concrete technical choices, and each one has a well-worn default and a reason to deviate from it.
Embeddings. Managed embedding APIs are the easiest starting point and fine for most workloads under a few million vectors. Local embedding models earn their complexity when you need data sovereignty guarantees or when API latency starts showing up in your p95 response times. There is no universal winner here, it depends on your retention obligations and query volume.
Vector storage. FAISS remains the default for high-throughput similarity search at scale. pgvector suits teams already running Postgres who want memory alongside relational data without standing up a separate service. HNSW indexing shows up in both, and increasingly in local-first patterns where SQLite handles structured profile facts and an HNSW index handles the vector side in the same lightweight deployment, a combination the open-source agent-memory project implements directly for low-latency, self-hosted setups.

Retrieval strategy. Pure vector search misses exact-match cases badly, an account number or a product SKU rarely embeds close enough to itself to rank first. Hybrid retrieval, combining full-text search for exact identifiers with vector search for fuzzy preference matching, consistently produces better precision at lower latency, a pattern documented in the ark-chatbot implementation.
Fact extraction and supersession. Every context aware chatbot needs a pipeline that turns raw turns into structured facts, checks them against existing records, and decides whether a new fact supersedes an old one rather than duplicating it. Systems like RecallMEM demonstrate this with contradiction detection built into the write path, so “user moved to Perth” retires rather than sits alongside “user lives in Sydney.”
Pro Tip: Never delete a superseded fact outright. Mark it retired with a timestamp and keep it queryable. You will need it the day a customer asks why the chatbot’s understanding of their account changed.
The engineer’s recipe: ingestion, indexing, retrieval, injection
Building this from scratch breaks into five concrete stages, each with its own failure modes.
- Model the data. Messages feed two downstream tables: a triples table (subject, predicate, object, timestamp, confidence, source turn ID) and a profile table (user ID, attribute, value, last updated, superseded_by). Conversation summaries sit in a third table, keyed by session ID, refreshed on compaction.
- Ingest on-turn. After each turn, run extraction to pull candidate facts, embed the turn for episodic storage, and check the profile table for conflicts. Flush to durable storage on a schedule rather than per-token, batching writes to control database load.
- Compact and retain intra-session retrieval. When the context window fills, compact older turns into a summary, but keep a pointer that lets the retrieval layer pull the original compacted turn back if a later question needs it. CALMem’s approach to this is worth studying closely if your sessions run long.
- Rank and inject with a token budget. Before each prompt, rank candidate memories by relevance to the current question (a technique often called question-conditioned retrieval), then inject only as many as the token budget allows, MOIM-style, favouring high-confidence profile facts over loosely related episodic matches.
- Optimise batching and latency. Vector lookups and SQL queries can run in parallel rather than sequentially. Cache profile lookups aggressively since they change far less often than episodic content.
Keep extraction off the critical path wherever you can. Users notice retrieval latency immediately; they never notice that fact extraction ran two seconds after the response was already on screen.
Where memory systems break, and how to stop it
Context rot is the most common failure mode: as memory grows, naive retrieval starts pulling irrelevant matches that dilute the prompt rather than sharpening it. The fix is structural, not just “retrieve less.” Ranking by recency and relevance together, and capping injection depth adaptively as CALMem does, keeps prompt quality from degrading as the memory store scales.
Token cost creeps up fast with raw-history approaches. Memori’s benchmark result, roughly 1,294 tokens per query against 81.95% LoCoMo accuracy, shows how much headroom structured extraction buys you compared with re-sending transcripts.
Privacy and retention need explicit policy, not an afterthought. Avoid storing raw sensitive identifiers (card numbers, health details) in memory at all; a PCI-compliant approach to chatbot data keeps that category out of the memory layer entirely rather than trying to secure it after the fact. Retention windows should be defined and enforced, and a documented 30-day retention policy aligned to Australian privacy frameworks gives you a defensible standard to point to during an audit. Superseded facts should carry provenance, source, timestamp, confidence, so the system can answer “what was true then” as well as “what’s true now.”
Testing and observability get skipped more often than they should. Build acceptance tests that check fact retrieval precision against known ground truth, log every write and supersession event for audit trails, and run regression checks whenever you change extraction logic, because a subtle prompt change can silently corrupt how facts get parsed.

Best-practice checklist before you ship
A memory system is production-ready when it passes these checks, not when it merely works in a demo.
- Precision and recall on fact retrieval measured against a labelled test set, not eyeballed.
- Latency SLAs defined per layer (profile lookups under 10ms, episodic retrieval under 100ms is a reasonable starting target).
- Token-cost ceilings per query, monitored continuously, not just benchmarked once at launch.
- Encryption at rest and in transit for all stored memory, with retention windows enforced automatically rather than manually.
- Supersession audit trail that shows every fact’s history, not just its current value.
- Integration testing against the actual CRM or backend systems the chatbot writes to, since memory that never reaches the CRM is just an expensive log file.
| Criterion | Acceptable target | Why it matters |
|---|---|---|
| Fact retrieval precision | High accuracy on a held-out test set | False facts erode user trust fast |
| Episodic retrieval latency | Under 100ms | Slower retrieval is felt immediately by users |
| Token budget per query | Fixed ceiling, monitored | Prevents cost creep as memory grows |
| Retention compliance | Enforced automatically | Manual enforcement fails under load |
How this maps to an Australia-hosted platform
Some platforms run contextual memory as a core capability of a private cloud platform, built specifically for enterprises that need multi-channel agents across voice, SMS, email, and live chat tied into existing CRM systems. Because some platforms host entirely within Australia, retention and supersession policies can map directly onto local data sovereignty requirements rather than being bolted on afterwards. Teams evaluating fit typically start with an architecture review before committing to a proof of concept.
What I’d actually build first
Skip the full augmentation pipeline on day one. Start with profile facts plus episodic retrieval only, get write validation deterministic and supersession chains working correctly, then layer in triple extraction and token-budgeted injection once you can measure their cost impact. Most teams over-engineer retrieval before they’ve proven their extraction pipeline writes clean, non-contradictory facts. Fix that first, and the retrieval half becomes far easier to tune. Measure token cost from week one, not after a complaint about the LLM bill.
— Sowrabh
Get a contextual memory architecture that fits your systems
Most teams building this from scratch spend months on infrastructure that some platforms already run in production, purpose-built for businesses that need multi-channel agents with memory that actually persists across sessions. 
Where a generic API stitched together in-house means owning every layer, embeddings, vector storage, profile tables, retention logic, and the compliance risk that comes with it, some platforms offer architecture already hosted entirely within Australia, integrated with CRM systems, and built for sectors where data sovereignty is a requirement. Dynamic agent training and real-time analytics can sit on top of the same contextual memory patterns covered above, without needing to build and maintain the plumbing yourself. If your team is weighing a build-versus-buy decision on persistent memory infrastructure, a production-focused AI automation partner can also help validate your architecture choices before you commit engineering time.
Request an architecture review through Conversationalai to see how your existing CRM and channels map onto a memory-enabled deployment.
Sources
- Memori: A Persistent Memory Layer for Efficient, Context-Aware LLM Agents (arXiv)
- Introducing Agent Memory | Cloudflare Blog
- agent-memory (GitHub)