← All transcripts

CAG vs Long Context: How AI Models Use and Remember Information Transcript, AI Summary & Key Points

IBM Technology · May 21, 2026 · Education · 10:59 · EN

📄 Transcript

Searchable transcript of CAG vs Long Context: How AI Models Use and Remember Information — IBM Technology (10:59). Search for a phrase, then click its timestamp to jump straight to that moment in the video.

Captions sourced from the original video on YouTube, published by IBM Technology. The video, its captions and all related intellectual property remain the property of their respective owners; AINotes claims no ownership. Provided for research, accessibility and search — see the Transcript Notice and Copyright Policy.

00:00 Long context and cache augmented generation are two ways to give a large language model access to external knowledge and they actually build on each other in a way that's worth understanding. So an LLM, a large-language model, it only knows what was in its training data. So if it needs to reason over some data that's in a private document or maybe it needs to take a look at quarters financials, well it needs a way to access that information at inference time.

00:37 Now most people have heard of RAG, Retrieval Augmented Generation. My goodness, I feel like we've covered RAG a few times before. Now RAG solves this problem with a retrieval pipeline, meaning using things like vector databases and embedding models. But CAG and long context are two other ways to provide an AI model with this external knowledge. So how do they work?

01:07 Well, it doesn't get much more simple than long context, which is basically to say, just stuff everything into the context window. Send it into the prompt, send it to the AI model that way. Now that's simple, but will everything fit? Well, context windows have been growing pretty fast. So let me illustrate that with a quick diagram. So on this axis here, we're gonna put the size of the context window and then this axis there, this will just be time.

01:41 Okay, so let's kind of map this out. So if we start in 2020, GPT-3, that could handle thousand tokens. That's maybe 10 pages of text. If we go forward to 2023, well that's where GPT-4 Turbo pushed that quite significantly to 128,000 tokens, which is roughly 300 pages. Then by 2024 Google Gemini 1.5 Pro came along. What do we have here? Two million tokens, that's maybe 20 full length novels, and the trend is still climbing.

02:32 So the strategy becomes, if the context window is big enough, then just skip the retrieval pipeline entirely. So we just get all of our stuff, we get all our documents that we want to provide as external knowledge, and they go into the prompt along with the query that we want to send to the large language model and it's all stored in this context window.

03:00 We let the AI model just read the entire thing because we've got such a big context window to go from now and then a response comes out from that model. Now for certain workloads this works really really well. The model has access to everything so there's no risk of the- retrieval step, pulling the wrong documents, or missing something relevant, like rag might do.

03:26 But there are costs. Literal costs. Because pricing for most LLM APIs scales with token counts. So if this is 200,000 tokens of context, you're going to have to pay that on every single query, and that can add up pretty fast, as does latency. A large context can more processing time per response. Then there's a subtler problem as well, LLMs have what's called the lost in the middle effect.

03:55 So performance at the beginning of a context window, well that generally is is pretty strong, and then information at the end can generally be retrieved as well, but the information buried in the Middle of the long context window the accuracy can drop significantly. The model attends to the edges better than it does the center. But maybe the biggest issue is that every query reprocesses all of these documents from scratch.

04:28 So 10 questions about the same set of documents mean the model reads these documents 10 times. Which raises a pretty obvious question. What if the model could read the documents just once and then remember them? Well, that's the idea behind Cache Augmented Generation, or CAG, and the key to understanding it is something called Key Value Cache. So Key Value When a large language model processes text, each layer of the transformer computes what are called key and value matrices.

05:11 And these are basically the model's working memory and they represent how the model has understood and encoded everything it's read so far. Now, normally these get computed fresh on every single request, but Cag says, do it once and reuse the result. Now we can think about how CAG works in three different phases. And phase number one, that's knowledge preparation.

05:40 So the relevant documents, they get created and formatted to fit within the model's context window. And those documents can be whatever you like, company policies, product documentation, whatever the knowledge base is. Then in phase two, we move on to pre-computation, which is to say, the model processes all of those documents and generates the KV cache, which is the internal representation of everything it's read.

06:07 And then that cache gets saved somewhere. So it's persisted. So to disk or to memory. And then phase number three, that's actually the inference phrase. So what happens here is a, you know, a query comes in and it's sent into an AI model to process that query. Then instead of rereading all of the documents, the model just loads this pre-computed KV cache, it appends the new question and then it generates the answer coming out.

06:43 So the heavy computation already happened in phase two, so phase three here is going to be a lot faster. And in fact Research has showed something like a 10x speedup. When we use small data sets in the KV cache and even more with larger ones, something like 40X compared to reprocessing the full context every time. Now, CAG does have limitations and the obvious one is that the entire knowledge base still has to fit within the context window and when the source documents change, well, the entire KVCache has to be

07:18 recomputed. So if the data changes frequently. The cost of constantly re-caching starts in, it starts to eat into any latency savings that you are making. So really, CAG works best when the knowledge base here that it's using is stable. So the difference between long context and CAG, well, it comes down to when the computation happens. So with long context, the model has to process every document that we've put into the context window.

07:54 It needs to do it. Every time on every query that comes in. I mean, it's simple to set up, there's nothing to manage, but the cost and the latency hit come on every inference. With CAG, the model processes all of those same documents, but it only does it once during pre-computation. It saves that KV cache, and then each subsequent query just loads the cache and answers the question.

08:24 Now the first query is just as expensive as long context, but the second query, the tenth query, the hundredth query, that's where tag starts paying off. Now, long context is also a really good fit for a specific type of query, and that is. Queries where we just call something one time, so analyzing a single document, maybe answering a couple of questions about it.

08:51 There's no point pre-computing a cache for something that's only going to be queried one time. Whereas CAG is much better if those requests happen over and over. So repeated queries are really where CAG shines against a stable knowledge base, so an HR chat bot answering questions about company policies, for example, because the knowledge doesn't change very often and the cache stays valid.

09:18 So every query after the first one is gonna be fast and cheap. Now, there is one more piece to this that makes CAG a practical thing to use in the real world. That is called prompt caching. Now major LLM providers now offer prompt cashing as a feature of their APIs. And the idea is essentially CAG as a service which is not a real acronym but that's what it is.

09:46 So you send a long system prompt containing all the documents and the provider handles the KEV cash management behind the scenes. The subsequent requests that share the same prompt prefix they skip the document processing entirely, and the the economics of this are pretty significant because cash reads can come at a big discounts, something like a 90% discount compared to processing those tokens fresh.

10:15 So prompt caching takes what was an interesting research idea, which is CAG, and it turns it into something any developer can use without having to manage the cache infrastructure themselves. So an entire video about giving models external knowledge, and we didn't build a single retrieval pipeline using RAG. Whether RAG is still needed after all of this? Well, that's literally the title of another video on this channel. Go check that one out.

🧠 AI Summary

Long context and Cache Augmented Generation (CAG) both give language models access to external knowledge. Long context places all documents directly in the prompt and works well for one-off analysis, but it incurs processing costs and latency on every query and can suffer from the lost-in-the-middle effect. CAG processes documents once, saves the resulting Key Value Cache, and reuses it for later queries, making it faster and cheaper for repeated questions over stable knowledge bases. Prompt caching makes CAG-like behavior available through major LLM APIs, with cache reads potentially costing 90% less than processing tokens fresh.

🔑 Key Points

  • Long context gives an LLM external knowledge by placing all relevant documents into its context window.
  • Context windows expanded from GPT-3 handling thousand tokens in 2020 to GPT-4 Turbo handling 128,000 tokens in 2023 and Google Gemini 1.5 Pro handling two million tokens in 2024.
  • Long context avoids retrieval errors but increases token costs and latency because documents are processed on every query.
  • The lost-in-the-middle effect means models generally retrieve information from the beginning and end of a long context more accurately than information in the middle.
  • CAG precomputes and persists a Key Value Cache representing the model's processing of a knowledge base.
  • CAG is most effective for stable knowledge bases that receive repeated queries.
  • Prompt caching provides CAG-like cache management through LLM APIs and can substantially reduce the cost of repeated prompt prefixes.

✅ Actionable items

  • Use long context for one-time tasks such as analyzing a single document or answering a small number of questions.
  • Prepare and format stable source documents so they fit within the model's context window before using CAG.
  • Precompute the KV cache from the prepared documents and persist it to disk or memory.
  • For each subsequent query, load the saved KV cache, append the question, and generate the answer without rereading the documents.
  • Use prompt caching when an LLM provider supports it to avoid managing cache infrastructure directly.

🧭 Frameworks

Cache Augmented Generation05:29
  1. Prepare and format relevant documents to fit within the context window.
  2. Process the documents and generate the KV cache.
  3. Persist the cache to disk or memory.
  4. Load the cache for incoming queries, append the question, and generate the response.
Long context01:07
  1. Collect the external documents.
  2. Place the documents and query into the model's context window.
  3. Have the model process the full context and generate a response.

🧰 Tools & AI usage

  • Vector databases — Support the retrieval pipeline used by Retrieval Augmented Generation.00:49
  • Embedding models — Support document retrieval in Retrieval Augmented Generation pipelines.00:49
  • Key Value Cache — Store the model's internal representation of processed documents for reuse across queries.00:49
  • Prompt caching — Reuse processed prompt prefixes through LLM provider APIs.09:29

AI is used for

  • Provide external knowledge to a large language model at inference time — Enable reasoning over private documents, company policies, product documentation, or financial data outside the model's training data.00:13

📊 Numbers mentioned

Costs

  • A 200,000-token context is charged on every query when using long context.
  • CAG requires recomputing the entire KV cache when source documents change.

Growth

  • GPT-3 handled thousand tokens in 2020.
  • GPT-4 Turbo handled 128,000 tokens in 2023.
  • Google Gemini 1.5 Pro handled two million tokens in 2024.
  • CAG research showed approximately a 10x speedup with small cached datasets and approximately 40x with larger datasets compared with reprocessing the full context.

Pricing

  • Long-context API costs scale with token counts and require payment for the full context on every query.
  • Prompt cache reads can have a 90% discount compared with processing the tokens fresh.

⚖️ Advantages, risks & lessons

Advantages

  • Long context is simple to set up and requires nothing to manage.
  • Long context gives the model access to all supplied documents and avoids retrieval errors from selecting the wrong or incomplete documents.
  • CAG reduces repeated computation by reusing a precomputed KV cache.
  • Prompt caching makes CAG-like functionality accessible without directly managing cache infrastructure.

Risks

  • Long-context processing can increase API costs and latency.
  • Long contexts can suffer from the lost-in-the-middle effect.
  • CAG requires the entire knowledge base to fit within the context window.
  • Frequently changing source documents can eliminate CAG's latency savings because the KV cache must be recomputed.

Lessons

  • Choose long context when documents will be queried only once or a small number of times.
  • Choose CAG when a stable knowledge base will receive repeated queries.
  • The main difference between long context and CAG is when document processing occurs: every query for long context versus once during precomputation for CAG.

💬 Quotes

The model attends to the edges better than it does the center.

It concisely describes the lost-in-the-middle limitation of long context.04:21

👤 People & companies

Google

Company associated with the Gemini 1.5 Pro model.

02:14