Skip to main content

Overview

Type: Coding ToolPrimary Path: OpenAI-compatibleSupport Confidence: Supported path
OpenCode is an open-source AI coding assistant that runs in your terminal. It supports multiple LLM providers and can be configured to use AI Sonar’s API for access to hundreds of models.

Installation

curl -fsSL https://opencode.ai/install | bash
Verify installation:
opencode --version

Configuration

Step 1: Set Environment Variables

export OPENAI_API_KEY="sk-your-api-key"
For permanent configuration, add to ~/.bashrc or ~/.zshrc:
echo 'export OPENAI_API_KEY="sk-your-api-key"' >> ~/.zshrc
source ~/.zshrc

Step 2: Configure OpenCode

OpenCode’s current docs recommend configuring providers through opencode.json or opencode.jsonc. For AI Sonar, use a custom provider instead of overloading the built-in openai provider:
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "aisonar": {
      "npm": "@ai-sdk/openai-compatible",
      "name": "AI Sonar",
      "options": {
        "baseURL": "https://api.aisonar.dev/v1",
        "apiKey": "{env:OPENAI_API_KEY}"
      },
      "models": {
        "gpt-5.4": {
          "name": "GPT-5.4"
        },
        "gpt-5-mini": {
          "name": "GPT-5 Mini"
        }
      }
    }
  },
  "model": "aisonar/gpt-5.4"
}
Use @ai-sdk/openai-compatible for chat-completions style compatibility. If you specifically need a provider path built on /v1/responses, OpenCode’s provider docs recommend switching the package to @ai-sdk/openai.

Basic Usage

Start interactive mode:
opencode
Run with a prompt:
opencode "Explain this codebase"
Specify model:
opencode --model aisonar/gpt-5.4 "Fix the bugs in main.py"

Available Models

ModelBest For
gpt-5.4Complex tasks, code architecture
gpt-5-miniQuick fixes, simple queries
claude-sonnet-4-6Code review, documentation
claude-opus-4-6Complex reasoning
gemini-2.5-flashFast responses
deepseek-r1Algorithm design

Common Commands

Analyze code:
opencode "What does this function do?" < src/utils.ts
Generate code:
opencode "Create a REST API with Express"
Review changes:
git diff | opencode "Review these changes"
Fix errors:
opencode "Fix the TypeScript errors in this project"

Interactive Commands

CommandDescription
/helpShow available commands
/model <name>Switch to a different model
/clearClear conversation history
/exitExit OpenCode

Troubleshooting

  • Verify options.baseURL is set to https://api.aisonar.dev/v1
  • Check network connectivity
  • Try curl https://api.aisonar.dev/v1/models to test
  • Verify OPENAI_API_KEY environment variable is set
  • Verify options.apiKey references {env:OPENAI_API_KEY} or another valid secret source
  • Check that the key starts with sk-
  • Ensure the key is active in AI Sonar dashboard

Best Practices

Run OpenCode from your project root for better understanding of your codebase.
Use faster models (gpt-5-mini) for simple tasks and stronger models (gpt-5.4, claude-sonnet-4-6) for complex ones.
Always review AI-generated code before applying changes to your project.