Skip to content

feat: add context_tiers for arbitrary tiered pricing based on context length#1324

Open
Pxys-io wants to merge 1 commit intoanomalyco:devfrom
Pxys-io:feat-context-tiers
Open

feat: add context_tiers for arbitrary tiered pricing based on context length#1324
Pxys-io wants to merge 1 commit intoanomalyco:devfrom
Pxys-io:feat-context-tiers

Conversation

@Pxys-io
Copy link
Copy Markdown
Contributor

@Pxys-io Pxys-io commented Apr 3, 2026

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_context thresholds.

Solution

New Schema: cost.context_tiers

An ordered array of pricing tiers, each with a min_context threshold. 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.

[cost]
input = 1.00
output = 3.00
cache_read = 0.20

# Tier 1: applies when input + cache_read >= 200,000 tokens
[[cost.context_tiers]]
min_context = 200_000
input = 2.00
output = 6.00
cache_read = 0.40

# Tier 2: applies when input + cache_read >= 500,000 tokens
[[cost.context_tiers]]
min_context = 500_000
input = 4.00
output = 12.00
cache_read = 0.80

Changes

File Change
packages/core/src/schema.ts Add ContextTier zod schema, context_tiers array field (max 4 tiers), validation that tiers are sorted by min_context ascending
providers/openrouter/models/xiaomi/mimo-v2-pro.toml Example usage with 3-tier pricing (base, 200K, 500K)

Backward Compatibility

  • context_over_200k is kept but marked as @deprecated
  • Consumers should prefer context_tiers when present, falling back to context_over_200k for legacy models
  • Existing models using context_over_200k continue to work unchanged

Consumer Changes (opencode)

A companion PR in opencode will:

  1. Parse context_tiers from models.dev into the internal Model type
  2. Replace the hardcoded 200_000 threshold in session/index.ts with dynamic tier selection
  3. Fall back to experimentalOver200K for legacy models

… 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>
@rekram1-node
Copy link
Copy Markdown
Contributor

It's an interesting api, i was also considering something like:

cost_over: {
200_000: {...},
300_000: {....}
}

@Pxys-io
Copy link
Copy Markdown
Contributor Author

Pxys-io commented Apr 3, 2026

It's an interesting api, i was also considering something like:

cost_over: {
200_000: {...},
300_000: {....}
}

Most I have seen is 128k and 256k etc, but I think flexible would be easier

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.

2 participants