LangChain provides four main document chain strategies: Stuff (simple concatenation), MapReduce (parallel processing with summarization), Refine (iterative refinement), and MapRerank (scored selection). They differ in how they handle multiple documents, balancing token usage, latency, and quality.
LangChain's document chains are methods for processing multiple documents in a QA pipeline. The Stuff chain simply concatenates all documents into a single prompt, which is fast but limited by the LLM's context window. MapReduce processes each document in parallel (Map step) and then combines the results (Reduce step), suitable for large document sets but requires more API calls. Refine iteratively updates an answer by processing documents sequentially, preserving context but being slower. MapRerank runs an initial prompt to generate an answer and confidence score for each document, then selects the best one; it's useful when you need a single high-quality answer but can be expensive.[reference:0][reference:1]
Stuff: Use for < 5 short documents where total token count fits within the model's context window. Fastest, lowest cost, but limited capacity.
MapReduce: Use for large document collections (e.g., 20+ pages). Parallel processing reduces latency, but final answer may lose nuance across documents.
Refine: Use when sequential reasoning is important (e.g., legal analysis, complex research). Preserves context but has higher latency.
MapRerank: Use when you need the single best answer from a set of documents (e.g., fact lookup, customer support). Provides confidence scores but is expensive.