Skip to content

feat: support context_tiers for arbitrary tiered pricing#20798

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

feat: support context_tiers for arbitrary tiered pricing#20798
Pxys-io wants to merge 1 commit intoanomalyco:devfrom
Pxys-io:feat-context-tiers-consumer

Conversation

@Pxys-io
Copy link
Copy Markdown

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

Problem

OpenCode currently uses a hardcoded 200_000 token threshold to switch between base and long-context pricing (experimentalOver200K). This doesn't match how providers like OpenRouter handle tiered pricing, which supports arbitrary context length thresholds.

Solution

This PR adds support for the new context_tiers schema from models.dev#1324, replacing the hardcoded threshold with dynamic tier selection.

Changes

File Change
src/provider/models.ts Add context_tiers array to ModelsDev cost schema
src/provider/provider.ts Add contextTiers to internal Model cost type; parse tiers from models.dev
src/session/index.ts Replace hardcoded 200_000 check with dynamic tier selection — picks the highest tier where input + cache_read >= min_context

How It Works

const costInfo = (() => {
  const totalInputTokens = tokens.input + tokens.cache.read;

  // Prefer new context_tiers with arbitrary thresholds
  if (input.model.cost?.contextTiers?.length > 0) {
    let selected = input.model.cost; // base cost
    for (const tier of input.model.cost.contextTiers) {
      if (totalInputTokens >= tier.min_context) {
        selected = tier;  // highest matching tier wins
      }
    }
    return selected;
  }

  // Fall back to legacy experimentalOver200K (hardcoded 200K)
  if (input.model.cost?.experimentalOver200K && totalInputTokens > 200_000) {
    return input.model.cost.experimentalOver200K;
  }

  return input.model.cost;
})();

Backward Compatibility

  • Falls back to experimentalOver200K for models using the legacy context_over_200k field
  • Falls back to base cost if neither is defined
  • No breaking changes to existing models or cost calculations

Companion PR

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>
@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Apr 3, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 3, 2026

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 3, 2026

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Apr 3, 2026
@github-actions github-actions bot closed this Apr 3, 2026
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.

1 participant