Skip to content

Connect Claude (Anthropic)

Claude is Anthropic's large language model, excellent at complex coding tasks with a 200K tokens context window.

In regions with network restrictions, you typically need to complete 1.3 Network Configuration first, otherwise you may experience connection timeouts.


What You'll Be Able to Do

  • Get an Anthropic API Key and configure it in OpenCode
  • Understand the features of Claude 3.5 Sonnet, Claude 3.7 Sonnet, and other models
  • Complete your first conversation using Claude models

Your Current Situation

You might have already:

  • Heard about Claude's strong code generation capabilities
  • Have an Anthropic API Key or Claude Max subscription
  • Want to use Claude models in OpenCode

But don't know how to configure it, or which model to choose.

When to Use This

  • You have an Anthropic API Key or Claude Max subscription
  • You need Claude's long context capabilities (200K tokens)
  • You have high requirements for code quality and reasoning ability

🎒 Before You Start

Prerequisites

  • Anthropic Account: Register at console.anthropic.com
  • API Key or Claude Max subscription: Choose one
  • Network Environment: Need access to Anthropic API (may require VPN)

Core Concept

The workflow for configuring Anthropic Claude provider:

Get API Key → Configure in OpenCode → Verify Connection

Follow Along

Step 1: Get Anthropic API Key

Visit the Anthropic console to get your API Key:

  1. Open console.anthropic.com
  2. Log in or register an account
  3. Click API KeysCreate Key
  4. Copy the generated API Key (format: sk-ant-...)

Security Tips

  • The API Key is only shown once, save it immediately
  • Do not commit the API Key to code repositories
  • Rotate API Keys regularly

You should see: A string similar to sk-ant-api03-xxxx-xxxx-xxxx

Step 2: Configure in OpenCode

OpenCode provides two configuration methods, choose the one that suits you:

Set the API Key as an environment variable:

bash
export ANTHROPIC_API_KEY=sk-ant-api03-your-key-here
opencode
bash
# Add to ~/.bashrc or ~/.zshrc
echo 'export ANTHROPIC_API_KEY=sk-ant-api03-your-key-here' >> ~/.zshrc
source ~/.zshrc
opencode
powershell
$env:ANTHROPIC_API_KEY="sk-ant-api03-your-key-here"
opencode
powershell
# Add to system environment variables
[System.Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", "sk-ant-api03-your-key-here", "User")

Method 2: Use OpenCode Auth Command

Run the authentication command, OpenCode will save the API Key locally:

bash
opencode auth login

Then:

  1. Select Anthropic
  2. Paste the API Key
  3. Press Enter to complete

You should see: Done or Login successful

Auth Storage Location

OpenCode stores authentication information at:

  • All platforms: ~/.local/share/opencode/auth.json (follows XDG specification)

Step 3: Verify Configuration

Verify if the Anthropic provider is configured successfully:

bash
opencode auth list

You should see:

Credentials ~/.local/share/opencode/auth.json
● Anthropic api

Environment
● Anthropic ANTHROPIC_API_KEY

This indicates you have successfully configured the Anthropic provider.

Step 4: View Available Models

View all models provided by Anthropic:

bash
opencode models

You should see something like:

anthropic/claude-3-5-haiku-20241022
anthropic/claude-3-5-sonnet-20241022
anthropic/claude-3-7-sonnet-20250219
anthropic/claude-opus-4-20250514
...

View Complete Model Information

Use opencode models --verbose to see detailed metadata like model names and costs.

Step 5: Have Your First Conversation

Start OpenCode and select a Claude model:

bash
cd /path/to/your/project
opencode

In the OpenCode interface, enter your first question:

Help me write a simple HTTP server

You should see:

  1. AI starts generating a response (using Claude model)
  2. May invoke tools to create files
  3. Provides complete code and explanation

Claude Model Selection Guide

Anthropic provides multiple Claude models, choose based on your needs:

ModelModel IDContext WindowFeaturesBest For
Claude 3.7 Sonnetclaude-3-7-sonnet-20250219200KLatest and most powerful, excellent reasoningComplex tasks, code generation, refactoring
Claude 3.5 Sonnetclaude-3-5-sonnet-20241022200KBalanced performance and costDaily development, code review
Claude Opus 4claude-opus-4-20250514200KFlagship performance, most expensiveUltra-complex tasks, research work
Claude 3.5 Haikuclaude-3-5-haiku-20241022200KFast response, low costSimple tasks, quick Q&A

Recommended Choices

  • Default choice: Claude 3.7 Sonnet (most powerful)
  • Cost-sensitive: Claude 3.5 Sonnet (best value)
  • Fast response: Claude 3.5 Haiku (fastest)

Checklist ✅

After completing the above steps, you should be able to:

  • [ ] See Anthropic provider in opencode auth list
  • [ ] See Claude series models in opencode models
  • [ ] Start OpenCode and have a conversation using Claude model
  • [ ] See the model return with Claude identification

Common Issues

Invalid API Key

Symptoms: Shows Authentication failed or Invalid API Key

Causes:

  • API Key format error (doesn't start with sk-ant-)
  • API Key expired or revoked
  • API Key doesn't have permission to access the specified model

Solutions:

  1. Check if API Key format is correct
  2. Regenerate API Key in Anthropic console
  3. Confirm account has sufficient balance or valid subscription

Network Connection Failed

Symptoms: Shows Network error or Connection timeout

Causes:

  • Cannot access Anthropic API (may need VPN)
  • Unstable network

Solutions:

  1. Check network connection
  2. Configure proxy (if needed):
bash
export HTTP_PROXY=http://your-proxy:port
export HTTPS_PROXY=http://your-proxy:port

Model Unavailable

Symptoms: Shows Model not found or model list is empty

Causes:

  • API Key doesn't have permission to access this model
  • Model has been discontinued or renamed

Solutions:

  1. Check model access permissions in Anthropic console
  2. Use opencode models to view available models
  3. Upgrade account plan to access more models

Lesson Summary

In this lesson you learned:

  1. Get API Key: Obtain API Key from Anthropic console
  2. Configure Authentication: Use environment variable or opencode auth login
  3. Verify Connection: Confirm successful configuration and view available models
  4. Model Selection: Understand features and use cases of different Claude models

Key Commands:

CommandPurpose
opencode auth loginConfigure provider authentication
opencode auth listView configured providers
opencode modelsView available models

Claude Model Features:

  • ✅ Long context window (200K tokens)
  • ✅ Strong code generation capability
  • ✅ Excellent reasoning ability (Claude 3.7 Sonnet)

Next Lesson Preview

Next lesson we'll learn about Claude Code Relay.

You'll learn:

  • What is Claude Code relay service
  • How to configure relay endpoint
  • Differences between relay and official API

Appendix: Source Code Reference

Click to expand source code locations

Last updated: 2026-02-14

FeatureFile PathLine Number
Anthropic provider definitionsrc/provider/provider.ts93-103
Environment variable detectionsrc/provider/provider.ts841-850
Model data loadingsrc/provider/models.ts87-104
Authentication managementsrc/auth/index.ts37-70

Key Configuration:

  • Environment variable name: ANTHROPIC_API_KEY
  • Auth type: API Key only (OAuth not supported)
  • Auto-load: false (requires manual API Key configuration)