Skip to content

fix(codex): support 272k tiered pricing for gpt-5.4#882

Open
GrayXu wants to merge 1 commit intoryoppippi:mainfrom
GrayXu:fix/codex-gpt54-tiered-pricing
Open

fix(codex): support 272k tiered pricing for gpt-5.4#882
GrayXu wants to merge 1 commit intoryoppippi:mainfrom
GrayXu:fix/codex-gpt54-tiered-pricing

Conversation

@GrayXu
Copy link

@GrayXu GrayXu commented Mar 8, 2026

Add 272k threshold support to shared LiteLLM pricing parsing and wire Codex cost calculation to use tiered model pricing metadata.

Summary by CodeRabbit

Release Notes

  • New Features
    • Added tiered pricing support, enabling different cost rates above specified token thresholds
    • Models can now configure multiple pricing tiers to optimize costs based on usage levels
    • Pricing system automatically detects and applies tier configurations when available

Add 272k threshold support to shared LiteLLM pricing parsing and wire Codex cost calculation to use tiered model pricing metadata.

Co-authored-by: chatgpt-codex-connector[bot] <199175422+chatgpt-codex-connector[bot]@users.noreply.github.com>
@coderabbitai
Copy link

coderabbitai bot commented Mar 8, 2026

📝 Walkthrough

Walkthrough

The PR extends tiered pricing support by introducing four optional fields to the ModelPricing type, implementing a tiered pricing configuration system and helper functions in both the codex and internal packages, adding a new calculateTieredCost function for per-token cost calculations, and refactoring existing cost computation logic to use tiered rates when available.

Changes

Cohort / File(s) Summary
Type Definitions
apps/codex/src/_types.ts
Adds four optional fields to ModelPricing: tieredThresholdTokens, inputCostPerMTokenAboveThreshold, cachedInputCostPerMTokenAboveThreshold, and outputCostPerMTokenAboveThreshold.
Codex Pricing Implementation
apps/codex/src/pricing.ts
Introduces TIERED_PRICING_CONFIGS array and getTieredPricing helper to extract tiered pricing metadata from LiteLLMModelPricing. Integrates tiered pricing into CodexPricingSource.getPricing by spreading getTieredPricing result into returned ModelPricing. Includes test coverage for tiered pricing preservation.
Token Cost Utilities
apps/codex/src/token-utils.ts
Adds calculateTieredCost function to compute costs with optional threshold-based rates. Refactors calculateCostUSD to use tiered pricing for non-cached input, cached input, and output tokens, with edge case handling and test coverage for both flat and tiered pricing scenarios.
Internal Pricing Support
packages/internal/src/pricing.ts
Introduces TIERED_PRICING_CONFIGS array with multiple threshold configurations (272k, 200k, 128k), updates LiteLLMModelPricing schema with new tiered fields for 272k threshold, and reworks tier detection logic to dynamically determine applicable configuration. Modifies calculateCostFromPricing and calculateTieredCost to use config-driven field lookups instead of fixed threshold values.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant CodexPricing as CodexPricingSource
    participant TieredHelper as getTieredPricing
    participant LiteLLM as LiteLLM Cache

    Client->>CodexPricing: getPricing(model)
    CodexPricing->>LiteLLM: lookup pricing
    LiteLLM-->>CodexPricing: LiteLLMModelPricing
    CodexPricing->>TieredHelper: getTieredPricing(pricing)
    TieredHelper->>TieredHelper: scan TIERED_PRICING_CONFIGS
    TieredHelper-->>CodexPricing: {tieredThresholdTokens, inputCostPerMTokenAboveThreshold, ...}
    CodexPricing->>CodexPricing: spread tiered fields into ModelPricing
    CodexPricing-->>Client: ModelPricing with tiered fields
Loading
sequenceDiagram
    participant Caller
    participant CostCalc as calculateCostUSD
    participant TieredCost as calculateTieredCost

    Caller->>CostCalc: (tokenCounts, pricing)
    CostCalc->>TieredCost: calculateTieredCost(nonCachedInput, basePriceInput, tieredPriceInput, threshold)
    TieredCost-->>CostCalc: input cost
    CostCalc->>TieredCost: calculateTieredCost(cachedInput, basePriceCache, tieredPriceCache, threshold)
    TieredCost-->>CostCalc: cached input cost
    CostCalc->>TieredCost: calculateTieredCost(outputTokens, basePriceOutput, tieredPriceOutput, threshold)
    TieredCost-->>CostCalc: output cost
    CostCalc->>CostCalc: sum costs
    CostCalc-->>Caller: total USD cost
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

Poem

🐰 Thresholds crossed, the tiers ascend,
Where token costs bend and blend,
From codex base to internal cache,
Pricing tiers now flash and flash!
Hopping through configs so divine, 🎉

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding support for 272k tiered pricing for gpt-5.4, which aligns with the PR objectives and the core modifications across files.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
apps/codex/src/pricing.ts (1)

14-38: Reuse the shared tier config here.

packages/internal/src/pricing.ts now carries the same threshold/field table. Keeping both copies in sync is easy to miss when LiteLLM adds the next tier, and Codex/internal pricing will drift again. Consider exporting a small shared config or lookup helper from @ccusage/internal/pricing and consuming it here.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/codex/src/pricing.ts` around lines 14 - 38, Replace the duplicated
TIERED_PRICING_CONFIGS with a single shared source: import the tier table (or a
lookup helper) exported from `@ccusage/internal/pricing` and replace the local
TIERED_PRICING_CONFIGS usage with that import; ensure the imported shape matches
the existing typing (keys like thresholdTokens, inputField, cachedInputField,
outputField and the LiteLLMModelPricing key types) so callers that reference
TIERED_PRICING_CONFIGS continue to work without other changes.
apps/codex/src/token-utils.ts (1)

117-143: Add an exact 272k boundary case.

The new test proves the above-threshold path, but it does not lock down the totalTokens <= thresholdTokens edge in calculateTieredCost. A case at exactly 272_000 would catch off-by-one regressions cheaply.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/codex/src/token-utils.ts` around lines 117 - 143, Add a unit test that
covers the exact boundary where totalTokens === tieredThresholdTokens (272_000)
to prevent off-by-one regressions in the tiered pricing logic; call
calculateCostUSD with input/cached/output values summing to 272_000 and the same
pricing object used in the existing test, compute an expected cost using only
the "below threshold" rates (i.e., do not include any above-threshold
multipliers), and assert cost matches expected with toBeCloseTo; this will
validate calculateTieredCost/calculateCostUSD handles the <= threshold branch
correctly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@apps/codex/src/pricing.ts`:
- Around line 14-38: Replace the duplicated TIERED_PRICING_CONFIGS with a single
shared source: import the tier table (or a lookup helper) exported from
`@ccusage/internal/pricing` and replace the local TIERED_PRICING_CONFIGS usage
with that import; ensure the imported shape matches the existing typing (keys
like thresholdTokens, inputField, cachedInputField, outputField and the
LiteLLMModelPricing key types) so callers that reference TIERED_PRICING_CONFIGS
continue to work without other changes.

In `@apps/codex/src/token-utils.ts`:
- Around line 117-143: Add a unit test that covers the exact boundary where
totalTokens === tieredThresholdTokens (272_000) to prevent off-by-one
regressions in the tiered pricing logic; call calculateCostUSD with
input/cached/output values summing to 272_000 and the same pricing object used
in the existing test, compute an expected cost using only the "below threshold"
rates (i.e., do not include any above-threshold multipliers), and assert cost
matches expected with toBeCloseTo; this will validate
calculateTieredCost/calculateCostUSD handles the <= threshold branch correctly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e383cf4c-0a06-4d08-bafe-42fe4983eb6a

📥 Commits

Reviewing files that changed from the base of the PR and between 0adbb4f and c7d140b.

📒 Files selected for processing (4)
  • apps/codex/src/_types.ts
  • apps/codex/src/pricing.ts
  • apps/codex/src/token-utils.ts
  • packages/internal/src/pricing.ts

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