Repository navigation
fix(server): usage prices long-context requests at above-tier rates - #25430
juliusmarminge wants to merge 2 commits into
Conversation
…(red) Failing tests for #25414: a Codex gpt-6-sol turn above 272K prompt tokens, a Claude prompt above 100K, and a priority request above 272K are all priced at base rates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Requests whose prompt (input + cache read + cache write) exceeds a model's LiteLLM *_above_<N>k_tokens threshold now bill at that tier, including its _priority/_ultrafast variants. The tier is chosen per record, so many small requests stay at base rates. Fields a tier omits fall back to base rates; overrides and untiered models are unchanged. Fixes #25414 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| !sameTokenRates(fast, b.fast ?? fast) || | ||
| !sameTokenRates(ultrafast, b.ultrafast ?? ultrafast) | ||
| !sameTokenRates(ultrafast, b.ultrafast ?? ultrafast) || | ||
| (b.contextTiers.length > 0 && !sameContextTiers(contextTiers, b.contextTiers)) |
There was a problem hiding this comment.
🟡 Medium usage/usagePricing.ts:214
A bare alias is rejected even when both providers agree on its standard context-tier price, so priceUsage reports short standard requests as unpriced. sameContextTiers treats a missing fast or ultrafast rate as a conflict instead of filling it from the other matching tier; merge those missing speed variants before checking for conflicts.
🚀 Reply "fix it for me" or copy this AI Prompt for your agent:
In file @apps/server/src/usage/usagePricing.ts around line 214:
A bare alias is rejected even when both providers agree on its standard context-tier price, so `priceUsage` reports short standard requests as unpriced. `sameContextTiers` treats a missing `fast` or `ultrafast` rate as a conflict instead of filling it from the other matching tier; merge those missing speed variants before checking for conflicts.
ApprovabilityVerdict: Needs Human Approval Macroscope's review found this PR requires human approval — This production change alters customer-visible usage pricing, cache savings, speed premiums, and persisted usage repricing for long-context requests. Its billing/metering impact, together with an unresolved Medium pricing concern around aliases, warrants human review. Needs human approval because:
Adjust the Minimum Blocking Severity for this repo — including turning it Off — in Settings. You can add or adjust custom eligibility rules. Learn more. |
Thread transfer impact✅ Thread transfer remains within every enforced ceiling.
Baseline: Scenario and decoded snapshot size10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed. |
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Parse context-tier rates apps/server/src/usage/usagePricing.ts |
ModelRate now includes context tiers. Rate parsing reads tier-specific standard and speed rates, applies base-rate fallbacks for omitted fields, and sorts tiers by threshold. |
Select rates by prompt size apps/server/src/usage/usagePricing.ts, apps/server/src/usage/usagePricing.test.ts |
Alias merging includes context tiers. Pricing and cache-savings calculations select rates using prompt-token totals and record speed. Tests cover Claude and GPT thresholds, priority rates, missing tier fields, flat-rate models, and price overrides. |
Verify aggregate and service pricing apps/server/src/usage/usageAggregation.test.ts, apps/server/src/usage/UsageService.test.ts |
Aggregation tests check that higher rates apply to the long request. Service tests check the expected $0.207 cost when the rates endpoint returns 200 or 500. |
Priority:
Estimated code review effort: 3 (Moderate) | ~25 minutes
Change: Bug fix · Severity of issue fixed: Low
Suggested reviewers: t3dotgg, maria-rcks
Merge Risk: 
Minimal · up to f5a43
-
No concrete issue in the supplied evidence prevents merging after normal checks.
Pre-merge checks: 
4
-
Full details: Description check
-
Explanation
The description includes complete Problem, Change, Scope and approval, and Verification sections. It provides the issue link, implementation scope, focused test results, runtime evidence, and known verification limitations.
Full details: Linked Issues check
-
Explanation
Issue [
#25414] requires above-threshold pricing for GPT-family, Claude, and priority requests without changing token totals.parseRateTablereadsabove_Nk_tokenstiers, fills omitted tier fields from base rates, and preserves speed-specific tiers.ratesAtselects a tier when combined uncached, cached, and cache-creation input tokens exceed the threshold.priceUsageapplies that tier to input, cache, output, category costs, and speed premiums.cacheSavingsUsduses the same tier. Overrides remain flat. The added tests cover Claude above 100K, GPT above 272K, priority rates, threshold boundaries, fallback fields, overrides, mixed request aggregation, and persistedUsageServicepricing. The changed files are within the issue'sapps/serverscope.
Full details: Out of Scope Changes check
-
Explanation
The whole-PR diff contains changes only in server usage pricing and its regression tests:
apps/server/src/usage/usagePricing.ts,usagePricing.test.ts,usageAggregation.test.ts, andUsageService.test.ts. The implementation and tests directly support issue [#25414]. No client, wire, unrelated feature, or unrelated cleanup changes are present.
-
Finishing Touches
-
📝 Generate docstrings
-
- Commit to this branch
- Create a new PR
🧪 Generate unit tests (beta)
-
- Commit to this branch
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Comment @coderabbitai help to get the list of available commands.
Problem
Usage estimated requests above LiteLLM's long-context thresholds at base rates. A Codex rollout with 300,000 input tokens (280,000 cached) and 1,000 output tokens on
gpt-6-solcost $0.106 instead of $0.207; Claude's 100K tier and priority long-context rates were also ignored.Change
Parse LiteLLM's context tiers and select the rate from each request's uncached, cached, and cache-creation input total before aggregating costs. Apply the same tier to category costs, cache savings, and speed premiums. Existing raw rate documents and per-request scan caches are repriced on read; custom price overrides remain flat.
Scope and approval
Fixes #25414
Triaged bug via the automated issue flow. GitCafe: #25414. Reproduced in the real web app against an explicitly isolated server home. Scope is server pricing and its regression tests.
Verification
UsageService,usagePricing, andusageAggregation; covers threshold boundaries, mixed short/long requests, overrides, missing rates, and persisted cache restoration.--home-dir: cost $0.106 → $0.207; 301K processed tokens, 280K cached, 20K uncached, and 1K output unchanged. After categories: input $0.080, cache read $0.112, output $0.015. Before response · After response · Reproduction seed.[], independently audited sufficient.vp run check:changed: format, lint, GraphQL validation, and focused tests passed. The overall run failed on a stale local TypeScript executable and knip's mobile Metro configuration loader/unrelated unused export. A retry was stopped when VM load reached 25. Complete check output.vp run --filter t3 typecheckpassed (exit 0). The overall changed-file run still has no complete green result.Models: GPT-6.1 Sol through Codex and Claude Opus 5.5 through Claude Code, in T3 Code.