From 278242de84a9ea7e9901910f66cc973ac39df71a Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 25 May 2026 13:25:51 +0000 Subject: [PATCH] fix(anthropic/messages): guard context_management polyfill with try/except Wrap apply_context_management() in a try/except so any failure (e.g. litellm.token_counter raising on an unknown tokenizer or unexpected message format) is logged but does not crash the underlying LLM request. The polyfill is a best-effort additive feature; on failure we forward the original messages without applied edits. Co-authored-by: Yassin Kortam --- .../messages/handler.py | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py b/litellm/llms/anthropic/experimental_pass_through/messages/handler.py index 77dd2d6bd2d..0146362f979 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/handler.py @@ -11,6 +11,7 @@ from functools import partial from typing import Any, AsyncIterator, Coroutine, Dict, List, Optional, Union, cast import litellm +from litellm._logging import verbose_logger from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj from litellm.llms.anthropic.common_utils import ( strip_empty_text_blocks_from_anthropic_messages, @@ -473,14 +474,21 @@ def anthropic_messages_handler( apply_context_management, ) - edited_messages, polyfill_applied_edits = apply_context_management( - model=model, - messages=_shared_kwargs["messages"], - tools=_shared_kwargs.get("tools"), - system=_shared_kwargs.get("system"), - context_management_spec=context_management_spec, - ) - _shared_kwargs["messages"] = edited_messages + try: + edited_messages, polyfill_applied_edits = apply_context_management( + model=model, + messages=_shared_kwargs["messages"], + tools=_shared_kwargs.get("tools"), + system=_shared_kwargs.get("system"), + context_management_spec=context_management_spec, + ) + _shared_kwargs["messages"] = edited_messages + except Exception as e: + verbose_logger.exception( + "context_management polyfill: skipping edits due to error: %s", + e, + ) + polyfill_applied_edits = [] return ( LiteLLMMessagesToCompletionTransformationHandler.anthropic_messages_handler(