Skip to content

feat(providers): add anthropic-discount provider - #2465

Merged
steebchen merged 5 commits into
mainfrom
anthropic-discount-provider
May 31, 2026
Merged

steebchen merged 5 commits into
mainfrom
anthropic-discount-provider

Conversation

@steebchen

@steebchen steebchen commented May 31, 2026

Copy link
Copy Markdown
Member

Summary

Adds a new anthropic-discount provider that behaves exactly like upstream Anthropic for all request/response handling, with two differences:

  • Base URL + API key come from environment variables (LLM_ANTHROPIC_DISCOUNT_BASE_URL / LLM_ANTHROPIC_DISCOUNT_API_KEY), following the same stealth pattern as the avalanche provider.
  • Pricing carries a 10% discount relative to upstream Anthropic.

Currently exposes a single model: Claude Opus 4.8 (anthropic-discount/claude-opus-4-8).

What changed

  • packages/models: new anthropic-discount provider definition (env-based apiKey + baseUrl); claude-opus-4-8 gains an anthropic-discount provider mapping with all prices reduced by 10%.
  • packages/actions: endpoint resolution reads LLM_ANTHROPIC_DISCOUNT_BASE_URL and appends /v1/messages; headers, request-body transform, and Anthropic message/cache-control handling treat anthropic-discount identically to anthropic.
  • apps/gateway: every Anthropic-specific branch (content/reasoning/tool-call/token-usage extraction, streaming + non-streaming transforms, finish-reason mapping, response healing, effort/structured-output beta headers, temperature/top_p stripping, logs) now also matches anthropic-discount.
  • .env.example: documents the two new env vars.

Token and base-URL resolution flow through the existing generic env machinery, so no per-provider plumbing was needed beyond the protocol branches.

Testing

  • pnpm format
  • pnpm build
  • pnpm test:unit ✅ (1675 passed)

End-to-end verification against a real discounted endpoint is pending — the proxy will be run separately.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added an "Anthropic (Discount)" provider option in provider/model selection; supports streaming, tool calls, reasoning extraction, and usage reporting like the standard Anthropic integration. Supports Anthropic beta features (effort and structured outputs) when enabled.
  • Chores / Configuration
    • New environment settings to configure the discount endpoint: LLM_ANTHROPIC_DISCOUNT_BASE_URL and LLM_ANTHROPIC_DISCOUNT_API_KEY.
  • Models
    • Discount provider enabled for supported Anthropic models (e.g., Claude Opus entries).

Add an Anthropic-compatible 'anthropic-discount' provider that behaves
exactly like upstream Anthropic but resolves its base URL and API key
from environment variables (LLM_ANTHROPIC_DISCOUNT_BASE_URL /
LLM_ANTHROPIC_DISCOUNT_API_KEY), mirroring the avalanche stealth pattern.

Includes Claude Opus 4.8 with a 10% discount on all prices.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 31, 2026 14:32
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@coderabbitai

coderabbitai Bot commented May 31, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 92461d3f-7ff9-4671-b9a1-7abe495f9000

📥 Commits

Reviewing files that changed from the base of the PR and between 1c934b0 and d50f07e.

📒 Files selected for processing (4)
  • apps/gateway/src/chat/chat.ts
  • apps/gateway/src/chat/tools/transform-streaming-to-openai.ts
  • packages/models/src/models/anthropic.ts
  • packages/models/src/providers.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • apps/gateway/src/chat/tools/transform-streaming-to-openai.ts
  • packages/models/src/providers.ts
  • apps/gateway/src/chat/chat.ts

Walkthrough

This PR adds end-to-end support for a new anthropic-discount provider: provider registration and model entries, env var docs, base-URL resolution, header/context adjustments, request-body shaping (cache-control), and streaming/non-streaming response parsing and transformation mirroring Anthropic semantics.

Changes

Anthropic Discount Provider Support

Layer / File(s) Summary
Provider registration and model configuration
packages/models/src/providers.ts, packages/models/src/models/anthropic.ts, .env.example
New provider anthropic-discount is registered with required environment variables (LLM_ANTHROPIC_DISCOUNT_BASE_URL, LLM_ANTHROPIC_DISCOUNT_API_KEY), and provider entries are added for Claude Opus models.
Endpoint and base URL resolution
packages/actions/src/get-provider-endpoint.ts
Base URL resolution reads LLM_ANTHROPIC_DISCOUNT_BASE_URL from environment and endpoint path routing treats anthropic-discount identically to anthropic.
Request context and headers
packages/actions/src/get-provider-headers.ts, apps/gateway/src/chat/tools/resolve-provider-context.ts
Includes anthropic-discount in Anthropic-specific parameter handling (temperature/top_p) and anthropic-beta header injection (effort and structured-outputs) in streaming and non-streaming paths.
Request body and message transformation
packages/actions/src/prepare-request-body.ts, packages/actions/src/transform-anthropic-messages.ts
Routes anthropic-discount through Anthropic-compatible request-body shaping and cache-control marker handling, reusing existing Anthropic message transformation logic.
Streaming response processing and enrichment
apps/gateway/src/chat/chat.ts, apps/gateway/src/chat/tools/transform-streaming-to-openai.ts
Extends streaming flows to treat anthropic-discount like Anthropic for JSON-healing buffering, partial usage completion, tool-call enrichment/merge, content/reasoning extraction source selection, web-search accounting, and finish-reason parsing.
Response extraction and transformation utilities
apps/gateway/src/chat/tools/*, apps/gateway/src/lib/logs.ts
Extraction and transformation utilities (content, reasoning, token usage, tool calls, provider response parsing, finish-reason mapping) now recognize anthropic-discount and reuse Anthropic-family logic.

Sequence Diagram

sequenceDiagram
  participant Client
  participant Gateway as Gateway
  participant ProviderTools as ProviderTools
  participant AnthropicDiscount as anthropic-discount

  Client->>Gateway: Chat request (provider: anthropic-discount)
  Gateway->>Gateway: resolveProviderContext (headers, beta markers)
  Gateway->>Gateway: prepareRequestBody (cache control, messages)
  Gateway->>AnthropicDiscount: POST /messages (streaming)
  AnthropicDiscount-->>Gateway: Streaming deltas (content/tool-calls/reasoning)
  Gateway->>Gateway: extractContent (from delta)
  Gateway->>Gateway: extractToolCalls (delta enrichment)
  Gateway->>Gateway: extractReasoning (from delta)
  Gateway->>Gateway: extractTokenUsage (from delta)
  Gateway->>ProviderTools: transformStreamingToOpenai
  Gateway->>ProviderTools: parseProviderResponse
  Gateway->>ProviderTools: mapFinishReasonToOpenai
  Gateway-->>Client: Transformed streaming response
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • theopenco/llmgateway#2435: Modifies packages/models/src/models/anthropic.ts provider configuration for Anthropic-family models (related to adding anthropic-discount).
  • theopenco/llmgateway#2407: Touches Anthropic request-body/cache-control logic (prepareRequestBody / transformAnthropicMessages) overlapping with anthropic-discount cache-control behavior.
  • theopenco/llmgateway#2178: Adds an Anthropic-compatible provider variant and adjusts gateway transformer/extractor switches (similar provider-dispatch changes).

Suggested reviewers

  • smakosh
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title clearly and concisely describes the main change: adding a new anthropic-discount provider with proper feature scope prefix.
Docstring Coverage ✅ Passed Docstring coverage is 85.71% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch anthropic-discount-provider

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds a new anthropic-discount provider that mirrors Anthropic's behavior but routes through a configurable endpoint (LLM_ANTHROPIC_DISCOUNT_BASE_URL / LLM_ANTHROPIC_DISCOUNT_API_KEY) and exposes Claude Opus 4.8 at 10% lower prices. The provider plugs into every Anthropic-specific code branch across model/provider definitions, request transforms, response/stream parsing, headers, finish-reason mapping, and logs.

Changes:

  • Define anthropic-discount provider (env-based apiKey + baseUrl) and add a discounted Claude Opus 4.8 mapping.
  • Endpoint resolution reads the discount base URL and reuses Anthropic's /v1/messages path.
  • Group anthropic-discount with anthropic (and vertex-anthropic where applicable) across gateway request/response/streaming/header/finish-reason/log branches.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated no comments.

Show a summary per file
File Description
packages/models/src/providers.ts Registers the new anthropic-discount provider with required env vars.
packages/models/src/models/anthropic.ts Adds Claude Opus 4.8 mapping for anthropic-discount with 10% lower prices.
packages/actions/src/get-provider-endpoint.ts Resolves discount base URL from env and routes to /v1/messages.
packages/actions/src/get-provider-headers.ts Includes anthropic-discount in Anthropic header path (x-api-key + beta features).
packages/actions/src/prepare-request-body.ts Treats anthropic-discount as Anthropic for cache-control and request-body transform.
packages/actions/src/transform-anthropic-messages.ts Enables cache-control auto-injection for anthropic-discount.
apps/gateway/src/chat/tools/parse-provider-response.ts Parses non-streaming responses identically to Anthropic.
apps/gateway/src/chat/tools/transform-streaming-to-openai.ts Routes streaming chunks through the Anthropic transform.
apps/gateway/src/chat/tools/transform-response-to-openai.ts Routes non-streaming responses through the Anthropic transform.
apps/gateway/src/chat/tools/extract-content.ts Extracts streaming text content for anthropic-discount.
apps/gateway/src/chat/tools/extract-reasoning.ts Extracts streaming reasoning for anthropic-discount.
apps/gateway/src/chat/tools/extract-tool-calls.ts Extracts streaming tool-use blocks for anthropic-discount.
apps/gateway/src/chat/tools/extract-token-usage.ts Reads Anthropic-style usage fields for anthropic-discount.
apps/gateway/src/chat/tools/map-finish-reason-to-openai.ts Maps Anthropic finish reasons for anthropic-discount.
apps/gateway/src/chat/tools/resolve-provider-context.ts Applies Anthropic temperature/top_p rule and beta headers to anthropic-discount.
apps/gateway/src/chat/chat.ts Includes anthropic-discount in all Anthropic branches (headers, healing, streaming handling).
apps/gateway/src/lib/logs.ts Unified finish-reason mapping covers anthropic-discount.
.env.example Documents the new env vars.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

steebchen and others added 4 commits May 31, 2026 18:12
Match anthropic-discount prices exactly to the upstream anthropic
mapping and express the 10% reduction with the mapping-level discount
flag instead of hardcoded discounted prices.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add claude-opus-4-7 to the anthropic-discount provider with prices
matching the upstream anthropic mapping and a 10% discount flag, and
set the provider's routing priority to 1.5.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@steebchen
steebchen enabled auto-merge May 31, 2026 17:38
@steebchen
steebchen disabled auto-merge May 31, 2026 17:42
@steebchen
steebchen enabled auto-merge May 31, 2026 17:42
@steebchen
steebchen added this pull request to the merge queue May 31, 2026
Merged via the queue into main with commit d36ee3d May 31, 2026
17 checks passed
@steebchen
steebchen deleted the anthropic-discount-provider branch May 31, 2026 17:54
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.

2 participants