Guides

Use AI Hub with OpenCode

Connect OpenCode to AI Hub with a custom provider and the OpenAI-compatible Chat Completions adapter.

1. Prepare your workspace

Create a workspace inference key in API keys in this AI Hub environment. Give the agent its own key and choose a spend cap. Use the API base below with a key from the same environment; a console login, OpenRouter key or vendor key cannot replace it.

Choose an exact author/slug ID from Models or the catalogue for this environment:

bash
curl --fail "https://tokenhub-api.belive.sg/api/v1/models"

The example uses qwen/qwen3-coder-flash. Check tools,tool_choice and context_length on the Models pagefor your selected model before starting a session.

2. Connect your key

Install OpenCode using its official getting-started guideand run opencode in your project. Enter /connect, choose Other, enter provider ID tokenhub, then supply your AI Hub inference key in the credential prompt. This stores a credential; the endpoint still needs the configuration below. Keep the credential store private and out of source control.

3. Configure the provider and model

Merge this into your project's opencode.json:

json
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "tokenhub": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "AI Hub",
      "options": { "baseURL": "https://tokenhub-api.belive.sg/api/v1" },
      "models": {
        "qwen/qwen3-coder-flash": { "name": "Qwen3 Coder Flash" }
      }
    }
  },
  "model": "tokenhub/qwen/qwen3-coder-flash",
  "small_model": "tokenhub/qwen/qwen3-coder-flash"
}

Use /models to select the configured model. The provider ID must match /connect. The tokenhub/ prefix is local to OpenCode; the catalogue model ID sent to AI Hub is qwen/qwen3-coder-flash. @ai-sdk/openai-compatible selects Chat Completions. Do not switch this recipe to the Responses API adapter.

The explicit small_model keeps lightweight tasks on the same selected model. Review global config and agent-level model overrides too. To use an environment secret instead of /connect, set options.apiKey to the literal {env:TOKENHUB_API_KEY} and supply that variable to the OpenCode process. Never put a real key in project JSON or a shell command saved in history.

Try a bounded task in a disposable project that reads a file and runs a test. Verify the result and check Activity for model, usage and cost. To change models, add another exact catalogue ID under provider.tokenhub.modelsand update the selection and any intended overrides.

Troubleshooting

  • Authentication failure: check the key is active in the correct workspace and environment, and available to the process running the client. Never print the key to diagnose it.
  • Model missing or rejected: copy the exact catalogue ID, check key model restrictions, and check the client's selected provider and model.
  • 404 or transport error: use the API base shown here, including its version prefix, without appending /chat/completions. Select Chat Completions transport.
  • Tool or context errors: check supported_parameters and context_length in the catalogue; reduce the session context or output cap. See Errors for gateway responses.
  • Unexpected service or missing Activity: review auxiliary models, fallbacks and local overrides. OpenRouter presets, aliases and plugins are not interchangeable with AI Hub.

Client support

Use OpenCode's custom-provider guide and configuration reference for client settings.