-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Litellm anthropic contex management param support #16528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ishaan-jaff
merged 6 commits into
main
from
litellm_anthropic_contex_management_param_support
Nov 15, 2025
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
308c1bc
Add support for anthropic context_management param
Sameerlite 852cb45
Add support for anthropic context_management param
Sameerlite 8cdd836
Add context management in response
Sameerlite 569e873
Merge branch 'main' into litellm_anthropic_contex_management_param_su…
Sameerlite 0853e4e
fix review changes
Sameerlite a7918af
Merge branch 'main' into litellm_anthropic_contex_management_param_su…
ishaan-jaff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,6 +132,7 @@ def get_supported_openai_params(self, model: str): | |
| "response_format", | ||
| "user", | ||
| "web_search_options", | ||
| "context_management", | ||
| ] | ||
|
|
||
| if "claude-3-7-sonnet" in model or supports_reasoning( | ||
|
|
@@ -661,6 +662,13 @@ def update_headers_with_optional_anthropic_beta( | |
| headers[ | ||
| "anthropic-beta" | ||
| ] = ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_MANAGEMENT_2025_06_27.value | ||
| if optional_params.get("context_management") is not None: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. make this a helper method
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||
| existing_beta = headers.get("anthropic-beta") | ||
| beta_value = ANTHROPIC_BETA_HEADER_VALUES.CONTEXT_MANAGEMENT_2025_06_27.value | ||
| if existing_beta is None: | ||
| headers["anthropic-beta"] = beta_value | ||
| elif beta_value not in [beta.strip() for beta in existing_beta.split(",")]: | ||
| headers["anthropic-beta"] = f"{existing_beta}, {beta_value}" | ||
| return headers | ||
|
|
||
| def transform_request( | ||
|
|
@@ -972,13 +980,21 @@ def transform_parsed_response( | |
| ): | ||
| text_content = prefix_prompt + text_content | ||
|
|
||
| context_management: Optional[Dict] = completion_response.get( | ||
| "context_management" | ||
| ) | ||
|
|
||
| provider_specific_fields: Dict[str, Any] = { | ||
| "citations": citations, | ||
| "thinking_blocks": thinking_blocks, | ||
| } | ||
| if context_management is not None: | ||
| provider_specific_fields["context_management"] = context_management | ||
|
|
||
| _message = litellm.Message( | ||
| tool_calls=tool_calls, | ||
| content=text_content or None, | ||
| provider_specific_fields={ | ||
| "citations": citations, | ||
| "thinking_blocks": thinking_blocks, | ||
| }, | ||
| provider_specific_fields=provider_specific_fields, | ||
| thinking_blocks=thinking_blocks, | ||
| reasoning_content=reasoning_content, | ||
| ) | ||
|
|
@@ -1011,6 +1027,16 @@ def transform_parsed_response( | |
| model_response.created = int(time.time()) | ||
| model_response.model = completion_response["model"] | ||
|
|
||
| context_management_response = completion_response.get("context_management") | ||
| if context_management_response is not None: | ||
| _hidden_params["context_management"] = context_management_response | ||
| try: | ||
| model_response.__dict__["context_management"] = ( | ||
| context_management_response | ||
| ) | ||
| except Exception: | ||
| pass | ||
|
|
||
| model_response._hidden_params = _hidden_params | ||
|
|
||
| return model_response | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is not an openai param