feat: add context_tiers for arbitrary tiered pricing based on context length#1324
Open
Pxys-io wants to merge 1 commit intoanomalyco:devfrom
Open
feat: add context_tiers for arbitrary tiered pricing based on context length#1324Pxys-io wants to merge 1 commit intoanomalyco:devfrom
Pxys-io wants to merge 1 commit intoanomalyco:devfrom
Conversation
… length Replace the hardcoded context_over_200k with a flexible context_tiers array that allows providers to define pricing tiers at arbitrary token thresholds, matching OpenRouter's tiered pricing format. - Add ContextTier schema with min_context, input, output, cache_read, cache_write - Keep backward compatibility with deprecated context_over_200k - Add validation that tiers must be sorted by min_context ascending - Update xiaomi/mimo-v2-pro with example 3-tier pricing (base, 200k, 500k) OpenRouter docs: https://openrouter.ai/docs/guides/get-started/for-providers Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
Pxys-io
added a commit
to Pxys-io/opencode
that referenced
this pull request
Apr 3, 2026
Update cost calculation to use the new context_tiers array from models.dev instead of the hardcoded 200K threshold. - Add contextTiers to Model cost schema (provider.ts) - Parse context_tiers from models.dev ModelsDev schema (models.ts) - Replace hardcoded 200_000 check in session/index.ts with dynamic tier selection — picks highest tier where totalInputTokens >= min_context - Backward compatible: falls back to experimentalOver200K for legacy models Companion PR: anomalyco/models.dev#1324 Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
Contributor
|
It's an interesting api, i was also considering something like: cost_over: { |
Contributor
Author
Most I have seen is 128k and 256k etc, but I think flexible would be easier |
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.
Problem
models.dev currently supports tiered pricing through
cost.context_over_200k, which is hardcoded to a 200K token threshold. However, OpenRouter and other providers support tiered pricing at arbitrary context lengths (e.g., 200K, 500K, 1M), where each tier has different per-token rates.This feature generalizes the tiered pricing system to match OpenRouter's tiered pricing format, where tiers are defined as an array with
min_contextthresholds.Solution
New Schema:
cost.context_tiersAn ordered array of pricing tiers, each with a
min_contextthreshold. When the total input tokens (input + cache_read) meet or exceed a tier's threshold, that tier's pricing applies. Only the highest matching tier is used.Changes
packages/core/src/schema.tsContextTierzod schema,context_tiersarray field (max 4 tiers), validation that tiers are sorted bymin_contextascendingproviders/openrouter/models/xiaomi/mimo-v2-pro.tomlBackward Compatibility
context_over_200kis kept but marked as@deprecatedcontext_tierswhen present, falling back tocontext_over_200kfor legacy modelscontext_over_200kcontinue to work unchangedConsumer Changes (opencode)
A companion PR in opencode will:
context_tiersfrom models.dev into the internal Model type200_000threshold insession/index.tswith dynamic tier selectionexperimentalOver200Kfor legacy models