A retrieval-augmented generation pipeline combines a retriever, a prompt, and an LLM so the model answers questions grounded in your own data. The canonical LCEL pattern is to build a dict with the retriever piped to a format function and the original question carried through RunnablePassthrough, then pipe that dict into a prompt that fills the context and question slots, then into an LLM, and finally into an output parser. RunnablePassthrough is what carries the user's original question into the prompt alongside the retrieved context; the format function is usually a RunnableLambda that joins the page_content strings of the retrieved documents with double newlines. For chat with history, add a question rewriter step before the retriever that uses the chat history to produce a standalone search query — implemented as a RunnableLambda or a small prompt-chain whose output becomes the retriever's input. Beyond the basic retriever, ContextualCompressionRetriever wraps a base retriever with a DocumentCompressor — an LLM extractor like LLMChainExtractor, an EmbeddingsFilter that scores each chunk by similarity, or an EmbeddingsRedundantFilter that drops near-duplicates — to keep only the most relevant content.
Document processing strategies define how multiple retrieved chunks are combined into a single prompt. The stuff strategy, exposed via create_stuff_documents_chain, concatenates every retrieved document into one prompt as the context variable; it is the simplest and highest-quality option when the total context fits the window. Map-reduce generates an answer per document in parallel, then summarizes all answers; it scales to large corpora but loses cross-document reasoning. Refine processes documents sequentially, using each new document to update a running answer; it produces high-quality long-form synthesis but is linear in the number of documents. Map-rerank asks the LLM for an answer and a confidence score per document and returns the highest scorer — cheap and effective when the answer is concentrated in a single source. A useful post-processor in any of these pipelines is LongContextReorder, which rearranges documents so the most relevant ones sit at the start and end of the context, exploiting the lost-in-the-middle effect where LLMs pay more attention to context extremes.
Several retrieval enhancements complement these strategies. Cross-encoder rerankers such as CohereRerank and FlashrankRerank score each query and document pair jointly and are more accurate than bi-encoder similarity, so they are typically applied to the top-K candidates from a fast first-stage retriever. HyDE — Hypothetical Document Embeddings — asks the LLM to write a hypothetical answer, embeds that, and uses the resulting vector for retrieval; it often improves recall because the hypothetical answer lives in the same embedding region as the real documents. Finally, the older RetrievalQA and ConversationalRetrievalChain classes still work but are considered legacy; in modern code you express the same flows in LCEL or in a LangGraph graph, which gives you more control over the steps and the state.