Skip to content

Feature: Dynamic context length detection for OpenAI-compatible providers #40908

Description

@PGCRT

Summary

When using a custom OpenAI-compatible provider (via @ai-sdk/openai-compatible), opencode does not dynamically detect the context length of the currently loaded model from the server's /models endpoint. Instead, it requires limit.context to be hardcoded in the config for each model.

Problem

Local model servers (LM Studio, Unsloth Studio, etc.) expose context_length for the loaded model in their /models API response. However, opencode ignores this and requires static configuration. This means:

  1. Users must manually set limit.context for every model
  2. If the user changes the context size on the server, opencode doesn't know
  3. Different models may have different context lengths, requiring constant config updates

Expected Behavior

opencode should query the provider's /models endpoint at runtime and read the context_length field to determine the appropriate context window for the currently loaded model.

Example Server Response

{
  "object": "list",
  "data": [
    {
      "id": "DavidAU/Qwen3.5-9B-...",
      "object": "model",
      "owned_by": "unsloth-studio",
      "context_length": 262144,
      "max_context_length": 208128,
      "native_context_length": 262144,
      "loaded": true
    }
  ]
}

Current Workaround

Users must manually specify limit.context (and limit.output) in opencode.json for each model definition under provider.<name>.models.

Suggested Solution

For OpenAI-compatible providers, fetch the /models endpoint at connection time and use the context_length from the loaded model's metadata as the context window. This could be refreshed periodically or on model switch.

Environment

  • Provider type: @ai-sdk/openai-compatible
  • Local server: Unsloth Studio (also applies to LM Studio, Ollama, etc.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions