Concepts / Memory Systems for Language Models

Memory Systems for Language Models

A RAG system works in two steps: it first retrieves relevant information from external memory, then uses that information to generate a response.

  • Programming

Why a Model Needs Outside Memory

A language model answers questions using two sources: what it absorbed during training, and whatever text appears in its current input. Neither source is ideal. Training knowledge goes stale, because the world keeps changing after training ends. The input also has a limited size, so a model cannot simply be handed an entire company's documents and asked a question about them. Retrieval-Augmented Generation, usually shortened to RAG, is a design pattern that gets around both limits. Instead of relying on memory alone, the system first looks up material that might be relevant and then places that material in front of the model.

This article focuses on the first half of that arrangement, the retriever. The quality of a RAG system is tied closely to how well the retriever does its job, so understanding how retrievers decide what is relevant is the key to understanding RAG itself.

The Two Steps of a RAG System

A RAG system works in two stages. In the first stage, it retrieves information from an external memory, which is a store of documents, passages, or records that exists outside the model. In the second stage, it passes the retrieved material to the model, which uses it to generate a response. The retrieval stage supplies the facts, and the generation stage turns those facts into an answer.

User queryRetrieversearches external memoryExternal memorydocuments, passages,recordsRetrieved materialpassed to the modelLanguage modelgenerates the responseAnswerquerysearchesmatching itemsplaced in inputgenerates
The user's query goes to the retriever, which searches external memory. The retrieved text then reaches the language model, which produces the answer.

Tracing the flow step by step: the query enters the retriever, the retriever searches the external memory and selects material that might be relevant, that material is placed in the model's input, and the model generates the answer from it. The stated expectation of this design is that the answer will be more accurate than what the model could produce from its own memory, because the answer is anchored to material the system actually found.

How a Term-Based Retriever Decides Relevance

A term-based retriever decides relevance by looking at the words themselves. A query is broken into its terms, and the retriever favors documents that contain those same terms. Documents where the matches are strong receive more weight. Systems such as Elasticsearch and the BM25 ranking method are examples of this approach.

refundDoc 1, Doc 3policyDoc 1, Doc 2shippingDoc 2shared document Doc 1
Each query term points to the documents that contain it. Documents that match more of the query's terms rise in the relevance ranking.
Worked example

Ranking Documents by Term Matches

A user asks: What is the refund policy? Three stored documents are available. Doc 1 contains refund and policy. Doc 2 contains policy and shipping. Doc 3 contains refund and timing. Which document should a term-based retriever favor?

  1. 1
    Break the query into terms

    The query contains the terms refund, policy, and the rest are common words that do not add much. The key terms are refund and policy.

  2. 2
    Check which documents contain those terms

    Doc 1 contains both refund and policy. Doc 2 contains only policy. Doc 3 contains only refund.

  3. 3
    Compare the strength of the matches

    Doc 1 matches both query terms, so its match is the strongest of the three.

  4. 4
    Rank the documents

    The retriever places Doc 1 first, followed by Doc 2 and Doc 3, which each match one term.

Result

Doc 1 is favored because it contains the most query terms. The retriever never checked what the documents mean, only which words they share with the query.

The main practical advantage of term-based retrievers is that they are much lighter to set up and run. They often give a solid starting point against which more advanced retrievers can be measured.

Vector Search in an Embedding-Based Retriever

An embedding-based retriever takes a different route. Rather than comparing words directly, it relies on vector search, in which items are represented as vectors and the system looks for the ones closest to the query's vector. Because the comparison happens in this representation space, the approach has the potential to recognize relevance even when the wording differs. This is why embedding-based retrievers can outperform term-based algorithms.

Query vectorDocument Aclose to the queryDocument Bclose to the queryDocument Cfarther from the querynearestnearestless near
The query and each document become points in a shared vector space. The system returns the documents whose points sit closest to the query point.

Vector search is not a technique invented only for language models. It is also the backbone of many core internet applications, including search and recommender systems. Many vector search algorithms developed for those applications can be reused in RAG, so the retrieval side of a RAG system can draw on a large body of existing work.

Comparing the Two Retrievers

The two families differ along several dimensions at once: what they match on, how much effort they require, and where their advantages appear. Placing them side by side makes the choice easier to reason about than reading about each separately.

DimensionTerm-based retrieverEmbedding-based retriever
What it matches onShared words between query and documentsCloseness of query vector and document vectors
Effort to runMuch lighter to set up and runMore computationally intensive per lookup
Typical roleStrong baseline to measure againstPotential to outperform term-based methods
Handling different wordingDepends on vocabulary overlapHas the potential to recognize relevance across different wording
Named examples in sourceElasticsearch, BM25Vector search methods used in RAG

Term-based and embedding-based retrievers, compared on the dimensions the source describes

What do you think happens?

If a team has limited computing resources and wants a simple, fast retrieval baseline, which family should it start with?

  • Term-based retriever

  • Embedding-based retriever

Reveal answer
Answer:

Term-based retriever

The source describes term-based retrievers as much lighter to implement and run, and as providing strong baselines. Embedding-based retrieval is more computationally intensive.

Mistakes Beginners Make About Retrieval

  • Common mistake

    Assuming a RAG system's answer is correct because the model sounds confident

    What it looks like

    A model answers from its training memory when the retriever found nothing useful, and the answer reads smoothly.

    Why it fails

    The expected accuracy gain comes from the answer being anchored to material the system actually found. Without good retrieved material, that anchor is missing.

    Do this instead

    Check what the retriever returned. The retriever's output is the foundation of the answer.

  • Common mistake

    Thinking a term-based retriever understands meaning

    What it looks like

    A query about a refund uses different words from a document that answers it, and the document is not found.

    Why it fails

    A term-based retriever decides relevance by the words themselves, so it depends on vocabulary overlap.

    Do this instead

    Recognize that term matching only works as well as the shared vocabulary allows.

  • Common mistake

    Choosing embedding-based retrieval by default without considering cost

    Why it fails

    Embedding-based retrieval is more computationally intensive, and term-based methods are lighter and often give a solid starting point.

    Do this instead

    Start with a term-based baseline and move to embedding-based retrieval when its potential advantage justifies the extra running cost.

  • Common mistake

    Treating the generator as the main place to fix poor RAG results

RAG as a Special Case of an Agent

RAG can be understood as a special case of an agent in which the retriever is one of the tools the model is allowed to use. Both RAG and agentic designs let a model get past its context limit and stay current, because both bring in outside information. The agentic pattern goes further. An agent is defined by its environment and by the tools it can access. Inside an AI-powered agent, the model acts as the planner: it analyzes the task, considers several possible solutions, and selects the most promising one.

Planning matters because a complex task may need many steps, and a model must be powerful enough to plan them. The planning ability can be strengthened with reflection and with a memory system that keeps track of progress. Adding tools also adds capability: the more tools a model is given, the more capabilities it has, which lets it take on harder tasks.

RAG and agents share one more feature. Both are prompt-based methods. They shape the model's output solely through what goes into its input, without changing the model's internal weights. That is what separates them from approaches that modify the underlying model itself.

Practice: Choosing and Explaining a Retriever

MEDIUM

A small company wants its support assistant to answer questions from its internal policy documents. Users sometimes phrase questions in very different words from the documents. Decide which retriever family you would start with, explain how it decides which documents are relevant, and state what the retriever's quality means for the final answer.

Hints
  • Consider what a term-based retriever matches on and what happens when the wording differs.
  • Consider what an embedding-based retriever matches on and what it costs to run.
  • Remember that the generator can only work with what the retriever supplies.
EASY

Name the two stages of a RAG system in order, and say in one sentence what each stage contributes to the final answer.

Hints
  • The first stage finds material outside the model.
  • The second stage turns that material into a response.

Key Takeaways

  • A RAG system works in two steps: it first retrieves relevant material from an external memory, then passes that material to the language model to generate the response.
  • A term-based retriever such as Elasticsearch or BM25 judges relevance by shared query terms. It is lighter to run and gives a strong baseline, but it depends on vocabulary overlap.
  • An embedding-based retriever uses vector search to find items whose vectors lie closest to the query vector. It can recognize relevance across different wording, but it is more computationally intensive.
  • The retriever largely determines RAG quality, because the generator can only work with what the retriever supplies.
  • RAG is a special case of an agent in which the retriever is a tool. More automation and more tools bring more capability, along with greater failure and security risk.

Related concepts

More concepts starting with M