Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Add tranlation of context_management

Sameer Kankute7adaf49db7a0
diff --git a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/handler.py b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/handler.py
index 18dbabb1e14..c268d6c5be8 100644
--- a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/handler.py
+++ b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/handler.py
@@ -24,7 +24,9 @@ def _build_responses_kwargs(
max_tokens: int,
messages: List[Dict],
model: str,
+ context_management: Optional[Dict] = None,
metadata: Optional[Dict] = None,
+ output_config: Optional[Dict] = None,
stop_sequences: Optional[List[str]] = None,
stream: Optional[bool] = False,
system: Optional[str] = None,
@@ -42,6 +44,10 @@ def _build_responses_kwargs(
"""
# Build a typed AnthropicMessagesRequest for the adapter
request_data: Dict[str, Any] = {"model": model, "messages": messages, "max_tokens": max_tokens}
+ if context_management:
+ request_data["context_management"] = context_management
+ if output_config:
+ request_data["output_config"] = output_config
if metadata:
request_data["metadata"] = metadata
if system:
@@ -98,7 +104,9 @@ class LiteLLMMessagesToResponsesAPIHandler:
max_tokens: int,
messages: List[Dict],
model: str,
+ context_management: Optional[Dict] = None,
metadata: Optional[Dict] = None,
+ output_config: Optional[Dict] = None,
stop_sequences: Optional[List[str]] = None,
stream: Optional[bool] = False,
system: Optional[str] = None,
@@ -115,7 +123,9 @@ class LiteLLMMessagesToResponsesAPIHandler:
max_tokens=max_tokens,
messages=messages,
model=model,
+ context_management=context_management,
metadata=metadata,
+ output_config=output_config,
stop_sequences=stop_sequences,
stream=stream,
system=system,
@@ -145,7 +155,9 @@ class LiteLLMMessagesToResponsesAPIHandler:
max_tokens: int,
messages: List[Dict],
model: str,
+ context_management: Optional[Dict] = None,
metadata: Optional[Dict] = None,
+ output_config: Optional[Dict] = None,
stop_sequences: Optional[List[str]] = None,
stream: Optional[bool] = False,
system: Optional[str] = None,
@@ -168,7 +180,9 @@ class LiteLLMMessagesToResponsesAPIHandler:
max_tokens=max_tokens,
messages=messages,
model=model,
+ context_management=context_management,
metadata=metadata,
+ output_config=output_config,
stop_sequences=stop_sequences,
stream=stream,
system=system,
@@ -187,7 +201,9 @@ class LiteLLMMessagesToResponsesAPIHandler:
max_tokens=max_tokens,
messages=messages,
model=model,
+ context_management=context_management,
metadata=metadata,
+ output_config=output_config,
stop_sequences=stop_sequences,
stream=stream,
system=system,
diff --git a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py
index a428e8f4e8f..c2752272905 100644
--- a/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py
+++ b/litellm/llms/anthropic/experimental_pass_through/responses_adapters/transformation.py
@@ -191,6 +191,37 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
return {"type": "function", "name": tool_choice.get("name", "")}
return {"type": "auto"}
+ @staticmethod
+ def translate_context_management_to_responses_api(
+ context_management: Dict[str, Any],
+ ) -> Optional[List[Dict[str, Any]]]:
+ """
+ Convert Anthropic context_management dict to OpenAI Responses API array format.
+
+ Anthropic format: {"edits": [{"type": "compact_20260112", "trigger": {"type": "input_tokens", "value": 150000}}]}
+ OpenAI format: [{"type": "compaction", "compact_threshold": 150000}]
+ """
+ if not isinstance(context_management, dict):
+ return None
+
+ edits = context_management.get("edits", [])
+ if not isinstance(edits, list):
+ return None
+
+ result: List[Dict[str, Any]] = []
+ for edit in edits:
+ if not isinstance(edit, dict):
+ continue
+ edit_type = edit.get("type", "")
+ if edit_type == "compact_20260112":
+ entry: Dict[str, Any] = {"type": "compaction"}
+ trigger = edit.get("trigger")
+ if isinstance(trigger, dict) and trigger.get("value") is not None:
+ entry["compact_threshold"] = int(trigger["value"])
+ result.append(entry)
+
+ return result if result else None
+
@staticmethod
def translate_thinking_to_reasoning(thinking: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""
@@ -276,8 +307,13 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
if reasoning:
responses_kwargs["reasoning"] = reasoning
- # output_format -> text format
+ # output_format / output_config.format -> text format
+ # output_format: {"type": "json_schema", "schema": {...}}
+ # output_config: {"format": {"type": "json_schema", "schema": {...}}}
output_format = anthropic_request.get("output_format")
+ output_config = anthropic_request.get("output_config")
+ if not isinstance(output_format, dict) and isinstance(output_config, dict):
+ output_format = output_config.get("format")
if isinstance(output_format, dict) and output_format.get("type") == "json_schema":
schema = output_format.get("schema")
if schema:
@@ -290,10 +326,17 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
}
}
+ # context_management: Anthropic dict -> OpenAI array
+ context_management = anthropic_request.get("context_management")
+ if isinstance(context_management, dict):
+ openai_cm = self.translate_context_management_to_responses_api(context_management)
+ if openai_cm is not None:
+ responses_kwargs["context_management"] = openai_cm
+
# metadata user_id -> user
metadata = anthropic_request.get("metadata")
if isinstance(metadata, dict) and "user_id" in metadata:
- responses_kwargs["user"] = metadata["user_id"]
+ responses_kwargs["user"] = str(metadata["user_id"])[:64]
return responses_kwargs