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).
ContextEditingMiddleware(
self,
*,
edits: Iterable[ContextEdit] | None = None,
token_count_method: Literal['approximate', 'model'] = 'approximate'
)| Name | Type | Description |
|---|---|---|
edits | Iterable[ContextEdit] | None | Default: NoneSequence of edit strategies to apply. Defaults to a single |
token_count_method | Literal['approximate', 'model'] | Default: 'approximate'Whether to use approximate token counting (faster, less accurate) or exact counting implemented by the chat model (potentially slower, more accurate). |
| Name | Type |
|---|---|
| edits | Iterable[ContextEdit] | None |
| token_count_method | Literal['approximate', 'model'] |
Logic to run before the agent execution starts.
Async logic to run before the agent execution starts.
Logic to run before the model is called.
Async logic to run before the model is called.
Logic to run after the model is called.
Async logic to run after the model is called.
Logic to run after the agent execution completes.
Async logic to run after the agent execution completes.
Intercept tool execution for retries, monitoring, or modification.
Intercept and control async tool execution via handler callback.