Skip to content

feat: add OAuth 2.1 support for Claude.ai Connectors#26

Merged
acedatacloud-dev merged 2 commits intomainfrom
feat/oauth-support
Mar 20, 2026
Merged

feat: add OAuth 2.1 support for Claude.ai Connectors#26
acedatacloud-dev merged 2 commits intomainfrom
feat/oauth-support

Conversation

@acedatacloud-dev
Copy link
Member

Problem

OpenAI models return cached_tokens in prompt_tokens_details, but _chat_models.json had no discount terms for cached tokens on any OpenAI model. This caused overcharging when the upstream returned cached tokens:

Model cached_tokens Expected Cost Actual Billed Overcharge
gpt-4o 3712 ~0.000303 ~0.000583 92.4%
gpt-4.1-mini 3840 ~0.000153 ~0.000204 33.1%

Root Cause

OpenAI's prompt_tokens includes cached_tokens (unlike Claude where prompt_tokens excludes them). So the discount must be applied as a negative offset — subtracting the discount portion of cached tokens from the total.

Changes

cost/api/_chat_models.json — 19 model rules modified

Each rule's consumption formula now includes a third term:

cached_mult * var("prompt_tokens_details.cached_tokens", 0)

Where cached_mult = -(prompt_mult * discount_rate), so it subtracts the discounted portion.

50% discount (matching OpenAI pricing):

  • gpt-4o, gpt-4o-mini, gpt-4o-2024-05-13
  • o1, o1-mini, o1-pro, o3-mini

75% discount (matching OpenAI pricing):

  • gpt-4.1, gpt-4.1-mini, gpt-4.1-nano
  • o3, o3-pro, o4-mini
  • gpt-5, gpt-5-mini, gpt-5-nano, gpt-5.1, gpt-5.4, gpt-5.4-pro

Also added official_price.cached_tokens to each rule for transparency.

tests/api_cost/test_claude_cache_tokens.py

  • Replaced test_non_claude_model_unaffected (tested OLD behavior) with two new tests verifying cached_tokens discount IS now applied
  • Added TestOpenAICachedTokens class with 14 test cases covering all discount tiers, edge cases (zero cached, all cached, cached_creation_tokens ignored)
  • All 642 tests pass

Backwards Compatibility

When cached_tokens = 0 (or absent), the new term evaluates to 0, so billing for non-cached requests is completely unchanged.

@acedatacloud-dev acedatacloud-dev merged commit 7ebec13 into main Mar 20, 2026
5 checks passed
@acedatacloud-dev acedatacloud-dev deleted the feat/oauth-support branch March 20, 2026 16:07
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