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:
- Users must manually set
limit.context for every model
- If the user changes the context size on the server, opencode doesn't know
- 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.)
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/modelsendpoint. Instead, it requireslimit.contextto be hardcoded in the config for each model.Problem
Local model servers (LM Studio, Unsloth Studio, etc.) expose
context_lengthfor the loaded model in their/modelsAPI response. However, opencode ignores this and requires static configuration. This means:limit.contextfor every modelExpected Behavior
opencode should query the provider's
/modelsendpoint at runtime and read thecontext_lengthfield 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(andlimit.output) inopencode.jsonfor each model definition underprovider.<name>.models.Suggested Solution
For OpenAI-compatible providers, fetch the
/modelsendpoint at connection time and use thecontext_lengthfrom the loaded model's metadata as the context window. This could be refreshed periodically or on model switch.Environment
@ai-sdk/openai-compatible