Route completion directly to Anthropic Messages API with OAuth. Unlike API-key auth (x-api-key header), OAuth uses Authorization: Bearer header + Claude Code spoofing headers. This makes the request indistinguishable from a real Claude Code session.
(
self,
model: str,
messages: list[dict[str, Any]],
api_base: str | None = None,
custom_prompt_dict: dict[str, Any] | None = None,
model_response: ModelResponse | None = None,
print_verbose: Any = None,
encoding: Any = None,
logging_obj: Any = None,
optional_params: dict[str, Any] | None = None,
acompletion: bool | None = None,
timeout: float | None = None,
litellm_params: dict[str, Any] | None = None,
logger_fn: Any = None,
headers: dict[str, str] | None = None,
**kwargs: Any,
)
| 623 | return body_str, api_url, actual_model |
| 624 | |
| 625 | def completion( |
| 626 | self, |
| 627 | model: str, |
| 628 | messages: list[dict[str, Any]], |
| 629 | api_base: str | None = None, |
| 630 | custom_prompt_dict: dict[str, Any] | None = None, |
| 631 | model_response: ModelResponse | None = None, |
| 632 | print_verbose: Any = None, |
| 633 | encoding: Any = None, |
| 634 | logging_obj: Any = None, |
| 635 | optional_params: dict[str, Any] | None = None, |
| 636 | acompletion: bool | None = None, |
| 637 | timeout: float | None = None, |
| 638 | litellm_params: dict[str, Any] | None = None, |
| 639 | logger_fn: Any = None, |
| 640 | headers: dict[str, str] | None = None, |
| 641 | **kwargs: Any, |
| 642 | ) -> ModelResponse: |
| 643 | """Route completion directly to Anthropic Messages API with OAuth. |
| 644 | |
| 645 | Unlike API-key auth (x-api-key header), OAuth uses |
| 646 | Authorization: Bearer header + Claude Code spoofing headers. |
| 647 | This makes the request indistinguishable from a real Claude Code session. |
| 648 | """ |
| 649 | body_str, api_url, actual_model = self._build_anthropic_request( |
| 650 | model, messages, optional_params, api_base, stream=False |
| 651 | ) |
| 652 | |
| 653 | def _send(force_refresh: bool) -> httpx.Response: |
| 654 | access_token = get_access_token(force_refresh=force_refresh) |
| 655 | req_headers = _build_headers(access_token) |
| 656 | return _http_post( |
| 657 | f"{api_url}/v1/messages?beta=true", |
| 658 | content=body_str, |
| 659 | headers=req_headers, |
| 660 | timeout=timeout or 600, |
| 661 | ) |
| 662 | |
| 663 | resp = with_retry_on_401(_send) |
| 664 | |
| 665 | if resp.status_code == 401: |
| 666 | raise litellm.AuthenticationError( |
| 667 | message=( |
| 668 | "Claude Code authentication was rejected. Run 'claude /login' " |
| 669 | f"and retry. Underlying: {resp.text}" |
| 670 | ), |
| 671 | model=model, |
| 672 | llm_provider="auth", |
| 673 | ) |
| 674 | |
| 675 | if resp.status_code == 429: |
| 676 | # Parse retry-after header (seconds or milliseconds) |
| 677 | retry_after = None |
| 678 | retry_after_ms = resp.headers.get("retry-after-ms") |
| 679 | if retry_after_ms: |
| 680 | try: |
| 681 | retry_after = int(retry_after_ms) / 1000 |
| 682 | except ValueError: |
nothing calls this directly
no test coverage detected