Use OpenCode with Models Hub: /connect and opencode.json
Add Models Hub to OpenCode interactively with /connect, or edit opencode.json to declare Anthropic and OpenAI-compatible providers, with verification and error troubleshooting.
OpenCode is an open-source terminal AI coding agent that runs in TUI and CLI modes, with built-in @ai-sdk/anthropic and @ai-sdk/openai-compatible adapters that can each connect to Models Hub's Claude models and OpenAI-compatible models. This page covers the connection only, not how to use OpenCode itself.
Three things you need
A Models Hub API key, the base URL https://modelsok.com/v1, and a model name. There's no one-click setup — OpenCode isn't one of the targets the Models Hub CLI supports.
Prerequisites
- OpenCode installed. See the official install instructions if you haven't.
- A Models Hub API key, created on the Tokens page. The token's group must have access to the models you plan to use.
Recommended models
| Use case | Model | Pricing | Live status |
|---|---|---|---|
| Everyday coding | claude-sonnet-4-6 | Price | Availability and latency |
| Harder reasoning and long context | gpt-5.5 | Price | Availability and latency |
| Cheap and fast for small edits | gpt-5.4-mini | Price | Availability and latency |
Model names are authoritative on the pricing page.
Option 1: interactive setup
Launch OpenCode and type /connect, choose Other (custom endpoint) from the provider list, then fill in:
- Base URL:
https://modelsok.com/v1 - API Key: your Models Hub API key
The key is saved to ~/.local/share/opencode/auth.json. Type /models to see and switch to the newly added model.
Option 2: edit the config file
Config file locations:
- Global:
~/.config/opencode/opencode.json - Project-level (higher priority):
opencode.jsonin the project root
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"modelsok-anthropic": {
"npm": "@ai-sdk/anthropic",
"name": "Models Hub (Anthropic)",
"options": {
"baseURL": "https://modelsok.com/v1",
"apiKey": "{env:MODELSOK_API_KEY}"
},
"models": {
"claude-sonnet-4-6": {
"name": "Claude Sonnet 4.6",
"limit": { "context": 200000, "output": 8192 }
}
}
},
"modelsok": {
"npm": "@ai-sdk/openai-compatible",
"name": "Models Hub",
"options": {
"baseURL": "https://modelsok.com/v1",
"apiKey": "{env:MODELSOK_API_KEY}"
},
"models": {
"gpt-5.5": {
"name": "GPT-5.5",
"limit": { "context": 128000, "output": 32768 }
},
"gpt-5.4-mini": {
"name": "GPT-5.4 Mini",
"limit": { "context": 128000, "output": 16384 }
}
}
}
},
"model": "modelsok-anthropic/claude-sonnet-4-6"
}Key fields
| Field | Description |
|---|---|
provider.<id>.npm | @ai-sdk/anthropic for Claude, @ai-sdk/openai-compatible for the OpenAI-compatible protocol |
provider.<id>.options.baseURL | Both providers use https://modelsok.com/v1 |
provider.<id>.options.apiKey | {env:MODELSOK_API_KEY} references an environment variable so you don't hardcode the key; a plain string also works |
provider.<id>.models | Model IDs must match the Models Hub console; name is just a display alias |
Top-level model | The default model, in <provider>/<model> format |
If you reference an environment variable, export MODELSOK_API_KEY="your-models-hub-api-key" before launching OpenCode, or run opencode auth login afterward and enter the key for modelsok-anthropic and modelsok when prompted. Project-level opencode.json overrides the global config.
Verify
Launch OpenCode and type /models to confirm you can see modelsok-anthropic/claude-sonnet-4-6, modelsok/gpt-5.5, and similar entries. Pick one and send a test message; a normal reply means you're connected. Then open Usage logs in the Models Hub console; a request under your configured model name appears within seconds.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
Models Hub models don't show under /models | provider.*.models is empty in opencode.json, or the JSON is malformed | Check the config syntax, then restart OpenCode |
| Model not found | The model ID doesn't match the Models Hub console | Check the name on the pricing page |
401 / invalid API key | apiKey in opencode.json or auth.json is wrong | Rerun opencode auth login, or confirm MODELSOK_API_KEY is exported |
| Protocol error calling a Claude model | The model is configured under the wrong provider | Confirm the Claude model is under the npm: @ai-sdk/anthropic provider |
| Config changes have no effect | Project-level config takes priority, or OpenCode wasn't restarted | Check for another opencode.json in the project root, then restart |
More in Troubleshooting.
FAQ
How is it billed? Per input and output token actually consumed, at the price on each model's pricing page; every request is itemized in usage logs.
How is this different from using the official APIs directly? OpenCode's adapter sends the request, and the Models Hub gateway forwards it to the right model — you don't need separate Anthropic or OpenAI accounts, and one key covers both protocol paths.
How do I switch models? Type /models and pick a configured model, or edit the top-level model field in opencode.json and restart.
Related
- CLI one-command setup: Claude Code, Codex, Gemini CLI and OpenClaw support one-click configuration
- Model pricing and model status
- Crush setup, Oh My Pi setup
Use Cline with Models Hub: OpenAI Compatible and Anthropic Setup
Connect the VS Code extension Cline to Models Hub via either OpenAI Compatible or Anthropic provider, with field details, verification steps, and error troubleshooting.
Use CodeBuddy CLI with Models Hub: Env Vars and settings.json
Point Tencent Cloud's CodeBuddy CLI at Models Hub's Claude models using CODEBUDDY_BASE_URL and related environment variables or settings.json, with verification and error troubleshooting.