From 2209041e4a57ea213d3e3d3cba85eeb92a09452b Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 10:58:30 -0700 Subject: [PATCH] test(context_management): cover a client compaction summary counting toward the trigger The trigger also skips the system prompt count when messages and tools alone already pass it --- .../editors/clear_tool_uses.py | 2 ++ .../test_clear_tool_uses.py | 32 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/litellm/llms/anthropic/messages/context_management/editors/clear_tool_uses.py b/litellm/llms/anthropic/messages/context_management/editors/clear_tool_uses.py index 314e7411ffc..31f0f9e0963 100644 --- a/litellm/llms/anthropic/messages/context_management/editors/clear_tool_uses.py +++ b/litellm/llms/anthropic/messages/context_management/editors/clear_tool_uses.py @@ -223,6 +223,8 @@ def _trigger_met( messages=messages, tools=cast("list[ChatCompletionToolParam] | None", tools), ) + if message_tokens > threshold: + return True, message_tokens current_tokens: Final = message_tokens + count_system_tokens(model, system) verbose_logger.debug("context_management polyfill: current_tokens: %s", current_tokens) verbose_logger.debug("context_management polyfill: threshold: %s", threshold) diff --git a/tests/unit/llms/anthropic/messages/context_management/test_clear_tool_uses.py b/tests/unit/llms/anthropic/messages/context_management/test_clear_tool_uses.py index bd5ba341302..38b60e716fb 100644 --- a/tests/unit/llms/anthropic/messages/context_management/test_clear_tool_uses.py +++ b/tests/unit/llms/anthropic/messages/context_management/test_clear_tool_uses.py @@ -10,7 +10,7 @@ from unittest.mock import AsyncMock, MagicMock import pytest import litellm -from litellm.llms.anthropic.messages.adapters.handler import _run_polyfill_if_enabled +from litellm.llms.anthropic.messages.adapters.handler import _run_polyfill_if_enabled, prepare_context_managed_request from litellm.llms.anthropic.messages.context_management import ( AnthropicContextManagementError, PolyfillResult, @@ -724,6 +724,36 @@ def test_input_tokens_trigger_counts_the_system_prompt(system: str | list[dict[s assert with_system["cleared_input_tokens"] == met_without_system["cleared_input_tokens"] > 0 +async def test_input_tokens_trigger_counts_a_client_compaction_summary() -> None: + """A client compaction summary moves into the system prompt, so the trigger counts it too.""" + tail: Final = _make_history(n_pairs=3) + edit_spec: Final = { + "type": "clear_tool_uses_20250919", + "trigger": {"type": "input_tokens", "value": litellm.token_counter(model=MODEL, messages=tail) + 100}, + "keep": {"type": "tool_uses", "value": 1}, + } + compacted_turn: Final = {"role": "assistant", "content": [{"type": "compaction", "content": "Earlier work. " * 400}]} + + async def prepare(messages: list[dict[str, object]]) -> PolyfillResult | None: + return await prepare_context_managed_request( + model=MODEL, + messages=messages, + tools=None, + system=None, + context_management_spec={"edits": [edit_spec]}, + litellm_metadata=None, + additional_drop_params=None, + llm_router=None, + ) + + without_summary: Final = await prepare(deepcopy(tail)) + with_summary: Final = await prepare([compacted_turn, *deepcopy(tail)]) + + assert without_summary is None or not without_summary.applied_edits + assert with_summary is not None + assert [edit.get("cleared_tool_uses") for edit in with_summary.applied_edits] == [2] + + @pytest.mark.parametrize(("clear_tool_inputs", "triggers"), [(False, False), (True, True)]) def test_tool_uses_trigger_counts_tiny_results_only_when_their_inputs_are_cleared( clear_tool_inputs: bool, triggers: bool