Skip to content

istarwyh/ai-speeds

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Claude-Code-Router

Universal API proxy for using Claude Code with multiple AI providers. Seamlessly translates between Anthropic's Claude API and OpenAI-compatible APIs.

โœจ Features

  • ๐Ÿ”„ API Translation: Anthropic โ†” OpenAI format conversion
  • ๐ŸŒ Multi-Provider: OpenRouter, OpenAI, DeepSeek, Kimi, SiliconFlow
  • โšก Edge Computing: Cloudflare Workers for global performance
  • ๐Ÿ“ก Streaming Support: Real-time response streaming
  • ๐Ÿ›ก๏ธ Type Safety: Full TypeScript implementation

๐Ÿš€ Quick Start

1. Install Claude Code

npm install -g @anthropic-ai/claude-code

2. Configure API Access

# Option A: Use shared instance (testing only)
export ANTHROPIC_BASE_URL="https://cc.xiaohui.cool"
export ANTHROPIC_API_KEY="your-provider-api-key"

# Option B: Deploy your own instance (recommended)
git clone https://github.com/your-username/claude-code-router
cd claude-code-router && wrangler deploy
export ANTHROPIC_BASE_URL="https://your-domain.workers.dev"

3. Start Using Claude Code

source ~/.bashrc && claude

Provider Setup

Provider API Key Source Base URL
OpenRouter openrouter.ai https://cc.xiaohui.cool
DeepSeek platform.deepseek.com Deploy with DEEPSEEK_BASE_URL
OpenAI platform.openai.com Deploy with OPENAI_BASE_URL

๐Ÿ—๏ธ Architecture

graph TB
    A[Claude Code Client] -->|Anthropic API Format| B[Claude-Code-Router]
    B -->|Provider Selection| C{Environment Config}
    C -->|OpenRouter| D[OpenRouter API]
    C -->|DeepSeek| E[DeepSeek API]
    C -->|OpenAI| F[OpenAI API]
    C -->|Others| G[Other OpenAI-Compatible APIs]
    
    B -->|Format Conversion| H[Request Translator]
    B -->|Response Processing| I[Response Translator]
    B -->|Streaming| J[SSE Handler]
    
    subgraph "Cloudflare Workers"
        B
        H
        I
        J
    end
    
    style A fill:#e1f5fe
    style B fill:#fff3e0
    style D fill:#f3e5f5
    style E fill:#e8f5e8
    style F fill:#fff8e1
    style G fill:#fce4ec
Loading

โšก Technical Stack

Core Runtime

  • Platform: Cloudflare Workers (V8 Isolates)
  • Language: TypeScript with strict typing
  • Runtime: Edge-optimized JavaScript (no Node.js dependencies)
  • Deployment: Wrangler CLI + GitHub Actions

Architecture Patterns

Pattern Implementation Benefit
Edge Computing 300+ global locations <1ms cold start, 0ms warm start
Serverless Auto-scaling isolates 0 maintenance, infinite scale
Type Safety Full TypeScript coverage Runtime error prevention
Streaming Web Streams API Real-time response delivery
Modular Design Functional composition Easy testing & maintenance

Build System

  • Bundler: esbuild (10x faster than webpack)
  • Module System: ES6 with tree-shaking
  • Asset Pipeline: TypeScript โ†’ JavaScript + type checking
  • Hot Reload: Wrangler dev server with instant updates

๐Ÿ”ง Deployment

Development

git clone https://github.com/your-username/claude-code-router
cd claude-code-router
npm install && npm install -g wrangler
npm run build:client    # Build frontend modules
npm run dev             # Start development server

Production Deployment

# Configure environment variables
wrangler secret put OPENROUTER_BASE_URL       # OpenRouter backend
wrangler secret put DEEPSEEK_BASE_URL         # DeepSeek backend  
wrangler secret put OPENAI_BASE_URL           # OpenAI backend

# Deploy to Cloudflare Workers
npm run deploy

Environment Configuration

flowchart LR
    A[Environment Variables] --> B{Provider Selection}
    B -->|Priority 1| C[DEEPSEEK_BASE_URL]
    B -->|Priority 2| D[OPENAI_BASE_URL]
    B -->|Priority 3| E[KIMI_BASE_URL]
    B -->|Priority 4| F[SILICONFLOW_BASE_URL]
    B -->|Default| G[OPENROUTER_BASE_URL]
    
    C --> H[DeepSeek API]
    D --> I[OpenAI API]
    E --> J[Kimi API]
    F --> K[SiliconFlow API]
    G --> L[OpenRouter API]
Loading

๐Ÿ”Œ API Reference

Request Format (Anthropic)

curl -X POST https://cc.xiaohui.cool/v1/messages \
  -H "Content-Type: application/json" \
  -H "x-api-key: your-api-key" \
  -d '{
    "model": "claude-3-5-sonnet-20241022",
    "messages": [{"role": "user", "content": "Hello"}],
    "max_tokens": 100,
    "stream": true
  }'

Model Mapping

Claude Model OpenRouter DeepSeek OpenAI
claude-3-5-haiku-20241022 anthropic/claude-3.5-haiku deepseek-chat gpt-4o-mini
claude-3-5-sonnet-20241022 anthropic/claude-3.5-sonnet deepseek-chat gpt-4o
claude-3-opus-20240229 anthropic/claude-3-opus deepseek-reasoner gpt-4o

๐Ÿ“ Architecture & Structure

Worker Runtime Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                        Cloudflare Workers                        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ ๐Ÿ”„ Request Router    โ”‚ ๐Ÿ”„ Format Converter โ”‚ ๐Ÿ“ก Stream Handler  โ”‚
โ”‚ โ€ข Path matching       โ”‚ โ€ข Anthropic โ†’ OpenAI  โ”‚ โ€ข SSE processing    โ”‚
โ”‚ โ€ข Method validation   โ”‚ โ€ข OpenAI โ†’ Anthropic  โ”‚ โ€ข Chunk buffering   โ”‚
โ”‚ โ€ข Auth handling       โ”‚ โ€ข Model mapping      โ”‚ โ€ข Error recovery    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

File Structure & Frontend Architecture

claude-code-router/
โ”œโ”€โ”€ ๐Ÿ“ src/
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ api/                 # API ้€‚้…ๅ™จๅ’Œ็ฑปๅž‹ๅฎšไน‰
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ adapters/         # ่ฏทๆฑ‚ๅ’Œๅ“ๅบ”ๆ ผๅผ่ฝฌๆข
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ format.ts      # ่ฏทๆฑ‚/ๅ“ๅบ”ๆ ผๅผๅŒ–
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“ stream.ts      # ๆตๅค„็†
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ types.ts          # API ็ฑปๅž‹ๅฎšไน‰
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“ providers.ts      # ไพ›ๅบ”ๅ•†้…็ฝฎ
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ client/              # ๅ‰็ซฏๅ’Œๆ–‡ๆกฃ็ณป็ปŸ (TypeScript ๆบ็ )
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ bestPractices/    # ๆœ€ไฝณๅฎž่ทตๆจกๅ— (ๅผ€ๅ‘ๆบ็ )
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ core/          # ็ฎก็†ๅ™จๅ’ŒไธšๅŠก้€ป่พ‘
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ data/          # ๅก็‰‡ๆ•ฐๆฎๅ’Œ้…็ฝฎ
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ renderers/     # UI ๆธฒๆŸ“็ป„ไปถ
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ services/      # ๅ†…ๅฎนๅ’Œ Markdown ๆœๅŠก
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“ index.ts       # ๆจกๅ—ๅ…ฅๅฃ็‚น
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ howToApplyCC/     # ๅฆ‚ไฝ•ไฝฟ็”จ CC ๆจกๅ—
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ components/    # UI ็ป„ไปถ
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ services/      # ๆœๅŠกๅฑ‚
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“ index.ts       # ๆจกๅ—ๅ…ฅๅฃ็‚น
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“ howToImplement/   # ๅฎž็ŽฐๆŒ‡ๅ—ๆจกๅ— (ๅผ€ๅ‘ๆบ็ )
โ”‚   โ”‚       โ”œโ”€โ”€ ๐Ÿ“ core/          # HowToImplementManager
โ”‚   โ”‚       โ”œโ”€โ”€ ๐Ÿ“ data/          # ๅก็‰‡ๆ•ฐๆฎๅ’Œ้…็ฝฎ
โ”‚   โ”‚       โ”œโ”€โ”€ ๐Ÿ“ handlers/      # ไบ‹ไปถๅค„็†
โ”‚   โ”‚       โ”œโ”€โ”€ ๐Ÿ“ renderers/     # UI ๆธฒๆŸ“็ป„ไปถ
โ”‚   โ”‚       โ”œโ”€โ”€ ๐Ÿ“ services/      # ๅ†…ๅฎนๆœๅŠก
โ”‚   โ”‚       โ””โ”€โ”€ ๐Ÿ“ index.ts       # ๆจกๅ—ๅ…ฅๅฃ็‚น
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ components/           # ๅ…ฑไบซ UI ็ป„ไปถ
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ config/               # ๅ…จๅฑ€้…็ฝฎ
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ server/               # ๆœๅŠกๅ™จ่ฟ่กŒๆ—ถ้€ป่พ‘
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ env.ts             # ็Žฏๅขƒๅ˜้‡็ฑปๅž‹ๅฎšไน‰
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ index.ts           # ๆœๅŠกๅ™จๅ…ฅๅฃ็‚น
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“ routes/            # ่ทฏ็”ฑๅค„็†ๅ™จ
โ”‚   โ”‚       โ””โ”€โ”€ ๐Ÿ“ imgProxy.ts     # ๅ›พ็‰‡ไปฃ็†
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ scripts/              # ๅฎขๆˆท็ซฏ่„šๆœฌ
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“ generated/         # ไปŽ src/client/* ่‡ชๅŠจ็”Ÿๆˆ
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ styles/               # ๅ…จๅฑ€ๆ ทๅผ
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ templates/            # HTML ๆจกๆฟ
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ components/        # ๆจกๆฟ็ป„ไปถ
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“ favicon.ts      # ๅ›พๆ ‡็”Ÿๆˆ
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ index.ts           # ไธป้กตๆจกๆฟ
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ terms.ts           # ๆœๅŠกๆกๆฌพ้กต้ข
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“ privacy.ts         # ้š็งๆ”ฟ็ญ–้กต้ข
โ”‚   โ””โ”€โ”€ ๐Ÿ“ utils/                # ๅทฅๅ…ทๅ‡ฝๆ•ฐ
โ”œโ”€โ”€ ๐Ÿ“ modules/                 # ้™ๆ€ HTML ๆจกๆฟ + ็ผ–่ฏ‘ๅŽ็š„ JavaScript
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ best-practices/      # HTML ๆจกๆฟ + ๆ‰“ๅŒ…็š„ๅฎขๆˆท็ซฏไปฃ็ 
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ get-started/         # ้™ๆ€ๆจกๅ—็ป„ไปถ
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ how-to-apply-cc/     # ๅฆ‚ไฝ•ไฝฟ็”จ CC ๆจกๅ—
โ”‚   โ””โ”€โ”€ ๐Ÿ“ how-to-implement/    # HTML ๆจกๆฟ + ๆ‰“ๅŒ…็š„ๅฎขๆˆท็ซฏไปฃ็ 
โ”œโ”€โ”€ ๐Ÿ“ scripts/                 # ๆž„ๅปบ่‡ชๅŠจๅŒ–ๅ’Œๆ‰“ๅŒ…
โ”œโ”€โ”€ ๐Ÿ”ง index.ts                 # Worker ๅ…ฅๅฃ็‚น (fetch ๅค„็†ๅ™จ)
โ””โ”€โ”€ โš™๏ธ wrangler.toml            # Worker ้…็ฝฎๅ’Œ็ป‘ๅฎš

Frontend Build Architecture

The project uses a dual-layer frontend architecture:

Development Layer (src/client/)

  • Purpose: Modern TypeScript development with full module structure
  • Architecture: Modular design (core, data, handlers, renderers, services)
  • Benefits: Type safety, code organization, maintainability
  • Build Target: Gets compiled and bundled by scripts/build-client.js

Runtime Layer (modules/)

  • Purpose: Production-ready HTML templates + compiled JavaScript
  • Architecture: Static HTML containers + bundled client code
  • Benefits: Single-file deployment, optimized for Cloudflare Workers
  • Source: Generated from development layer through build process

Build Process Flow

graph LR
    A[src/client/*/index.ts] -->|esbuild| B[Bundled JavaScript]
    B -->|build-client.js| C[modules/*/index.ts]
    C -->|Runtime| D[HTML + JS Module]
    E[npm run build:client] -->|Orchestrates| A

    style A fill:#e3f2fd
    style B fill:#fff3e0
    style C fill:#e8f5e8
    style D fill:#fce4ec
Loading

Active Modules:

  • Best Practices (src/client/bestPractices โ†’ modules/best-practices)
  • How to Implement (src/client/howToImplement โ†’ modules/how-to-implement)
  • How to Apply Claude Code (src/client/howToApplyCC โ†’ modules/how-to-apply-cc)
  • Get Started (Static components in modules/get-started)

This approach ensures clean separation between development complexity and runtime efficiency.

Key Design Principles

  • ๐ŸŒ Edge-First: Optimized for Cloudflare's global network
  • ๐Ÿ”’ Zero Dependencies: No external runtime dependencies
  • โšก Performance: Sub-millisecond response times
  • ๐Ÿ”„ Streaming: Native Web Streams API support
  • ๐Ÿ›ก๏ธ Type Safety: Full TypeScript coverage with strict mode

๐Ÿ™ Acknowledgments

Built with inspiration from:

โš–๏ธ License & Disclaimer

MIT License - Use at your own risk and discretion.

โš ๏ธ Important: This is an independent tool, not affiliated with Anthropic, OpenAI, or OpenRouter. Users are responsible for compliance with all relevant Terms of Service and API usage policies.

About

A Simple Proxy enabling Claude Code to work with OpenRouter or Deepseek

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 97.9%
  • JavaScript 2.1%