Engineering

Staging context: how RAG answers follow-ups without re-reading the library

Learn how smart search fallbacks and self-summarizing context windows keep agent Q&A affordable, accurate, and on-topic.

An AI agent that re-scans an entire knowledge base on every follow-up question quickly becomes slow and inefficient. When an agent is conversing with a user, a question like “and what did it say about pricing?” should intuitively build upon what was already referenced in the active thread.

Here is how a staged retrieval architecture keeps agent context sharp, accurate, and cost-efficient.

Follow-up Stage 1: Cited Scope Search active sources MATCH Fast Return (0.1x ms) MISS Stage 2: Full Library Broad recall Agent Context

Staged Retrieval: Cite Before You Scan

In conversational RAG, rather than immediately searching the entire corpus on every prompt, we evaluate context in stages:

  1. Stage 1 (Cited Context): Search only across the documents and notes cited in the active conversation thread.
  2. Stage 2 (Corpus Fallback): If and only if the query cannot be resolved with high confidence in the existing context, escalate to a full library search.

This tiered approach provides two major advantages:

  • Zero Latency on Follow-ups: Answering questions regarding previously cited materials avoids repetitive multi-thousand-document scans.
  • Contextual Coherence: The agent prioritizes consistency with the narrative established in the current session.

High-Level Algorithm

function RetrieveRelevantContext(query, activeSession):
    # Step 1: Check active session citations first
    citedDocumentIds = activeSession.getCitedDocuments()
    
    if citedDocumentIds.isNotEmpty():
        localMatch = SearchEngine.queryWithinScope(query, scope=citedDocumentIds)
        if localMatch.confidenceScore >= HIGH_CONFIDENCE_THRESHOLD:
            return localMatch
            
    # Step 2: Fall back to full library search
    globalResults = SearchEngine.queryFullCorpus(
        query, 
        modes=[EXACT_MATCH, FUZZY_PREFIX]
    )
    
    # Step 3: Register newly discovered citations
    activeSession.registerCitations(globalResults.documentIds)
    
    return globalResults

Rolling Memory Windows & Summarization

To maintain infinite conversation length without exceeding prompt token budgets:

  • Active Raw Turns: The most recent dialogue turns are supplied verbatim to preserve precise conversational nuance.
  • Incremental Summarization: Older dialogue history is asynchronously compressed into structured background summaries.

This guarantees that an extended 100-turn agent session consumes predictable token volume while retaining critical prior decisions and factual grounding.

All field notes

Kiomon Engineering

We build persistent context, knowledge, and memory infrastructure for AI agents over native MCP.

Give your AI agent a brain that remembers

Stream persistent context to Claude Code, Cursor, and any MCP agent with zero setup friction.

Get started free View docs