DEV Community

Hamza Khan
Hamza Khan

Posted on

Complete Guide to Setting Up Claude Code Router with Qwen on macOS

A complete guide to setting up Claude Code Router (CCR) as a middleware layer to use Qwen's free AI models with Claude Code on macOS.

Why Use This Setup

Claude Code Router redirects Claude Code requests to alternative AI providers like Qwen, giving you:

  • Claude Code's agentic coding capabilities
  • Free, open-source models with zero API costs
  • Full control over your AI provider

Prerequisites

Claude Code requires Node.js 18 or higher. Check your version:

node --version
Enter fullscreen mode Exit fullscreen mode

If you need to install or update Node.js, download it from nodejs.org.

Step 1: Install Required Tools

Install Qwen Code, Claude Code, and Claude Code Router globally:

npm install -g @qwen-code/qwen-code@latest
npm install -g @anthropic-ai/claude-code @musistudio/claude-code-router
Enter fullscreen mode Exit fullscreen mode

Step 2: Authenticate with Qwen OAuth

Qwen Code uses OAuth authentication which is the recommended method for using Qwen models. This provides free access with a quota of 60 requests/minute and 2,000 requests/day.

Start the authentication flow

qwen
Enter fullscreen mode Exit fullscreen mode

This command will automatically open your browser to complete the OAuth login.

Complete browser login

  • Log in with your qwen.ai account (create one if needed)
  • Click "Confirm" to authorize access
  • The CLI will confirm successful authentication

Credentials stored locally

After authorization, your OAuth credentials are automatically saved to /Users/YOUR_USERNAME/.qwen/oauth_creds.json and you won't need to log in again. The credentials will be automatically refreshed when needed.

Getting your access token for Claude Code Router

After successful authentication, you need to extract the access token from the OAuth credentials file:

cat /Users/YOUR_USERNAME/.qwen/oauth_creds.json
Enter fullscreen mode Exit fullscreen mode

Replace YOUR_USERNAME with your actual macOS username.

This will display a JSON file similar to this:

{
  "access_token": "your-long-access-token-here",
  "refresh_token": "your-refresh-token-here",
  "token_type": "Bearer",
  "resource_url": "portal.qwen.ai",
  "expiry_date": 1767490948168
}
Enter fullscreen mode Exit fullscreen mode

Copy the value of access_token (the long string after "access_token":). You'll need this token in the next step when configuring Claude Code Router.

Note: The access token is automatically managed by Qwen Code and refreshes as needed. However, for Claude Code Router integration, you'll need to manually update the token in the router configuration if it expires.

Step 3: Configure the Router

Create the configuration directory

mkdir -p ~/.claude-code-router
Enter fullscreen mode Exit fullscreen mode

Create the configuration file

cat > ~/.claude-code-router/config.json << 'EOF'
{
  "LOG": true,
  "LOG_LEVEL": "info",
  "HOST": "127.0.0.1",
  "PORT": 3456,
  "API_TIMEOUT_MS": 600000,
  "Providers": [
    {
      "name": "qwen",
      "api_base_url": "https://portal.qwen.ai/v1/chat/completions",
      "api_key": "YOUR_ACCESS_TOKEN_HERE",
      "models": [
        "qwen3-coder-plus",
        "qwen3-coder-plus",
        "qwen3-coder-plus"
      ]
    }
  ],
  "Router": {
    "default": "qwen,qwen3-coder-plus",
    "background": "qwen,qwen3-coder-plus",
    "think": "qwen,qwen3-coder-plus",
    "longContext": "qwen,qwen3-coder-plus",
    "longContextThreshold": 60000,
    "webSearch": "qwen,qwen3-coder-plus"
  }
}
EOF
Enter fullscreen mode Exit fullscreen mode

Important: Replace YOUR_ACCESS_TOKEN_HERE with the access token you copied from /Users/YOUR_USERNAME/.qwen/oauth_creds.json in Step 2.

Configuration Breakdown

  • LOG settings — Enable console output for debugging
  • HOST/PORT — Local address where CCR listens (127.0.0.1:3456)
  • API_TIMEOUT_MS — Maximum wait time for API responses (10 minutes)
  • Providers — Defines Qwen API connection and available models
  • Router — Specifies which model handles each task type

Step 4: Start the Router

Launch Claude Code Router:

ccr restart
Enter fullscreen mode Exit fullscreen mode

You should see console logs confirming successful startup.

Step 5: Launch Claude Code

Open the interactive CLI:

ccr code
Enter fullscreen mode Exit fullscreen mode

Test with a simple message:

> Hello Claude
Enter fullscreen mode Exit fullscreen mode

You should receive a response from Qwen3-Coder-Plus.

Troubleshooting

  • 404 errors — Verify your api_base_url and access token are correct.
  • Connection timeouts — Check your network connection and Qwen's service status.
  • Router won't start — Look for port conflicts or JSON syntax errors in your config file.
  • Model not responding — Ensure the model name in your config matches exactly with Qwen's available models.

Advanced Configuration

Using Different Models

Update the models array in the config to try different Qwen models:

"models": [
  "qwen3-coder-plus",
  "qwen2.5-coder-32b-instruct",
  "qwq-32b-preview"
]
Enter fullscreen mode Exit fullscreen mode

Adding Multiple Providers

You can configure multiple AI providers in the same config file:

"Providers": [
  {
    "name": "qwen",
    "api_base_url": "https://portal.qwen.ai/v1/chat/completions",
    "api_key": "YOUR_QWEN_TOKEN"
  },
  {
    "name": "another-provider",
    "api_base_url": "https://api.example.com/v1/chat/completions",
    "api_key": "YOUR_OTHER_KEY"
  }
]
Enter fullscreen mode Exit fullscreen mode

Adjusting Timeout Settings

For longer-running tasks, increase the timeout:

"API_TIMEOUT_MS": 900000  // 15 minutes
Enter fullscreen mode Exit fullscreen mode

What's Next

With this setup, you can:

  • Use Claude Code's full capabilities powered by Qwen models
  • Update the configuration file to try different models
  • Add multiple providers for different use cases
  • Work seamlessly from your terminal

The router handles everything behind the scenes while you maintain Claude Code's familiar interface and workflow.

Resources

Top comments (0)