Apurva Chaudhary

Effortlessly switch between Claude code and z.ai in terminal

Tired of manually switching Claude API endpoints every time you open a new terminal?
Here’s how to set up one-command switching between Z.AI and Anthropic — permanently.


I. Introduction: Why You Need This

If you’re using Claude in VS Code, you’ve probably run into this:
You want to toggle between Z.AI (for custom features, cost savings, or local routing) and the default Anthropic API — but every time you open a new terminal or switch workspaces, your environment variables vanish. You’re stuck re-running export commands… again… and again.

Sound familiar?

This guide will show you how to set up permanent, one-command aliases (use-zai and use-anthropic) that persist across terminals, directories, and VS Code sessions — so you can focus on coding, not configuration.

What you’ll learn:

  • How ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN control your Claude requests.
  • How to create simple shell scripts to toggle between endpoints.
  • How to alias them for instant, global access.
  • How to verify your setup and avoid common pitfalls.

Let’s fix this — once and for all.


II. The Problem: Why Manual Switching Fails

When you run something like:

export ANTHROPIC_BASE_URL="https://api.z.ai/api/anthropic"
export ANTHROPIC_AUTH_TOKEN="sk-..."

…it only lasts for that terminal session. Close the tab? Open a new one in a different VS Code workspace? Poof — your settings are gone. Claude suddenly can’t find the model. You get errors. You waste time.

That’s because export sets temporary environment variables — they don’t survive shell restarts.

The fix? Make it permanent with aliases and scripts.


III. The Solution: Shell Aliases for Instant Switching

Instead of typing long export commands, we’ll:

  1. Create two tiny shell scripts: one for Z.AI, one for Anthropic.
  2. Add aliases to your shell profile so you can trigger them with use-zai or use-anthropic — from anywhere.
  3. Source them to ensure they modify your current shell session (not a subshell).

Benefits:

  • ✅ One-time setup
  • ✅ Works in any terminal, any folder
  • ✅ No more “model not found” errors
  • ✅ Clean, reversible switching

IV. Step-by-Step Setup (Mac / Zsh)

Prerequisites:

  • VS Code with the Claude extension installed
  • Your Z.AI API key
  • (Optional) Your Anthropic API key (if you plan to use Anthropic directly)
  • Terminal access (this guide assumes Zsh, default on macOS since Catalina)

Step 1: Create a Scripts Folder

Open your terminal and run:

mkdir -p ~/scripts
cd ~/scripts

This keeps your custom tools organized and out of the way.


Step 2: Create use-zai.sh

Run:

nano use-zai.sh

Paste this (and replace the placeholder with your actual Z.AI API key):

#!/bin/bash
# Switch to Z.AI endpoint for Claude

export ANTHROPIC_BASE_URL="https://api.z.ai/api/anthropic"
export ANTHROPIC_AUTH_TOKEN="YOUR_ZAI_API_KEY_HERE"  # ⚠️ REPLACE THIS

echo "✅ Terminal configured for Z.AI. Claude requests will now route through Z.AI."

Save & exit:
Press Ctrl+O → Enter → Ctrl+X


Step 3: Create use-anthropic.sh

Run:

nano use-anthropic.sh

Paste this:

#!/bin/bash
# Revert to default Anthropic endpoint

unset ANTHROPIC_BASE_URL
unset ANTHROPIC_AUTH_TOKEN

echo "🔄 Terminal reset to default Anthropic API."

(Note: unset removes the variable — letting Claude fall back to its default endpoint.)

Save & exit the same way.


Step 4: Add Aliases to Your Shell Profile

Run:

nano ~/.zshrc

Scroll to the bottom and add:

# Claude API Switcher
alias use-zai="source ~/scripts/use-zai.sh"
alias use-anthropic="source ~/scripts/use-anthropic.sh"

💡 Why source?
Using source ensures the script runs in your current shell — so the environment variables stick. If you just run the script, it executes in a subshell and vanishes.

Save & exit.

Then, reload your shell config:

source ~/.zshrc

✅ Done. Your aliases are now active — forever.


V. Your New Workflow: Switch in Seconds

To Use Z.AI:

  1. Open any terminal (in any folder).
  2. Type:
   use-zai
  1. Launch VS Code:
   code .
  1. (Optional) Verify:
   echo $ANTHROPIC_BASE_URL

→ Should return: https://api.z.ai/api/anthropic


To Use Default Anthropic:

  1. Open any terminal.
  2. Type:
   use-anthropic
  1. Launch VS Code:
   code .
  1. Verify:
   echo $ANTHROPIC_BASE_URL

→ Should return nothing (blank = unset = default)


📌 Important Note:
Switching models (Opus, Sonnet, Haiku) is done inside VS Code via:
Cmd/Ctrl+Shift+P → “Claude: Select Model”
This is independent of your API endpoint. You can use any model with either Z.AI or Anthropic.


VI. Conclusion: Smoother, Faster, Better

You’ve just upgraded from a fragile, manual setup to a robust, one-command workflow.

No more:

  • Re-typing exports
  • “Model not available” errors
  • Forgetting which terminal is configured for what

Just:

  • use-zai
  • use-anthropic
  • Code.

🚀 Your productivity just got a permanent boost.


💡 Pro Tips & Troubleshooting

  • Forgot to replace the API key?
    Re-open use-zai.sh and fix it. Then re-source: source ~/.zshrc
  • Aliases not working?
    Make sure you ran source ~/.zshrc after editing. Open a new terminal to test.
  • Already had claude_code_prod_zai.sh?
    Delete it or rename it to avoid confusion. Stick with this new system.