# ReduceDocumentsChain

> **Class** in `langchain_classic`

📖 [View in docs](https://reference.langchain.com/python/langchain-classic/chains/combine_documents/reduce/ReduceDocumentsChain)

Combine documents by recursively reducing them.

This involves

- `combine_documents_chain`
- `collapse_documents_chain`

`combine_documents_chain` is ALWAYS provided. This is final chain that is called.

We pass all previous results to this chain, and the output of this chain is
returned as a final result.

`collapse_documents_chain` is used if the documents passed in are too many to all
be passed to `combine_documents_chain` in one go. In this case,
`collapse_documents_chain` is called recursively on as big of groups of documents
as are allowed.

## Signature

```python
ReduceDocumentsChain()
```

## Description

**Example:**

```python
from langchain_classic.chains import (
    StuffDocumentsChain,
    LLMChain,
    ReduceDocumentsChain,
)
from langchain_core.prompts import PromptTemplate
from langchain_openai import OpenAI

# This controls how each document will be formatted. Specifically,
# it will be passed to `format_document` - see that function for more
# details.
document_prompt = PromptTemplate(
    input_variables=["page_content"], template="{page_content}"
)
document_variable_name = "context"
model = OpenAI()
# The prompt here should take as an input variable the
# `document_variable_name`
prompt = PromptTemplate.from_template("Summarize this content: {context}")
llm_chain = LLMChain(llm=model, prompt=prompt)
combine_documents_chain = StuffDocumentsChain(
    llm_chain=llm_chain,
    document_prompt=document_prompt,
    document_variable_name=document_variable_name,
)
chain = ReduceDocumentsChain(
    combine_documents_chain=combine_documents_chain,
)
# If we wanted to, we could also pass in collapse_documents_chain
# which is specifically aimed at collapsing documents BEFORE
# the final call.
prompt = PromptTemplate.from_template("Collapse this content: {context}")
llm_chain = LLMChain(llm=model, prompt=prompt)
collapse_documents_chain = StuffDocumentsChain(
    llm_chain=llm_chain,
    document_prompt=document_prompt,
    document_variable_name=document_variable_name,
)
chain = ReduceDocumentsChain(
    combine_documents_chain=combine_documents_chain,
    collapse_documents_chain=collapse_documents_chain,
)
```

## Extends

- `BaseCombineDocumentsChain`

## Properties

- `combine_documents_chain`
- `collapse_documents_chain`
- `token_max`
- `collapse_max_retries`
- `model_config`

## Methods

- [`combine_docs()`](https://reference.langchain.com/python/langchain-classic/chains/combine_documents/reduce/ReduceDocumentsChain/combine_docs)
- [`acombine_docs()`](https://reference.langchain.com/python/langchain-classic/chains/combine_documents/reduce/ReduceDocumentsChain/acombine_docs)

## ⚠️ Deprecated

Deprecated since version 0.3.1.

---

[View source on GitHub](https://github.com/langchain-ai/langchain/blob/9f232caa7a8fe1ca042a401942d5d90d54ceb1a6/libs/langchain/langchain_classic/chains/combine_documents/reduce.py#L131)