A practical guide to combining Cache-Augmented Generation (CAG) and Retrieval-Augmented Generation (RAG), covering system architecture, hybrid search, prompt caching, implementation steps, and performance evaluation through a real pharmaceutical AI use case.

This article explains how Cache-Augmented Generation (CAG) and Retrieval-Augmented Generation (RAG) work together to improve AI-powered document search and question answering. Using AskGMP, a pharmaceutical regulatory chatbot, as a real-world example, it covers the system architecture, hybrid retrieval, prompt caching, citation validation, and step-by-step implementation. It also compares CAG + RAG with traditional RAG using real benchmark results, highlighting the trade-offs in accuracy, latency, and cost.
The Problem with Traditional RAG
Traditional Retrieval-Augmented Generation (RAG) depends on retrieval rankings, chunk boundaries, and context limits, meaning relevant information can exist in the knowledge base but never reach the LLM.
Combining Cache-Augmented Generation (CAG) with RAG addresses this limitation by keeping important documents directly available in the model's context while using retrieval for the remaining knowledge base.
Key Differences Between Traditional RAG and CAG + RAG
Traditional RAG retrieves relevant document chunks for each question, while CAG + RAG keeps selected high-value documents directly in the model's context and retrieves additional information when needed. The table below compares their main characteristics.
Traditional RAG | CAG + RAG | |
Core documents | Retrieved when ranked highly | Always available in context |
Other documents | Retrieved dynamically | Retrieved dynamically |
Retrieval dependency | All documents depend on retrieval | Primarily the remaining documents |
Context size | Usually smaller | Larger, with a cached prefix |
Main advantage | Scalability and flexibility | Reliable access to selected core documents |
Main trade-off | Relevant documents may be missed | Larger context and cache-write costs |
Note: CAG doesn't replace RAG. It reduces dependence on retrieval for important documents while retaining RAG's flexibility for the rest of the knowledge base.
The architecture combines two complementary approaches that come together in the LLM's context window.
CAG keeps core regulatory documents in a cached context, while RAG retrieves additional evidence relevant to each question.
The LLM combines both to generate an answer, and citation validation verifies the document references and page numbers before displaying the result.
How It Works
The user submits a question.
Relevant passages are retrieved from the knowledge base.
Core cached documents and retrieved evidence are combined in the prompt.
The LLM generates an answer with citations.
The citations are validated.
The final answer and supporting sources are displayed.
.
2. Implementation Guide to Build a CAG + RAG System
The following steps outline how to implement a CAG + RAG architecture using your own document collection, from data preparation to grounded answer generation.
Before implementing CAG or RAG, the first step is to collect and organize your documents.
For AskGMP, I collected approximately 90 public pharmaceutical regulatory documents from sources such as EudraLex, ICH, FDA, EMA, MHRA, PIC/S, and WHO. I then created a document registry to manage each document and its metadata.
A typical document registry includes:
Field | Purpose |
document_id | Unique document identifier |
document_title | Document title |
authority | Publishing organization |
jurisdiction | Applicable region |
reference_code | Official document reference |
status | Current, superseded, or withdrawn |
document_url | Original source URL |
local_path | Local file location |
Next, extract the content from your documents using tools appropriate for each file format. For example, pymupdf4llm can extract text from PDFs while preserving headings and document structure.
Split the extracted content into meaningful chunks (e.g., around 1,000 tokens), ensuring that each chunk retains its document ID, section, and page number.
Preserving this metadata is essential for generating traceable answers with accurate document references later in the pipeline.
Once the documents are extracted and chunked, the next step is to index them in a vector database. For AskGMP, I used Qdrant with hybrid retrieval, combining two search approaches:
Dense Vectors: Semantic Search
Dense embeddings capture the meaning of text, allowing the system to find relevant information even when the user's question uses different wording. For example, text-embedding-3-large can be used to generate these embeddings.
Sparse Vectors: Keyword Search
Sparse vectors complement semantic search by matching specific terms, technical vocabulary, and document identifiers. This is particularly useful when exact terminology matters.
Reciprocal Rank Fusion (RRF)
Dense and sparse searches produce separate ranked results. RRF combines these rankings based on document positions rather than raw similarity scores, improving the balance between semantic relevance and exact keyword matches.
Finally, apply metadata filters and diversify the results to avoid retrieving too many similar chunks from the same document.
At this point, we have a functional hybrid RAG pipeline. However, important documents can still be missed during retrieval, which is where CAG becomes useful.
The next step is to select a set of high-value, frequently referenced documents that will remain available in the LLM's context without depending on retrieval rankings.
For AskGMP, I selected 15 regulatory documents, including EU GMP chapters, FDA regulations, and ICH guidelines. Together, they account for approximately 133,000 tokens.
To select core documents, prioritize documents based on their relevance to common questions, frequency of use, and observed retrieval failures. The size of the core library should also fit within the model's context window.
To build a deterministic core, load the selected documents in a fixed order, preserve their source identifiers, and assemble their content consistently.
This is essential for prompt caching: even small changes to the cached prefix can cause a cache miss, requiring the model provider to process and cache the content again.
The result is a stable knowledge layer that can be reused across questions, while RAG handles the remaining documents dynamically.
Keeping a large document collection in the LLM's context can be expensive. Prompt caching reduces the cost of repeatedly processing the same content.
To structure the prompt, divide the prompt into two parts:
Static context: System instructions, CAG core documents, and stable source identifiers.
Dynamic context: Retrieved RAG passages, uploaded evidence (if applicable), and the user's question.
Place the cache breakpoint (cache_control) at the end of the static block. Keeping this prefix identical across requests allows the model provider to reuse cached content.
Cold vs. Warm Requests
A cold request processes and caches the full context. A warm request reuses that cached prefix, reducing processing costs.
In AskGMP, the observed approximate costs were:
Request Type | Approximate Cost |
Cold CAG + RAG question | $0.36 |
Warm CAG + RAG question | $0.04 |
Traditional RAG question | $0.03 |
These costs are specific to this implementation and depend on the model, provider, cache duration, and token usage.
When using an API gateway such as OpenRouter, consistent provider routing (e.g., using a session_id) can improve cache reuse. Monitor cached-token usage to verify that requests are actually benefiting from caching.
Remember that cached context still occupies the model's context window, and cache entries expire according to the provider's settings.
Note: CAG + RAG is not necessarily cheaper than traditional RAG. Its value comes from keeping important documents consistently available to the model, while prompt caching helps control the additional cost.
At this stage, the LLM receives context from two sources:
CAG: Core documents already available in the cached prompt.
RAG: Relevant passages dynamically retrieved based on the user's question.
The goal is to combine both efficiently without introducing unnecessary duplication.
Avoid Duplicate Content
Run hybrid retrieval across the knowledge base, then exclude chunks belonging to documents already included in the CAG core. This prevents sending the same content twice and reduces unnecessary token usage.
The remaining retrieved passages are added to the dynamic prompt, while the cached core remains unchanged.
Respect Retrieval Filters
User-selected filters, such as document type, authority, or jurisdiction, must also apply to the final context.
For example, AskGMP falls back to filtered RAG when an authority filter conflicts with its mixed-authority CAG library.
Important: CAG ensures important documents are available, while RAG provides question-specific evidence. Combining them effectively requires deduplication and consistent filtering.
The LLM should generate answers grounded in the provided context, with document references and page numbers supporting its claims.
Citation Validation:
Assign stable identifiers to each source and instruct the LLM to use them in its responses. After generation, validate citations programmatically against the supplied evidence, rejecting invented or incorrect references.
Source Transparency
Display the supporting documents in a structured table containing the document title, reference, page numbers, and source link.
Citation validation makes answers traceable, but it does not guarantee factual accuracy. For high-stakes applications, human review remains essential.
To evaluate the architecture, I tested AskGMP with 12 predefined regulatory questions, comparing traditional hybrid RAG against CAG + RAG using Claude Sonnet via OpenRouter.
The benchmark includes questions targeting core cached documents, dynamically retrieved documents, and both. Each question is evaluated using three automated metrics:
Document hit: Whether the expected source document was cited.
Fact coverage: Whether all predefined key facts appeared in the answer.
Rejected citations: Number of invented references detected by the citation validator.
Latency and API costs were also measured.
Benchmark Results:
Metric | CAG + RAG | Traditional RAG |
Correct document cited | 12/12 | 10/12 |
All expected facts present | 12/12 | 11/12 |
Invented citations detected | 0 | 0 |
Average latency | 13.1 s | 12.0 s |
Total cost (12 questions) | $0.81 | $0.39 |
Results and Real-World Potential
CAG + RAG achieved better document coverage and fact completeness in this benchmark, with slightly higher latency and approximately twice the cost.
The benefits could be even greater in certain real-world workflows, where users repeatedly ask questions about a relatively small set of important documents. In such cases, CAG can reduce retrieval misses, while frequent cache reuse can lower the average cost per request.
However, this small benchmark does not guarantee better performance across all workloads. Larger evaluations with representative real-world questions are needed to establish accuracy, reliability, and cost-effectiveness.
CAG + RAG is particularly useful when a relatively small set of important documents is frequently referenced, the content remains stable, and missing relevant information during retrieval can significantly affect answer quality.
Potential applications include pharmaceutical regulatory knowledge, legal research, technical documentation, and enterprise knowledge management.
However, traditional RAG may be more suitable when documents change frequently, the knowledge base is too large to cache, or relevant information varies widely between questions.
The right architecture depends on your data, retrieval requirements, context size, and cost constraints.
The AskGMP implementation demonstrates how CAG and RAG can complement each other rather than compete.
CAG keeps essential knowledge available, RAG retrieves question-specific information, and citation validation makes the supporting references traceable.
The goal isn't to provide the LLM with more context, but to provide the right evidence, at the right time, at an acceptable cost.