Skip to content

feat: add Claude Code Router (CCR) support#1

Open
airingursb wants to merge 2 commits intodevfrom
feat/ccr-support
Open

feat: add Claude Code Router (CCR) support#1
airingursb wants to merge 2 commits intodevfrom
feat/ccr-support

Conversation

@airingursb
Copy link
Owner

@airingursb airingursb commented Jan 9, 2026

Summary

This PR adds comprehensive support for Claude Code Router (CCR) in opencode, allowing users to route Anthropic API requests through alternative providers like OpenRouter, DeepSeek, or local models.

🎉 Key Feature: Automatic CCR Activation

Users no longer need to manually run ccr activate before using opencode. The tool now automatically detects and activates CCR if available!

Changes

1. Automatic CCR Detection & Activation (NEW!)

  • Created new CCR module (packages/opencode/src/ccr/index.ts)
  • Automatically checks if ccr command is available on startup
  • Runs ccr activate and parses environment variables
  • Transparently applies ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN
  • Integrated into opencode startup flow via middleware

2. Environment Variable Support

  • Added support for ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN environment variables
  • When these variables are set (manually or via CCR), opencode automatically uses them
  • No separate Anthropic authentication required

3. Authentication Flow Updates

  • opencode auth login: Detects CCR configuration and informs users that no additional authentication is needed
  • opencode auth list: Displays CCR configuration when environment variables are set (with partial token masking)

4. Provider Configuration

  • Modified anthropic CUSTOM_LOADER in provider.ts to auto-detect and apply CCR settings
  • When CCR is configured, the provider is automatically loaded with the appropriate baseURL and apiKey

Usage

Option 1: Automatic (Recommended) ✨

Just install ccr and run opencode - it will automatically detect and activate ccr:

# Install CCR (one time)
npm install -g @musistudio/claude-code-router

# Configure CCR (one time)
ccr config set OPENROUTER_API_KEY sk-or-v1-your-key

# Use opencode - CCR is automatically activated!
opencode

Option 2: Manual Environment Variables

Set environment variables manually:

export ANTHROPIC_BASE_URL="http://localhost:8787"
export ANTHROPIC_AUTH_TOKEN="sk-or-v1-your-openrouter-key"
opencode

Option 3: Shell Integration (for other tools)

For compatibility with other tools that don't auto-activate:

# Add to your ~/.bashrc or ~/.zshrc
if command -v ccr >/dev/null 2>&1; then 
  eval "$(ccr activate)"
fi

Technical Details

Files Modified

  • packages/opencode/src/ccr/index.ts (new): CCR auto-detection and activation module
  • packages/opencode/src/index.ts: Integrated CCR.autoActivate() into startup middleware
  • packages/opencode/src/provider/provider.ts: Added CCR environment variable support for anthropic provider
  • packages/opencode/src/cli/cmd/auth.ts: Enhanced auth commands to display and handle CCR configuration

Activation Logic

  1. On opencode startup, check if ANTHROPIC_BASE_URL or ANTHROPIC_AUTH_TOKEN are already set
  2. If not, check if ccr command exists in PATH
  3. If found, run ccr activate and parse the output
  4. Apply parsed environment variables to current process
  5. Provider loader picks up these variables automatically

Testing

  • ✅ Code compiles without errors
  • ✅ Git commits successfully created
  • ✅ Branch pushed to remote
  • ✅ Automatic activation logic implemented
  • ✅ Backward compatible with manual configuration

Benefits

  • 🚀 Zero-config experience: Just install ccr and run opencode
  • 🔄 Automatic activation: No need to modify shell config or run manual commands
  • 🔌 Seamless integration: Works transparently when ccr is installed
  • ⚙️ Flexible: Supports manual configuration if preferred
  • 🔙 Backward compatible: Existing Anthropic authentication still works
  • 🎯 Smart: Only activates when needed (skips if already configured)
  • 📊 Visible: Auth commands show CCR status clearly

factory-droid bot and others added 2 commits January 9, 2026 17:18
- Add support for ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN environment variables
- Skip authentication when CCR is configured for anthropic provider
- Display CCR configuration in 'opencode auth list' command
- Auto-detect and inform users when CCR is available during 'opencode auth login'

This allows users to use Claude Code Router (CCR) to route Anthropic requests
to alternative providers like OpenRouter, DeepSeek, or local models without
needing separate Anthropic authentication.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
- Created new CCR module to handle automatic detection and activation
- CCR.autoActivate() checks if 'ccr' command is available
- If available, runs 'ccr activate' and parses environment variables
- Automatically sets ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN
- Integrated into opencode startup flow via middleware
- Users now only need to run 'opencode' - no manual ccr activation needed

This eliminates the need for users to manually run 'eval "$(ccr activate)"'
before running opencode. The integration is transparent and only activates
when ccr is installed and not already configured.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant