Skip to content

OpenCode setup

OpenCode is an open-source, terminal-based AI coding agent. It runs locally on your machine, reads your project files, and interacts with a large language model (LLM) to help you write, refactor, and debug code. Because it runs in your terminal, you get interactive, real-time feedback — you can watch the agent work, approve tool calls, and steer it mid-task.

This section walks you through the full setup: installing OpenCode, connecting it to a model provider, configuring optional proxy endpoints, setting up workspace-level configuration, and verifying everything works.

OpenCode is distributed as an npm package. Install it globally so you can run it from any directory.

Terminal window
npm install -g @anthropic/opencode

Verify the installation:

Terminal window
opencode --version

Expected output:

opencode v0.1.x

If the command is not found, make sure your npm global bin directory is in your PATH. You can find the directory with:

Terminal window
npm config get prefix

The binary is located in the bin/ subdirectory of that path. Add it to your shell profile if it is not already there.

SymptomLikely causeFix
EACCES permission errorGlobal npm directory requires rootUse nvm to manage Node.js, or configure npm to use a user-writable directory with npm config set prefix ~/.npm-global
command not found after installnpm global bin not in PATHAdd export PATH="$(npm config get prefix)/bin:$PATH" to your ~/.zshrc or ~/.bashrc
Version mismatchMultiple Node.js installationsRun which node and which npm to confirm they point to the same installation

OpenCode needs access to a large language model to function. It connects to a model provider through an API key. OpenCode supports several providers out of the box, including Anthropic (Claude), OpenAI (GPT), and others.

The simplest way to configure a provider is through environment variables. Set the API key for your provider in your shell profile so it persists across sessions.

For Anthropic (Claude):

Terminal window
# Add to your ~/.zshrc or ~/.bashrc
export ANTHROPIC_API_KEY="<your-anthropic-api-key>"

For OpenAI:

Terminal window
# Add to your ~/.zshrc or ~/.bashrc
export OPENAI_API_KEY="<your-openai-api-key>"

After adding the variable, reload your shell:

Terminal window
source ~/.zshrc # or source ~/.bashrc

OpenCode selects a default model based on your configured provider. You can override this in your configuration file or at launch time. Common choices include:

Terminal window
# Launch OpenCode with a specific model
opencode --model claude-sonnet-4-20250514

Confirm OpenCode can reach your provider:

Terminal window
opencode doctor

Expected output (details vary by provider):

OpenCode Doctor
Provider: anthropic
Model: claude-sonnet-4-20250514
API Key: configured
Connectivity: ok

If the connectivity check fails, verify your API key is set correctly and that you can reach the provider’s API endpoint from your network (some corporate networks block outbound API calls).


Some organizations route model API traffic through a proxy or gateway for cost tracking, compliance, or access control. If your team uses a proxy endpoint, configure OpenCode to send requests through it instead of directly to the provider.

You need a proxy endpoint if:

  • Your organization provides model access through an internal gateway
  • You use a service like LiteLLM, AWS Bedrock, or Azure OpenAI that exposes an OpenAI-compatible API at a custom URL
  • Your network restricts direct access to model provider APIs

Set the base URL for your proxy using an environment variable:

Terminal window
# Add to your ~/.zshrc or ~/.bashrc
export OPENCODE_API_BASE_URL="https://your-gateway.example.com/v1"

If your proxy requires a separate API key (different from the provider key), set that as well:

Terminal window
export OPENCODE_API_KEY="<your-proxy-api-key>"

After configuring the proxy, run the doctor command again:

Terminal window
opencode doctor

The output should show your custom endpoint:

OpenCode Doctor
Provider: custom
Endpoint: https://your-gateway.example.com/v1
API Key: configured
Connectivity: ok

If connectivity fails through the proxy, test the endpoint directly with curl:

Terminal window
# Test that the proxy endpoint is reachable
curl -s -o /dev/null -w "%{http_code}" \
https://your-gateway.example.com/v1/models \
-H "Authorization: Bearer <your-api-key>"

A 200 response means the endpoint is reachable and your credentials work. A 401 means the API key is wrong. A connection timeout means the endpoint URL is incorrect or your network blocks the request.


OpenCode uses configuration files to customize its behavior for specific projects. This is separate from the context files you will learn about in Module 4 — workspace configuration controls OpenCode’s operational settings, not the context it uses to understand your codebase.

Create a configuration file in your project root to set project-specific defaults:

.opencode/config.json
{
"model": "claude-sonnet-4-20250514",
"approvalMode": "suggest",
"tools": {
"allowedCommands": ["npm test", "npm run build", "npm run lint"]
}
}

Key configuration options:

OptionDescriptionExample values
modelDefault model for this projectclaude-sonnet-4-20250514, gpt-4.1
approvalModeHow the agent handles tool callssuggest (ask first), auto (run automatically)
tools.allowedCommandsShell commands the agent can run without explicit approvalBuild, test, lint commands

OpenCode can operate at different levels of autonomy:

  • suggest (default): The agent shows you what it plans to do and waits for approval before executing any tool calls. This is the safest mode for getting started.
  • auto: The agent executes tool calls automatically without asking. Use this only for trusted operations in sandboxed environments.

Start with suggest mode. You can switch to auto once you are comfortable with how the agent behaves in your project.

Verify OpenCode reads your project configuration by running it from your project directory:

Terminal window
cd <your-project>
opencode

Inside the OpenCode session, ask it to confirm the configuration:

What model are you using and what is your current approval mode?

The agent should report the settings from your .opencode/config.json file.


At this point you should have:

  1. OpenCode installed globally and available in your PATH
  2. An API key configured for your model provider
  3. (Optional) A proxy endpoint configured if your organization requires one
  4. (Optional) A workspace configuration file for your project

Run through this quick checklist to confirm everything works:

Terminal window
# 1. Verify installation
opencode --version
# 2. Verify provider connectivity
opencode doctor
# 3. Start an interactive session
opencode

Inside the session, give OpenCode a simple task:

Create a file called hello.txt with the text "Hello from OpenCode"

If the agent creates the file, your setup is complete. Clean up the test file when you are done:

Terminal window
rm hello.txt