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'] |
Start the shell session and run startup commands.
Async start the shell session and run startup commands.
Check model call limits before making a model call.
Async check model call limits before making a model call.
Check for parallel write_todos tool calls and return errors if detected.
Check for parallel write_todos tool calls and return errors if detected.
Run shutdown commands and release resources when an agent completes.
Async run shutdown commands and release resources when an agent completes.
Intercept tool execution for retries, monitoring, or modification.
Intercept and control async tool execution via handler callback.