No history yet

Stateful Retrieval Architectures

From Amnesia to Memory

Large Language Models have a fundamental problem: they're forgetful. By default, an LLM has no memory of past interactions. Each query is a fresh start, a blank slate. This stateless nature is a major hurdle for creating truly useful applications. You can't build a personalized assistant or a tool that understands your company's internal documents if it forgets everything you told it a moment ago.

The solution isn't to retrain the model with every new piece of information. That would be incredibly slow and expensive. Instead, we give the model a cheat sheet. This approach is called Retrieval-Augmented Generation, or RAG for short. It connects the LLM to an external knowledge base, allowing it to pull in relevant, up-to-date information on the fly to answer a specific query.

Lesson image

At its core, a RAG system has two main components. First, there's the retriever. Its job is to search the external data source—a collection of company documents, user chat history, or product manuals—and find the specific snippets of text that are most relevant to the user's current question. Second, there's the generator, which is the LLM itself. It takes the original query and the retrieved information and synthesizes a coherent, context-aware answer. The model doesn't rely solely on its pre-trained knowledge; it uses the provided documents as its source of truth.

Finding the Right Needle

The magic of RAG hinges on the retriever's ability to find the right information. A simple keyword search, like the one you'd use in a traditional search engine, often isn't good enough. It matches exact words but misses the underlying meaning. If your document says "revenue decreased by 10%," a keyword search for "poor sales performance" will come up empty.

This is where comes in. Instead of matching keywords, it matches concepts and intent. It achieves this by converting both the query and the documents into numerical representations called embeddings. These embeddings capture the semantic meaning of the text. When a user asks a question, the system converts it into an embedding and then searches for the document chunks with the most similar embeddings. This allows it to find "revenue decreased" when the user asks about "poor sales," because the concepts are semantically close.

Keyword search finds documents. Semantic search finds meaning.

Preparing the Knowledge Base

You can't just point a RAG system at a folder of random files and expect good results. The knowledge base must be carefully prepared through a process called data ingestion. The most critical step in this process is document chunking.

LLMs have a limited context window—a maximum amount of text they can consider at one time. You can't feed an entire 100-page manual into the prompt. Instead, you break the document into smaller, more manageable pieces, or "chunks." The quality of these chunks directly impacts the quality of the retrieval. If a chunk is too small, it might lack the necessary context. If it's too big, it might contain too much noise, confusing the model.

Chunking StrategyDescriptionBest For
Fixed-SizeSplits text into chunks of a set number of characters or tokens.Simple, uniform text documents.
RecursiveSplits text based on a hierarchy of separators (e.g., paragraphs, then sentences).Structured text like markdown or code.
Content-AwareSplits text based on its logical structure (e.g., chapters, sections, or slides).Highly structured documents like PDFs or presentations.

Choosing the right strategy is a balancing act. You're trying to create chunks that are self-contained enough to be understood on their own but also fit efficiently within the model's context window. This often involves trade-offs. For example, a fixed-size strategy is simple to implement but might awkwardly split a sentence in half. A content-aware approach is more precise but requires more effort to set up.

By moving from stateless, one-off queries to a stateful architecture powered by RAG, we transform general-purpose LLMs into specialized, knowledgeable assistants. They gain a memory, not by changing their core programming, but by being given the right information at the right time. This is the foundation for building applications that can remember, reason, and provide truly personalized interactions.

Quiz Questions 1/5

What fundamental problem in Large Language Models does Retrieval-Augmented Generation (RAG) directly address?

Quiz Questions 2/5

In a RAG system, what is the primary role of the 'retriever' component?