Skip to content

feat(gateway): add platform mode for user-token authentication - #1015

Merged
tbille merged 2 commits into
mainfrom
feat/gateway-platform-mode
Apr 16, 2026
Merged

feat(gateway): add platform mode for user-token authentication#1015
tbille merged 2 commits into
mainfrom
feat/gateway-platform-mode

Conversation

@tbille

@tbille tbille commented Apr 10, 2026

Copy link
Copy Markdown
Member

Description

Add platform mode to GatewayProvider that allows users to authenticate with the gateway using a simple user token and URL, without needing a gateway API key.

Platform mode sends user auth as Authorization: Bearer <platform_user_token> (standard OpenAI client behavior) instead of the X-AnyLLM-Key custom header. It keeps using OpenAI-style /v1/chat/completions payloads and supports both provider:model and bare model formats.

How platform mode activates

Trigger Behavior
platform_mode=True with api_key param Explicit platform mode using the provided token
platform_mode=True without api_key Falls back to GATEWAY_PLATFORM_TOKEN env var
GATEWAY_PLATFORM_TOKEN env var set, no api_key passed Auto-detects platform mode
Explicit api_key passed (no platform_mode=True) Standard gateway mode (unchanged)

Error handling in platform mode

Maps gateway HTTP responses to typed any-llm exceptions:

HTTP Status Exception
401, 403 AuthenticationError
402 InsufficientFundsError (new)
404 ModelNotFoundError
429 RateLimitError (with retry_after attribute)
502 UpstreamProviderError (new)
504 GatewayTimeoutError (new)

X-Correlation-ID from gateway responses is surfaced in error messages for tracing.

Files changed

  • src/any_llm/exceptions.py - New exception types + retry_after on RateLimitError
  • src/any_llm/providers/gateway/gateway.py - Platform mode init, error handling, method overrides
  • src/any_llm/utils/exception_handler.py - Recognize new exception patterns in convert_exception
  • tests/unit/providers/test_gateway_provider.py - 20 new tests for platform mode (31 total)
  • tests/unit/providers/test_cohere_exceptions.py - Updated to expect GatewayTimeoutError

PR Type

  • 🆕 New Feature

Relevant issues

Checklist

  • I understand the code I am submitting.
  • I have added unit tests that prove my fix/feature works
  • I have run this code locally and verified it fixes the issue.
  • New and existing tests pass locally
  • Documentation was updated where necessary
  • I have read and followed the contribution guidelines
  • AI Usage:
    • No AI was used.
    • AI was used for drafting/refactoring.
    • This is fully AI-generated.

AI Usage Information

  • AI Model used: Claude Opus 4 (claude-opus-4-6)

  • AI Developer Tool used: OpenCode

  • Any other info you'd like to share: Full implementation including exception design, platform mode detection logic, error mapping, and comprehensive test coverage.

  • I am an AI Agent filling out this form (check box if true)

@tbille
tbille temporarily deployed to integration-tests April 10, 2026 13:33 — with GitHub Actions Inactive
@codecov

codecov Bot commented Apr 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.69892% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/any_llm/providers/gateway/gateway.py 94.87% 4 Missing ⚠️
Files with missing lines Coverage Δ
src/any_llm/exceptions.py 100.00% <100.00%> (ø)
src/any_llm/utils/exception_handler.py 71.42% <100.00%> (-15.90%) ⬇️
src/any_llm/providers/gateway/gateway.py 96.46% <94.87%> (-3.54%) ⬇️

... and 39 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tbille
tbille temporarily deployed to integration-tests April 10, 2026 14:10 — with GitHub Actions Inactive
@njbrake njbrake added the gateway Issues related to any-llm-gateway label Apr 13, 2026
@tbille
tbille force-pushed the feat/gateway-platform-mode branch from ef97b9d to 7429e05 Compare April 14, 2026 18:42
@tbille
tbille temporarily deployed to integration-tests April 14, 2026 18:42 — with GitHub Actions Inactive
@tbille
tbille requested a review from njbrake April 14, 2026 18:42

@njbrake njbrake left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM 🚀

tbille added 2 commits April 16, 2026 15:27
Add platform mode to GatewayProvider that sends user auth via
Authorization: Bearer <token> instead of X-AnyLLM-Key. Platform mode
activates when platform_mode=True is passed explicitly, or when the
GATEWAY_PLATFORM_TOKEN env var is set without an explicit api_key.

Platform-mode error handling maps HTTP status codes from the gateway
(401/402/403/404/429/502/504) to typed any-llm exceptions, preserves
Retry-After headers on RateLimitError, and surfaces X-Correlation-ID
in error messages for tracing.

New exception types: InsufficientFundsError (402),
UpstreamProviderError (502), GatewayTimeoutError (504).
RateLimitError gains an optional retry_after attribute.
…patterns

Cover all async method overrides (_acompletion, _aresponses, _aembedding,
_alist_models) for both platform-mode error wrapping and success paths,
plus non-platform passthrough. Add convert_exception tests for
InsufficientFundsError, UpstreamProviderError, and GatewayTimeoutError
patterns.
@tbille
tbille force-pushed the feat/gateway-platform-mode branch from 7429e05 to 734f6bc Compare April 16, 2026 13:27
@tbille
tbille temporarily deployed to integration-tests April 16, 2026 13:27 — with GitHub Actions Inactive
@tbille
tbille merged commit efab07d into main Apr 16, 2026
10 checks passed
@tbille
tbille deleted the feat/gateway-platform-mode branch April 16, 2026 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gateway Issues related to any-llm-gateway

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants