diff --git a/pr_agent/algo/__init__.py b/pr_agent/algo/__init__.py index 7cb13517..27af6af7 100644 --- a/pr_agent/algo/__init__.py +++ b/pr_agent/algo/__init__.py @@ -79,7 +79,7 @@ 'anthropic/claude-3-5-sonnet-20240620': 100000, 'anthropic/claude-3-5-sonnet-20241022': 100000, 'anthropic/claude-3-7-sonnet-20250219': 200000, - 'claude-3-7-sonnet-20250219': 200000, + 'anthropic/claude-sonnet-4-20250514': 1000000, 'anthropic/claude-3-5-haiku-20241022': 100000, 'bedrock/anthropic.claude-instant-v1': 100000, 'bedrock/anthropic.claude-v2': 100000, @@ -167,7 +167,7 @@ CLAUDE_EXTENDED_THINKING_MODELS = [ "anthropic/claude-opus-4-20250514", - "anthropic/claude-sonnet-4-20250514", + "anthropic/claude-sonnet-4-20250514", "anthropic/claude-3-7-sonnet-20250219", "claude-3-7-sonnet-20250219" ] diff --git a/pr_agent/algo/ai_handlers/litellm_ai_handler.py b/pr_agent/algo/ai_handlers/litellm_ai_handler.py index 0cf487f3..7e70428d 100644 --- a/pr_agent/algo/ai_handlers/litellm_ai_handler.py +++ b/pr_agent/algo/ai_handlers/litellm_ai_handler.py @@ -426,7 +426,7 @@ async def chat_completion(self, model: str, system: str, user: str, temperature: if self.repetition_penalty: kwargs["repetition_penalty"] = self.repetition_penalty - #Added support for extra_headers while using litellm to call underlying model, via a api management gateway, would allow for passing custom headers for security and authorization + # Added support for extra_headers while using litellm to call underlying model, via a api management gateway, would allow for passing custom headers for security and authorization if get_settings().get("LITELLM.EXTRA_HEADERS", None): try: litellm_extra_headers = json.loads(get_settings().litellm.extra_headers) @@ -434,7 +434,22 @@ async def chat_completion(self, model: str, system: str, user: str, temperature: raise ValueError("LITELLM.EXTRA_HEADERS must be a JSON object") except json.JSONDecodeError as e: raise ValueError(f"LITELLM.EXTRA_HEADERS contains invalid JSON: {str(e)}") - kwargs["extra_headers"] = litellm_extra_headers + # Merge with any pre-existing headers + merged_headers = dict(kwargs.get("extra_headers", {})) + merged_headers.update(litellm_extra_headers) + kwargs["extra_headers"] = merged_headers + + # Ensure Anthropic 1M-context beta is enabled for relevant Claude models (not Bedrock) + try: + if ( + ("claude" in model.lower() or model.lower().startswith("anthropic/")) + and not model.lower().startswith("bedrock/") + ): + headers = dict(kwargs.get("extra_headers", {})) + headers.setdefault("anthropic-beta", "context-1m-2025-08-07") + kwargs["extra_headers"] = headers + except Exception as e: + get_logger().debug(f"Unable to set Anthropic beta header: {e}") get_logger().debug("Prompts", artifact={"system": system, "user": user})