Skip to content

feat(core): discover context limits for custom OpenAI-compatible providers - #53236

Closed
woodsonl wants to merge 2 commits into
anomalyco:devfrom
woodsonl:fix/openai-compatible-model-limits
Closed

woodsonl wants to merge 2 commits into
anomalyco:devfrom
woodsonl:fix/openai-compatible-model-limits

Conversation

@woodsonl

@woodsonl woodsonl commented Oct 4, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #53235

Type of change

  • New feature

What does this PR do?

Custom @ai-sdk/openai-compatible providers are not in models.dev, so without a hand-written limit block OpenCode has no context window for their models and auto-compaction cannot know when to fire. Long sessions grow until the upstream rejects them with context_length_exceeded.

Most OpenAI-compatible servers already advertise per-model limits on GET {baseURL}/models (vLLM, LM Studio, LiteLLM, Ollama, OpenRouter). This PR fills the missing fields from that endpoint.

packages/core/src/plugin/provider/openai-compatible-limits.ts registers a catalog transform:

  • applies to models whose api package is @ai-sdk/openai-compatible and that have a baseURL
  • fills only empty fields; explicit config limit wins per field; existing values (models.dev) are never overwritten
  • one fetch per endpoint (grouped by baseURL and credentials), parallel across endpoints, 4s timeout, 5min success cache, 60s negative cache so a dead endpoint does not slow catalog rebuilds
  • fails open: an unreachable or malformed endpoint changes nothing and never blocks startup

How did you verify your code works?

  • 12 tests in packages/core/test/openai-compatible-limits*.test.ts: parsing, auth header precedence, merge rules, and two transform-level tests driving the real plugin effect against a fake catalog, config layer and fetch
  • full core suite: 1112 pass; tsgo --noEmit clean; oxlint 0 errors
  • to reproduce: point an openai-compatible provider at a server that advertises limits (vLLM works), omit limit; the model picks up the advertised window

Screenshots / recordings

N/A (no UI change)

Checklist

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

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Oct 4, 2026
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Oct 4, 2026
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

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

…iders

Custom @ai-sdk/openai-compatible providers are not in the models.dev
catalog, so models without a manual config limit carry no context window.
Auto-compaction then never fires and long sessions grow until the
upstream rejects them with context_length_exceeded.

Most OpenAI-compatible servers already advertise per-model limits on
GET {baseURL}/models. Fill the missing limit fields from that endpoint:
config limit wins per field, models.dev values are never overwritten,
discovery fails open with a short negative cache.
…t discovery

Drive the real plugin effect with a fake catalog draft, config layer and
fetch: fills empty limits, leaves config-owned and non-empty fields
alone, and fails open per endpoint.
@woodsonl
woodsonl force-pushed the fix/openai-compatible-model-limits branch from 57adba8 to 111cf31 Compare October 6, 2026 21:12
@thdxr

thdxr commented Oct 10, 2026

Copy link
Copy Markdown
Member

This branch has been upgraded to V2. We're no longer taking PRs for V1. If you think this is still relevant, please port it over and open it for V2.

— from 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊

@thdxr thdxr closed this Oct 10, 2026
woodsonl added a commit to woodsonl/opencode that referenced this pull request Oct 27, 2026
…iders

Custom OpenAI-compatible providers are not in the models.dev catalog, so
their models keep the built-in placeholder limits and auto-compaction
fires against a guess instead of the real window. Request
GET {baseURL}/models once per endpoint and fill context, input, and
output from the advertised context_length, max_input_tokens, and
max_output_tokens when those fields are still placeholders.

Configured limits win per field through the config provider transform
that runs after this plugin, and limits from models.dev or dedicated
discovery plugins are never replaced. Discovery fails open: an
unreachable or malformed endpoint keeps the last known limits and never
blocks the catalog.

Ports anomalyco#53236 to V2.
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.

[FEATURE]: Fill context limits for OpenAI-compatible providers from their /models advertisement

2 participants