Skip to content

feat: add Anthropic OAuth pre-call hook to litellm#51

Merged
binary64 merged 2 commits intomasterfrom
feat-litellm-oauth-hook
Feb 24, 2026
Merged

feat: add Anthropic OAuth pre-call hook to litellm#51
binary64 merged 2 commits intomasterfrom
feat-litellm-oauth-hook

Conversation

@binary64
Copy link
Copy Markdown
Owner

@binary64 binary64 commented Feb 24, 2026

Switch Anthropic models from API key auth to OAuth auth using custom pre-call hook.

Changes

New Files

    • Custom pre-call hook that injects OAuth headers for Anthropic API calls

Modified Files

    • Upgrade to (supports custom hooks)
    • Add environment variable (requires ANTHROPIC_OAUTH_TOKEN secret)
    • Mount as ConfigMap volume
    • Remove from Anthropic model configs
    • Add and
    • Add new ConfigMap containing

What This Does

The hook replaces the previous approach with a proper OAuth flow:

  • Injects header
  • Sets required Anthropic headers (, )
  • Removes conflicting headers

Requirements

  • Add to the secret in the namespace
  • Ensure the litellm image supports custom hooks (using nightly build)

Testing

Test by making Anthropic API calls through the litellm proxy and verifying:

  • OAuth headers are present in the request
  • API key headers are NOT present
  • Models respond correctly with OAuth auth

Summary by cubic

Switches Anthropic models in LiteLLM from API key auth to OAuth via a custom pre-call hook. Centralizes auth, injects required headers, and cleans up config; also routes the Qwen thinking model to the free tier.

  • New Features

    • Added async_pre_call_hook that sets Authorization: Bearer from OAUTH_TOKEN (via os.environ) and Anthropic headers; removes API key headers.
    • Updated Anthropic configs to use the hook; added a litellm-hooks ConfigMap mounted at /etc/litellm/hooks.py with subPath; upgraded image to main-v1.81.14-nightly.
    • Set qwen3-235b-thinking model to the :free variant to avoid paid routing.
  • Migration

    • Add ANTHROPIC_OAUTH_TOKEN to the litellm-env secret; it is read as OAUTH_TOKEN.
    • Redeploy LiteLLM to pick up the new image, ConfigMaps, and env vars.

Written for commit f42e156. Summary will update on new commits.

Switch Anthropic models from API key auth to OAuth auth using custom
pre-call hook. This provides more flexible header injection and
replaces the previous extra_headers approach.

Changes:
- Add hooks.py with async_pre_call_hook for OAuth injection
- Update deployment.yaml:
  * Use litellm:main-v1.81.14-nightly (supports custom hooks)
  * Add OAUTH_TOKEN environment variable
  * Mount hooks.py as ConfigMap volume
- Update configmap.yaml:
  * Remove extra_headers from Anthropic models
  * Add custom_llm_provider and async_pre_call_hook
  * Add new litellm-hooks ConfigMap with hooks.py

Requires ANTHROPIC_OAUTH_TOKEN secret in litellm-env namespace.
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 issues found across 2 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="infra/manifests/litellm/configmap.yaml">

<violation number="1" location="infra/manifests/litellm/configmap.yaml:123">
P1: Missing `:free` suffix on model path. All other free-tier OpenRouter models use `:free` but this one doesn't, which will route requests to the paid version and incur costs.</violation>

<violation number="2" location="infra/manifests/litellm/configmap.yaml:208">
P0: Bug: `OAUTH_TOKEN` is referenced as a bare Python variable but it's an environment variable. This will raise `NameError` at runtime. You need to read it from `os.environ`.</violation>
</file>

<file name="infra/manifests/litellm/deployment.yaml">

<violation number="1" location="infra/manifests/litellm/deployment.yaml:104">
P2: Mounting the ConfigMap at a file path without `subPath` will create a directory at `/etc/litellm/hooks.py`, so the hook file won’t be readable where the app expects it. Use a subPath (and/or configMap items) to mount the file instead of the whole directory.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

Resolve conflicts in configmap.yaml:
- Keep OAuth hook approach for Anthropic models (PR intent)
- Fix OAUTH_TOKEN: use os.environ['OAUTH_TOKEN'] instead of bare variable (P0 review)
- Add :free suffix to qwen3-235b model path to avoid paid routing (P1 review)
- Add subPath to hooks.py volume mount to prevent directory creation (P2 review)
@binary64
Copy link
Copy Markdown
Owner Author

All three review comments from cubic-dev-ai[bot] have been addressed in the merge commit:

  1. P0 (OAUTH_TOKEN bare variable): Added import os and changed to os.environ['OAUTH_TOKEN']
  2. P1 (Missing :free suffix): Added :free suffix to openrouter/qwen/qwen3-235b-a22b-thinking-2507:free
  3. P2 (subPath mount): Added subPath: hooks.py to the hooks volume mount in deployment.yaml

@binary64 binary64 merged commit a50f226 into master Feb 24, 2026
8 checks passed
binary64 added a commit that referenced this pull request Feb 24, 2026
…s OAuth

PR #19912 (merged Feb 17) fixed optionally_handle_anthropic_oauth() to:
- Detect sk-ant-oat* tokens in api_key
- Remove x-api-key header
- Set Authorization: Bearer <token> correctly

The custom pre-call hook (from PR #51) is now redundant and causes
'Extra inputs are not permitted' errors. Removing it and relying on
ANTHROPIC_API_KEY env var containing the OAuth token.
binary64 added a commit that referenced this pull request Feb 24, 2026
…s OAuth (#54)

PR #19912 (merged Feb 17) fixed optionally_handle_anthropic_oauth() to:
- Detect sk-ant-oat* tokens in api_key
- Remove x-api-key header
- Set Authorization: Bearer <token> correctly

The custom pre-call hook (from PR #51) is now redundant and causes
'Extra inputs are not permitted' errors. Removing it and relying on
ANTHROPIC_API_KEY env var containing the OAuth token.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant