Models Hub
Agent Tools

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.

Edit this page

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

Use caseModelPricingLive status
Everyday codingclaude-sonnet-4-6PriceAvailability and latency
Harder reasoning and long contextgpt-5.5PriceAvailability and latency
Cheap and fast for small editsgpt-5.4-miniPriceAvailability 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.json in 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

FieldDescription
provider.<id>.npm@ai-sdk/anthropic for Claude, @ai-sdk/openai-compatible for the OpenAI-compatible protocol
provider.<id>.options.baseURLBoth 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>.modelsModel IDs must match the Models Hub console; name is just a display alias
Top-level modelThe 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

SymptomCauseFix
Models Hub models don't show under /modelsprovider.*.models is empty in opencode.json, or the JSON is malformedCheck the config syntax, then restart OpenCode
Model not foundThe model ID doesn't match the Models Hub consoleCheck the name on the pricing page
401 / invalid API keyapiKey in opencode.json or auth.json is wrongRerun opencode auth login, or confirm MODELSOK_API_KEY is exported
Protocol error calling a Claude modelThe model is configured under the wrong providerConfirm the Claude model is under the npm: @ai-sdk/anthropic provider
Config changes have no effectProject-level config takes priority, or OpenCode wasn't restartedCheck 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.

On this page