# ContextEditingMiddleware

> **Class** in `langchain`

📖 [View in docs](https://reference.langchain.com/python/langchain/agents/middleware/context_editing/ContextEditingMiddleware)

Automatically prune tool results to manage context size.

The middleware applies a sequence of edits when the total input token count exceeds
configured thresholds.

Currently the `ClearToolUsesEdit` strategy is supported, aligning with Anthropic's
`clear_tool_uses_20250919` behavior [(read more)](https://platform.claude.com/docs/en/agents-and-tools/tool-use/memory-tool).

## Signature

```python
ContextEditingMiddleware(
    self,
    *,
    edits: Iterable[ContextEdit] | None = None,
    token_count_method: Literal['approximate', 'model'] = 'approximate',
)
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `edits` | `Iterable[ContextEdit] \| None` | No | Sequence of edit strategies to apply.  Defaults to a single `ClearToolUsesEdit` mirroring Anthropic defaults. (default: `None`) |
| `token_count_method` | `Literal['approximate', 'model']` | No | Whether to use approximate token counting (faster, less accurate) or exact counting implemented by the chat model (potentially slower, more accurate). (default: `'approximate'`) |

## Extends

- `AgentMiddleware[AgentState[ResponseT], ContextT, ResponseT]`

## Constructors

```python
__init__(
    self,
    *,
    edits: Iterable[ContextEdit] | None = None,
    token_count_method: Literal['approximate', 'model'] = 'approximate',
) -> None
```

| Name | Type |
|------|------|
| `edits` | `Iterable[ContextEdit] \| None` |
| `token_count_method` | `Literal['approximate', 'model']` |


## Properties

- `edits`
- `token_count_method`

## Methods

- [`wrap_model_call()`](https://reference.langchain.com/python/langchain/agents/middleware/context_editing/ContextEditingMiddleware/wrap_model_call)
- [`awrap_model_call()`](https://reference.langchain.com/python/langchain/agents/middleware/context_editing/ContextEditingMiddleware/awrap_model_call)

---

[View source on GitHub](https://github.com/langchain-ai/langchain/blob/8fec4e7ceee2c368b068c49f9fed453276e210e7/libs/langchain_v1/langchain/agents/middleware/context_editing.py#L187)