From e3b4ef7cadc26c462cddfd68c2a48f945ceb7a09 Mon Sep 17 00:00:00 2001 From: Ryan Crabbe Date: Thu, 22 Jan 2026 12:52:12 -0800 Subject: [PATCH] perf: skip redundant redaction when global redaction enabled When turn_off_message_logging is enabled globally, skip per-callback redaction functions that would re-process already-redacted data. 17% faster async_success_handler when global redaction is ON. --- litellm/integrations/custom_logger.py | 8 +++++++- litellm/litellm_core_utils/litellm_logging.py | 15 ++++++++++----- litellm/litellm_core_utils/redact_messages.py | 9 ++++++++- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/litellm/integrations/custom_logger.py b/litellm/integrations/custom_logger.py index 12243a19184..5b3313bd82a 100644 --- a/litellm/integrations/custom_logger.py +++ b/litellm/integrations/custom_logger.py @@ -718,7 +718,9 @@ class CustomLogger: # https://docs.litellm.ai/docs/observability/custom_callbac return OLD_LITELLM_METADATA_FIELD def redact_standard_logging_payload_from_model_call_details( - self, model_call_details: Dict + self, + model_call_details: Dict, + global_redaction_applied: bool = False, ) -> Dict: """ Only redacts messages and responses when self.turn_off_message_logging is True @@ -730,6 +732,10 @@ class CustomLogger: # https://docs.litellm.ai/docs/observability/custom_callbac This is useful for logging payloads that contain sensitive information. """ + # skip redundant redaction if global redaction was already applied + if global_redaction_applied: + return model_call_details + from copy import copy from litellm import Choices, Message, ModelResponse diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index bc5faf962c2..9de11475d27 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -68,6 +68,7 @@ from litellm.litellm_core_utils.model_param_helper import ModelParamHelper from litellm.litellm_core_utils.redact_messages import ( redact_message_input_output_from_custom_logger, redact_message_input_output_from_logging, + should_redact_message_logging, ) from litellm.llms.base_llm.ocr.transformation import OCRResponse from litellm.llms.base_llm.search.transformation import SearchResponse @@ -2410,10 +2411,10 @@ class Logging(LiteLLMLoggingBaseClass): global_callbacks=litellm._async_success_callback, ) + _model_call_details = self.model_call_details if hasattr(self, "model_call_details") else {} + global_redaction_applied = should_redact_message_logging(_model_call_details) result = redact_message_input_output_from_logging( - model_call_details=( - self.model_call_details if hasattr(self, "model_call_details") else {} - ), + model_call_details=_model_call_details, result=result, ) @@ -2439,7 +2440,10 @@ class Logging(LiteLLMLoggingBaseClass): ) elif isinstance(callback, CustomLogger): result = redact_message_input_output_from_custom_logger( - result=result, litellm_logging_obj=self, custom_logger=callback + result=result, + litellm_logging_obj=self, + custom_logger=callback, + global_redaction_applied=global_redaction_applied, ) self.model_call_details, result = await callback.async_logging_hook( kwargs=self.model_call_details, @@ -2494,7 +2498,8 @@ class Logging(LiteLLMLoggingBaseClass): ################################## # call redaction hook for custom logger model_call_details = callback.redact_standard_logging_payload_from_model_call_details( - model_call_details=model_call_details + model_call_details=model_call_details, + global_redaction_applied=global_redaction_applied, ) ################################## if self.stream is True: diff --git a/litellm/litellm_core_utils/redact_messages.py b/litellm/litellm_core_utils/redact_messages.py index 0effed3db70..d3472196e1f 100644 --- a/litellm/litellm_core_utils/redact_messages.py +++ b/litellm/litellm_core_utils/redact_messages.py @@ -30,8 +30,15 @@ else: def redact_message_input_output_from_custom_logger( - litellm_logging_obj: LiteLLMLoggingObject, result, custom_logger: CustomLogger + litellm_logging_obj: LiteLLMLoggingObject, + result, + custom_logger: CustomLogger, + global_redaction_applied: bool = False, ): + # skip redundant redaction if global redaction was already applied + if global_redaction_applied: + return result + if ( hasattr(custom_logger, "message_logging") and custom_logger.message_logging is not True