Skip to content

Instantly share code, notes, and snippets.

@Turtlecute33
Last active February 14, 2026 01:00
Show Gist options
  • Select an option

  • Save Turtlecute33/dd13b98def9aa87c5821e1e7db15aa2e to your computer and use it in GitHub Desktop.

Select an option

Save Turtlecute33/dd13b98def9aa87c5821e1e7db15aa2e to your computer and use it in GitHub Desktop.
Claude Codel Model swap Zshrc

Claude Code Model Switcher

Switch between Claude (default), OpenRouter models, and free tiers with a single command. Never hit rate limits again.

Why?

  • Avoid hard limits: When you hit Claude's rate limits, switch to OpenRouter and keep working
  • Right model for the task: Use free/cheap models for simple tasks, save Claude for complex ones
  • $0 coding sessions: Free tiers on OpenRouter mean unlimited simple tasks

Setup

1. Get an OpenRouter API Key

  1. Go to openrouter.ai
  2. Create an account
  3. Go to Keys → Create Key
  4. Copy your key

2. Add to your .zshrc

Open your zshrc:

nano ~/.zshrc

Paste this at the end (replace YOUR_OPENROUTER_KEY with your actual key):

# Claude Code Model Switcher
# Usage: type the function name to switch models

# Set your OpenRouter API key here (only once)
OPENROUTER_API_KEY="YOUR_OPENROUTER_KEY"

# Helper function to fetch and display OpenRouter pricing
_openrouter_pricing() {
    local model_id="$1"
    local pricing=$(curl -s "https://openrouter.ai/api/v1/models" | \
        jq -r --arg id "$model_id" '.data[] | select(.id == $id) | "Input: $\(.pricing.prompt | tonumber * 1000000 | . * 100 | round / 100)/M | Output: $\(.pricing.completion | tonumber * 1000000 | . * 100 | round / 100)/M"' 2>/dev/null)
    if [[ -n "$pricing" ]]; then
        echo "$pricing"
    fi
}

# Default Claude Code (your subscription)
cc() {
    unset ANTHROPIC_BASE_URL ANTHROPIC_AUTH_TOKEN
    unset ANTHROPIC_DEFAULT_OPUS_MODEL ANTHROPIC_DEFAULT_SONNET_MODEL ANTHROPIC_DEFAULT_HAIKU_MODEL
    unset CLAUDE_CODE_USE_BEDROCK CLAUDE_MODEL
    echo "Switched to Claude Code (default)"
}

# GLM 4.7 - Free tier available
glm() {
    local model="z-ai/glm-4.7"
    export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
    export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
    export ANTHROPIC_DEFAULT_OPUS_MODEL="$model"
    export ANTHROPIC_DEFAULT_SONNET_MODEL="$model"
    export ANTHROPIC_DEFAULT_HAIKU_MODEL="$model"
    echo "Switched to OpenRouter (GLM 4.7)"
    _openrouter_pricing "$model"
}

# Kimi K2.5 - Strong reasoning model
kimi() {
    local model="moonshotai/kimi-k2.5"
    export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
    export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
    export ANTHROPIC_DEFAULT_OPUS_MODEL="$model"
    export ANTHROPIC_DEFAULT_SONNET_MODEL="$model"
    export ANTHROPIC_DEFAULT_HAIKU_MODEL="$model"
    echo "Switched to OpenRouter (Kimi K2.5)"
    _openrouter_pricing "$model"
}

# Grok Code Fast - Optimized for coding
grok() {
    local model="x-ai/grok-code-fast-1"
    export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
    export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
    export ANTHROPIC_DEFAULT_OPUS_MODEL="$model"
    export ANTHROPIC_DEFAULT_SONNET_MODEL="$model"
    export ANTHROPIC_DEFAULT_HAIKU_MODEL="$model"
    echo "Switched to OpenRouter (Grok Code Fast)"
    _openrouter_pricing "$model"
}

# Gemini 3 Flash - Fast and capable
gemini() {
    local model="google/gemini-3-flash-preview"
    export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
    export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
    export ANTHROPIC_DEFAULT_OPUS_MODEL="$model"
    export ANTHROPIC_DEFAULT_SONNET_MODEL="$model"
    export ANTHROPIC_DEFAULT_HAIKU_MODEL="$model"
    echo "Switched to OpenRouter (Gemini 3 Flash)"
    _openrouter_pricing "$model"
}

# Custom model - enter any OpenRouter model ID
custom() {
    local model
    echo -n "Enter OpenRouter model (e.g. writer/palmyra-x5): "
    read model
    if [[ -z "$model" ]]; then
        echo "No model provided, aborting."
        return 1
    fi
    export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
    export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
    export ANTHROPIC_DEFAULT_OPUS_MODEL="$model"
    export ANTHROPIC_DEFAULT_SONNET_MODEL="$model"
    export ANTHROPIC_DEFAULT_HAIKU_MODEL="$model"
    echo "Switched to OpenRouter ($model)"
    _openrouter_pricing "$model"
}

3. Reload your shell

source ~/.zshrc

Usage

Command Model Notes
cc Claude (default) Your normal subscription
glm GLM 4.7 Free tier available
kimi Kimi K2.5 Strong reasoning
grok Grok Code Fast Optimized for code
gemini Gemini 3 Flash Fast responses
custom Any model Enter model ID manually

Each command shows you the current pricing per million tokens.

Example Workflow

# Start with Claude for architecture decisions
cc
claude

# Hit rate limit? Switch to free tier for simpler tasks
glm
claude

# Back to Claude when limits reset
cc
claude

Requirements

  • jq for pricing display (optional but nice): brew install jq or apt install jq
  • OpenRouter account (free to create)

Finding More Models

Browse available models at openrouter.ai/models. Use the custom command to try any of them.

Tips

  • Free tiers: Many models on OpenRouter have free tiers with rate limits. Great for learning or simple tasks.
  • Paid tiers: Add credits to OpenRouter for higher limits and faster responses.
  • Local models: You can adapt this pattern for local models too (Ollama, LM Studio, etc.)

Made by @turtlecute33

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment