feat: serve per-category price_details (cached, cache_creation, audio) - #5
Merged
Merged
Conversation
Adds an open `price_details` map ({token_category: cost_per_token}) alongside the
scalar prompt/completion costs, so consumers (Openlayer platform cost engine) can
price granular token categories. Keys are a canonical contract matched by name to
a request's usageDetails.
Sources → canonical keys:
- LiteLLM: input_cost_per_token→input_tokens, output_cost_per_token→output_tokens,
cache_read_input_token_cost→cached_tokens,
cache_creation_input_token_cost→cache_creation_tokens,
input_cost_per_audio_token→audio_input_tokens,
output_cost_per_audio_token→audio_output_tokens.
- OpenRouter pricing: prompt/completion/input_cache_read/input_cache_write.
Reasoning is intentionally omitted: it is billed at the output rate and already
counted within output_tokens, so it carries no separate price (surfaced as an
informational usage-only category by producers).
- entities/models/schemas/repositories/api: thread price_details through; JSON
column (JSONB on PG, JSON on the SQLite test env), nullable for back-compat,
read as {} when null. Additive — the two scalars stay; consumers that ignore
the field are unaffected.
- alembic 004: add nullable price_details column.
Verified: 101 tests pass; live LiteLLM extraction across 2271 models
(cached=568, cache_creation=197, audio_input=111, audio_output=48).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
OPEN-11150 Backend: per-key cost computation
Part of OPEN-11148. Depends on OPEN-11149. Extend model price definitions with per-category prices; compute Acceptance criteria
|
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
gustavocidornelas
approved these changes
Jun 5, 2026
gustavocidornelas
left a comment
Collaborator
There was a problem hiding this comment.
Overall looks good. Added just a small comment. Feel free to merge after addressing it
Also, it'd be good if you could add unit tests for _coerce_price and _build_price_details (just because the rest of the methods overall are all tested).
…t tests Address review (gustavocidornelas): - Drop input_tokens/output_tokens from price_details — they duplicated the root prompt_cost_per_token/completion_cost_per_token. price_details now carries only the granular extras (cached, cache_creation, audio). Consumers reuse the root scalars for base input/output rates. - Add unit tests for _coerce_price and _build_price_details, plus provider-level checks that price_details carries only extras (empty for basic models).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What & why
Adds an open
price_detailsmap ({token_category: cost_per_token}) to the cost records, alongside the existing scalarprompt_cost_per_token/completion_cost_per_token. This lets the Openlayer platform's cost engine price granular token categories (cached reads, cache creation, audio) per category — the upstream half of OPEN-11148 / OPEN-11150. The platform already readsprice_detailsas an optional field, so once this deploys, global per-category prices flow through automatically.Canonical keys (contract)
The
price_detailskeys are a contract matched by name to a request'susageDetails(SDK) and the platform's price map:pricinginput_tokensinput_cost_per_tokenpromptoutput_tokensoutput_cost_per_tokencompletioncached_tokenscache_read_input_token_costinput_cache_readcache_creation_tokenscache_creation_input_token_costinput_cache_writeaudio_input_tokensinput_cost_per_audio_tokenaudio_output_tokensoutput_cost_per_audio_tokenReasoning is intentionally omitted — it's billed at the output rate and already counted within
output_tokens, so it carries no separate price (producers surface it as an informational usage-only category). Per-token categories only; non-token costs (web search per-query, file search per-call, code interpreter per-session, vector store per-GB-day) are out of scope for now.Changes
entities/models(JSON column,004migration) /schemas/repositories/api: threadprice_detailsthrough. JSON maps to JSONB on Postgres and JSON on the SQLite test env; nullable for back-compat (read as{}).providers: extract per-category fields → canonical keys in both LiteLLM and OpenRouter providers.Backward compatibility
Purely additive — the two scalars stay,
price_detailsis nullable and defaults to{}. Consumers that ignore it are unaffected.Verification
gpt-4o→ input/output/cached;gpt-4o-audio-preview→ + audio_input/audio_output;o1→ input/output/cached (no reasoning price, as intended). Key coverage: cached=568, cache_creation=197, audio_input=111, audio_output=48.🤖 Generated with Claude Code