fix(codex): support 272k tiered pricing for gpt-5.4#882
fix(codex): support 272k tiered pricing for gpt-5.4#882GrayXu wants to merge 1 commit intoryoppippi:mainfrom
Conversation
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>
📝 WalkthroughWalkthroughThe 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
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
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
Estimated code review effort🎯 4 (Complex) | ⏱️ ~50 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). 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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
apps/codex/src/pricing.ts (1)
14-38: Reuse the shared tier config here.
packages/internal/src/pricing.tsnow 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/pricingand 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 <= thresholdTokensedge incalculateTieredCost. A case at exactly272_000would 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
📒 Files selected for processing (4)
apps/codex/src/_types.tsapps/codex/src/pricing.tsapps/codex/src/token-utils.tspackages/internal/src/pricing.ts
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