Skip to content

feat(opencode): OpenRouter model discovery/pruning at runtime#20004

Open
Randomblock1 wants to merge 5 commits intoanomalyco:devfrom
Randomblock1:fix-openrouter
Open

feat(opencode): OpenRouter model discovery/pruning at runtime#20004
Randomblock1 wants to merge 5 commits intoanomalyco:devfrom
Randomblock1:fix-openrouter

Conversation

@Randomblock1
Copy link
Copy Markdown

@Randomblock1 Randomblock1 commented Mar 30, 2026

Issue for this PR

Closes #4734
Fixes #279
Fixes #10594
Fixes #15962

And probably quite a few more...

Type of change

  • New feature

What does this PR do?

OpenCode currently relies on Models.dev as the source of truth for available OpenRouter models. This has caused many issues for users because the catalog and limited-time free models changes daily. Additionally, some model IDs that have been removed from OpenRouter are still in Models.dev (for example, qwen3-4b:free).

This is because Models.dev appears to have no automation (!?!?). If a new model is added to OpenRouter, someone has to go make a commit or PR, for EVERY model, and every free promotion. There are hundreds of models on OpenRouter (the point is to have all of them) so this is not a good solution by itself.

Personally, I would STRONGLY recommend Models.dev use a Github Action or something to check every few hours, and either create a PR to remove old/deprecated models or create a draft for a new model (which would then be manually filled out and verified). You'll have to add discovery for each major provider but that's really not that hard, or you could only use OpenRouter since it lists almost all models.

This PR implements model discovery at runtime for OpenRouter. OpenCode now fetches the live catalog directly from the OpenRouter Models API, (in addition to using Models.dev) ensuring all models are at least usable. A cache ensures that this does not impact startup performance much.

  • Discovery: Fetches the live list and stores it in a cache file (refreshed every 60m or manually).
  • Pruning: Removes models from the Models.dev list if they are no longer present in OpenRouter.
  • Mapping: Fetches pricing, capabilities, modalities, etc. from the OpenRouter API. (Dynamic pricing like the auto router is just set to 0).

This is a fallback mechanism, not a complete replacement. High-quality, manually verified metadata in Models.dev is preferred, and in some cases, used for discovered models (OpenRouter API does not include interleaved info, so for example, minimax-m2.5 data is used to set interleaved for minimax-m2.5:free, as it errors after 1 message without it).

As a result of the auto-mapping, some models may not have correctly optimized settings, for example alibaba/tongyi-deepresearch-30b-a3b (which is a finetuned Qwen3) will use default settings instead of Qwen3 optimized ones.

In addition to the OpenRouter discovery code, I refactored the core provider initialization to be provider-agnostic. GitLab-specific discovery logic was replaced with a generic loop that automatically handles discoverModels for any provider that implements it. I had to add some OpenRouter-specific code there, but theoretically that could be made generic too, if other providers implement model pruning.

How did you verify your code works?

Diff the models it lists from those in the OpenRouter API:

diff \
  <(opencode models --refresh | grep "^openrouter/" | cut -d '/' -f 2- | sort) \
  <(curl -s https://openrouter.ai/api/v1/models | jq -r '.data[].id' | sort )

There's nothing, which means all OpenRouter models are accessible in OpenCode.

I was able to use free endpoints like minimax-m2.5:free in OpenCode which is not in Models.dev, and was not visible or usable in the main branch.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

If you do not follow this template your PR will be automatically rejected.

@github-actions github-actions bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Mar 30, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

# Conflicts:
#	packages/opencode/src/provider/provider.ts
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.

Model unavailable No endpoints found error from Openrouter -> Should be able to re-fetch/update model list [FEATURE]: Refresh models manually

1 participant