Claude Code: API Configuration and Model Selection

Last updated: 2026-08-31

Claude Code uses the Anthropic API by default, but also supports connecting third-party models like DeepSeek via compatible interfaces. Proper API configuration is key to controlling cost and quality.

💡 Tip: Claude Code natively supports the Anthropic API via the ANTHROPIC_API_KEY environment variable. Third-party models require OpenAI-compatible interfaces.

📋 Prerequisites: Chapter 2 - Installation and Usage

1. What You'll Learn


2. Anthropic API Configuration

(1) Getting an API Key

BASH
# Steps:
# 1. Visit https://console.anthropic.com/
# 2. Register/login to your account
# 3. Go to the API Keys page
# 4. Click "Create Key"
# 5. Copy the Key (format: sk-ant-api03-xxxxx)

(2) Configure Environment Variable

BASH
# Temporary (current terminal only)
export ANTHROPIC_API_KEY="sk-ant-api03-xxxxx"

# Permanent (add to shell config)
echo 'export ANTHROPIC_API_KEY="sk-ant-api03-xxxxx"' >> ~/.zshrc
source ~/.zshrc

# Verify
echo $ANTHROPIC_API_KEY
# sk-ant-api03-xxxxx

(3) Model Selection

Model Context Input Price Output Price Use Case
Claude Sonnet 200K $3/MTok $15/MTok Daily coding, best value
Claude Opus 200K $15/MTok $75/MTok Complex architecture, deep reasoning
Claude Haiku 200K $0.25/MTok $1.25/MTok Quick queries, low cost
BASH
# Specify model
claude --model claude-sonnet-4-20250514
claude --model claude-opus-4-20250514

# Or via environment variable
export CLAUDE_MODEL="claude-sonnet-4-20250514"

3. DeepSeek Model Integration

(1) Configure DeepSeek API

DeepSeek provides OpenAI-compatible interfaces usable by Claude Code:

BASH
# Set DeepSeek API
export ANTHROPIC_API_KEY="your-deepseek-api-key"
export ANTHROPIC_BASE_URL="https://api.deepseek.com"

# Or use OpenAI compatible endpoint
export OPENAI_API_KEY="your-deepseek-api-key"
export OPENAI_BASE_URL="https://api.deepseek.com/v1"

(2) DeepSeek Model Comparison

Model Context Price (Input/Output) Use Case
DeepSeek V3 128K ¥2/MTok / ¥8/MTok General coding, extreme value
DeepSeek R1 128K ¥4/MTok / ¥16/MTok Deep reasoning, math/logic

▶ Example 1: DeepSeek Configuration Complete Flow

BASH
# Alice uses DeepSeek to reduce costs

# 1. Get DeepSeek API Key
# Visit https://platform.deepseek.com/

# 2. Configure environment variables
export ANTHROPIC_API_KEY="sk-deepseek-xxxxx"
export ANTHROPIC_BASE_URL="https://api.deepseek.com"
export CLAUDE_MODEL="deepseek-chat"

# 3. Run Claude Code
claude "Help me refactor this function"

4. Cost Estimation and Control

(1) Token Consumption Patterns

Claude Code's Token consumption mainly comes from:

Source Share Description
Project context 40-60% Reading project file contents
Conversation history 20-30% Previous interaction records
Code output 10-20% Generated code
System prompts 5-10% CLAUDE.md and other instructions

(2) Cost Estimation

▶ Example 2: Monthly Cost Estimation Script

PYTHON
def estimate_monthly_cost(
    daily_sessions: int,
    avg_context_tokens: int,
    avg_output_tokens: int,
    model_input_price: float,
    model_output_price: float,
    working_days: int = 22
):
    daily_input = daily_sessions * avg_context_tokens
    daily_output = daily_sessions * avg_output_tokens
    monthly_input = daily_input * working_days
    monthly_output = daily_output * working_days

    cost = (
        monthly_input * model_input_price / 1_000_000
        + monthly_output * model_output_price / 1_000_000
    )
    return round(cost, 2)

# Sonnet plan
sonnet_cost = estimate_monthly_cost(
    daily_sessions=10,
    avg_context_tokens=50000,
    avg_output_tokens=5000,
    model_input_price=3,
    model_output_price=15
)
print(f"Sonnet monthly: ${sonnet_cost}")

# DeepSeek plan
deepseek_cost = estimate_monthly_cost(
    daily_sessions=10,
    avg_context_tokens=50000,
    avg_output_tokens=5000,
    model_input_price=0.27,
    model_output_price=1.1
)
print(f"DeepSeek monthly: ${deepseek_cost}")

Output:

TEXT 📖 Display only
Sonnet monthly: $46.2
DeepSeek monthly: $3.74

5. Multi-API Key Management

▶ Example 3: Switch API Config Per Project

BASH
# Configure per project in .env files
# project-a/.env
ANTHROPIC_API_KEY=sk-ant-project-a-key
CLAUDE_MODEL=claude-sonnet-4-20250514

# project-b/.env
ANTHROPIC_API_KEY=sk-deepseek-key
ANTHROPIC_BASE_URL=https://api.deepseek.com
CLAUDE_MODEL=deepseek-chat

# Use direnv for auto-switching
# .envrc
source .env
export ANTHROPIC_API_KEY
export ANTHROPIC_BASE_URL
export CLAUDE_MODEL

(2) API Quota Monitoring

BASH
# Check Anthropic API usage
curl https://api.anthropic.com/v1/usage \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01"

# Claude Code built-in stats
claude /cost
# Current session token usage: 125,430
# Estimated cost: $2.34

6. Comprehensive Example: Cost Optimization Configuration

BASH
# Alice's cost optimization plan

# Core projects: Use Sonnet (quality first)
export PROJECT_CORE_API_KEY="sk-ant-api03-xxxxx"
export PROJECT_CORE_MODEL="claude-sonnet-4-20250514"

# Auxiliary projects: Use DeepSeek (cost first)
export PROJECT_AUX_API_KEY="sk-deepseek-xxxxx"
export PROJECT_AUX_BASE_URL="https://api.deepseek.com"
export PROJECT_AUX_MODEL="deepseek-chat"

# Configure in shell aliases
alias claude-core='ANTHROPIC_API_KEY=$PROJECT_CORE_API_KEY CLAUDE_MODEL=$PROJECT_CORE_MODEL claude'
alias claude-aux='ANTHROPIC_API_KEY=$PROJECT_AUX_API_KEY ANTHROPIC_BASE_URL=$PROJECT_AUX_BASE_URL CLAUDE_MODEL=$PROJECT_AUX_MODEL claude'

# Usage
cd core-project && claude-core "Refactor authentication module"
cd side-project && claude-aux "Add logging feature"

❓ FAQ

Q How does DeepSeek's quality compare to Claude?
A DeepSeek V3 reaches about 80-90% of Claude Sonnet's level on coding tasks, but still falls short on complex reasoning and long context understanding. Sufficient for daily coding; critical tasks should use Claude.
Q What if my API Key is leaked?
A Immediately revoke the Key in the console and generate a new one. Check git history for accidentally committed Keys.
Q How long do free credits last?
A Anthropic gives new accounts $5 in credits, supporting about 50-100 medium-complexity coding sessions at Sonnet pricing.
Q Can I configure multiple APIs simultaneously?
A Yes. Switch between projects via environment variables, or use direnv for auto-switching.
Q Is DeepSeek's context length enough?
A DeepSeek V3 supports 128K context, sufficient for most projects. Very large projects (100+ files) may need Claude's 200K.
Q How do I know current Token usage?
A Type /cost in Claude Code to see current session token usage and estimated cost.

📖 Summary


📝 Exercises

  1. Basic (⭐): Configure Anthropic API Key, run Claude Code and confirm connection success.
  2. Intermediate (⭐⭐): Estimate your team's monthly Claude Code API cost (using the script in the example).
  3. Advanced (⭐⭐⭐): Set up DeepSeek integration, compare the same task's quality and cost between Claude Sonnet and DeepSeek V3.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏