RAG (Retrieval-augmented generation)

RAG, retrieval-augmented generation, is a method that finds relevant documents for a request and adds them to the prompt of a language model before it answers.

RAG stands for retrieval-augmented generation. It combines a search step with a language model. When a request arrives, the system searches a collection of documents for the parts that match it. It adds those parts to the prompt, so that the model answers from them instead of only from what it learned in training.

The documents are usually split into small chunks before they are searched. Each chunk is often turned into an embedding, a list of numbers that captures its meaning, and stored in a vector database. A request is turned into an embedding in the same way, and the closest chunks are returned. Keyword search is also common, alone or together with embeddings.

RAG lets a model use private or recent information without retraining it, and lets answers point to their sources. Its quality depends on the search, because the model cannot use a document that was not found. Agents often go further and search as a tool, choosing their own queries and reading files step by step. An agent with a sandbox can search files with ordinary commands, such as grep, as one way to retrieve.