Skip to content

CAG: realize KV-cache reuse through CAGRouter (prompt-prefix / generation wiring) #1018

Open

Description

@AmitoVrito

Follow-up from the #1010 (#1000) review.

CAGRouter.retrieve() loads the persisted KV state (load_state) and returns the corpus as context, but never generates with it. The loaded state only accelerates a subsequent generation whose prompt token-prefix matches the raw corpus_text. A typical RAGPipeline wraps context in a system prompt / chat template, so the prefix diverges and llama.cpp re-evaluates from the divergence point — the "warm cache" speedup is reduced or lost.

Additionally, benchmarks/cag_vs_rag_bench.py measures backend.generate_with_cache directly, not the router path, so its "CAG warm" numbers do not reflect what a CAGRouter user gets through retrieve().

Options to consider:

  • Wire generation through the router (e.g. a generate()/stream() on CAGRouter that builds the corpus-prefixed prompt and calls generate_with_cache), so the cache benefit is realized end-to-end.
  • Or keep the router retrieval-only and document that callers must generate against the corpus-prefixed prompt to benefit (a limitation note was added to docs/rag/cag.md at merge).
  • Update the benchmark to measure the router path so the published numbers are representative.

Minor related gaps from the same review: KVCacheStore has no eviction/size bound (llama states are large), and CAGRouter does not implement retrieve_mmr.

Activity

  1. added
    enhancement
    on Sep 3, 2026
  2. added a commit that references this issue on Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions