What Is OpenCode? Install and Connect a Custom Model in 5 Steps

Blog / Developer tools

What Is OpenCode? Install and Connect a Custom Model in 5 Steps

OpenCode is an open-source AI coding agent for the terminal. You start it in a project directory and use plain language to have a model read code, edit files, and run commands. Its defining trait is that it isn't tied to one model vendor: you can connect an official API or any compatible endpoint. This guide first explains what it is, then walks through five steps to install it and get a first conversation working.

The commands and interface described here follow the official OpenCode docs (opencode.ai). They may drift as versions change, so when something doesn't match, trust the official docs.

What is OpenCode, and how does it differ from Claude Code and Codex?

All three are "coding agents that work in the terminal," and day-to-day use is very similar: you enter a project directory, describe what you want, the tool reads and writes files and runs commands, and you can approve or reject each step.

The main difference is where the model comes from:

ToolDefault model sourceSwitching models or endpoints
Claude CodeAnthropic modelsEndpoint can be changed through environment variables or config
Codex CLIOpenAI modelsCustom providers can be configured
OpenCodeNot tied to any vendor; open sourceConfigured per provider; several can coexist

So if you want to switch between models from different vendors in one tool, or run the tool against your own endpoint, OpenCode's configuration model is more direct. If you're still deciding between Claude Code and Codex, start with Claude Code vs. Codex CLI: how to choose.

Step 1: What do you need before installing?

  • A terminal (macOS or Linux, or WSL on Windows; check the official docs for exact platform support).
  • If you install with npm, Node.js must be installed first. Check with node -v. The environment section of the Claude Code installation guide covers Node setup, and the approach is the same.
  • A source of models: an official API key or a compatible third-party endpoint. Without one, OpenCode installs fine but can't hold a conversation.

Step 2: How do you install it?

The project offers several install methods. The most common is npm:

npm i -g opencode-ai

There are also an official install script and package-manager options for each platform. Copy the exact command from the install page of the official docs rather than from a third-party article, because script URLs and package names can change.

After installing, verify:

opencode --version

If it prints a version number, the install worked. If the shell says the command isn't found, the global npm directory is usually missing from your PATH. Check whether the bin directory under npm config get prefix is in PATH.

Step 3: What do you see on first run?

Go to a project directory you want it to work on and run:

cd your-project
opencode

It opens a terminal UI (TUI) with an input box at the bottom. For the first run, pick a small project or a fresh test directory to get familiar with the workflow. Don't point it at an important repository right away.

A few entry points worth knowing first (slash commands; check the official docs for the full list):

  • /connect: add a model provider and enter your key.
  • /models: list and switch the models currently available.
  • /init: have it analyze the project and generate a project description file for later conversations to reference.

Step 4: How do you connect a model?

There are two routes.

Route 1: pick a built-in provider with /connect. Choose a vendor from the list, paste your API key when prompted, then pick a model with /models. This suits you if you already hold an official key.

Route 2: a custom provider. If you use a third-party endpoint compatible with the OpenAI or Anthropic format, you need to add a provider block to opencode.json with the endpoint address (baseURL), the key, and the model list. There are two ways to write it, and it's also where "configured but the model doesn't show up" problems happen most often. Both are covered item by item in OpenCode custom provider configuration, so they aren't repeated here. To compare how Claude Code, Codex, and OpenCode each configure their endpoint address, see Custom API and endpoint configuration.

Step 5: How do you confirm it really works?

In your test directory, enter a request with no side effects, for example: "List the files in the current directory and describe in one sentence what this project does."

Check three things:

  1. You get a normal streamed reply with no errors.
  2. It calls a tool such as reading files and asks for your confirmation.
  3. /models shows the model you expected.

If something fails, work through this order:

SymptomCheck first
401 / authentication failedWhether the key was copied in full and entered under the right provider
404Whether baseURL has an extra or missing path segment (such as /v1)
Timeouts or slownessWhether your network can reach that endpoint. You can use the cdnping online speed test to see connection times from your network to various AI APIs; the background is in AI API latency from China
Model missing from the listWhether the model ID in the provider config is spelled correctly
Occasional 5xxCheck the API status page to see whether the provider is having problems

No overseas credit card: where can you get a model source?

This is where beginners in China most often get stuck: the tool is installed, but official APIs require an overseas payment method, and some services also have regional restrictions. Follow each service's terms and local regulations, and don't try to work around the restrictions.

One compliant option is an aggregation gateway. AGI Plan (agiplan.dev) is a multi-provider LLM aggregation gateway. It is billed by monthly subscription, paid in RMB, with no overseas credit card needed. It is compatible with both the OpenAI and Anthropic API formats, so in OpenCode you just fill in the endpoint address and key using the "custom provider" method above. Setup is roughly three steps: sign up with an emailed verification code, choose a plan and create a key, and change one setting. One subscription includes several capability groups (frontier reasoning, balanced general-purpose, China flagship models, and fast lightweight models), and you can switch between them within the same quota. Note that the service states plainly that it is not an officially authorized channel, and some models are marked "overseas only"; see the AGI Plan models page for specifics. AGI Plan is a commercial partner of cdnping. You can find step-by-step tutorials on the AGI Plan getting-started page, or treat it as just one alternative and compare options yourself before deciding.

If what you want is Codex but you don't have a ChatGPT account, see Can you use Codex without a ChatGPT account?. The approach is similar.

What to do next

  1. Complete the Step 5 check in the test directory first.
  2. In a real project, run /init to generate a project description, then ask it for one small change and review its edits step by step.
  3. Put your provider config into opencode.json and adjust as needed; see custom provider configuration for details.
  4. Keep an eye on usage: a coding agent can burn more tokens than you'd expect. See how to estimate token consumption.

Related articles

Keep reading