# SummarizationToolMiddleware

> **Class** in `deepagents`

📖 [View in docs](https://reference.langchain.com/python/deepagents/middleware/summarization/SummarizationToolMiddleware)

Middleware that provides a `compact_conversation` tool for manual compaction.

This middleware composes with a `SummarizationMiddleware` instance, reusing
its summarization engine (model, backend, trigger thresholds) to let the
agent compact its own context window.

This middleware never compacts automatically. Compaction only occurs when
`compact_conversation` is called as a normal tool call (by the model or by
an explicit user action, e.g. as implemented in the deepagents-cli).

To avoid compacting too early, compact tool execution is gated by
`_is_eligible_for_compaction`, which requires reported usage to reach about
50% of the configured auto-summarization trigger.

The tool and auto-summarization share the same `_summarization_event` state
key, so they interoperate correctly.

For a simpler setup, use `create_summarization_tool_middleware` which
handles both steps.

## Signature

```python
SummarizationToolMiddleware(
    self,
    summarization: _DeepAgentsSummarizationMiddleware,
)
```

## Description

**Example:**

```python
from deepagents.middleware.summarization import (
    SummarizationMiddleware,
    SummarizationToolMiddleware,
)

summ = SummarizationMiddleware(model="gpt-4o-mini", backend=backend)
tool_mw = SummarizationToolMiddleware(summ)

agent = create_deep_agent(middleware=[summ, tool_mw])
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `summarization` | `_DeepAgentsSummarizationMiddleware` | Yes | The `SummarizationMiddleware` instance whose summarization engine this tool will delegate to. |

## Extends

- `AgentMiddleware`

## Constructors

```python
__init__(
    self,
    summarization: _DeepAgentsSummarizationMiddleware,
) -> None
```

| Name | Type |
|------|------|
| `summarization` | `_DeepAgentsSummarizationMiddleware` |


## Properties

- `state_schema`
- `tools`

## Methods

- [`wrap_model_call()`](https://reference.langchain.com/python/deepagents/middleware/summarization/SummarizationToolMiddleware/wrap_model_call)
- [`awrap_model_call()`](https://reference.langchain.com/python/deepagents/middleware/summarization/SummarizationToolMiddleware/awrap_model_call)

---

[View source on GitHub](https://github.com/langchain-ai/deepagents/blob/a827cddf72d72e4b17921b8eb445a3bfb0511cb4/libs/deepagents/deepagents/middleware/summarization.py#L1205)