Okay, so here’s the thing. If you use Claude in VS Code and you’ve tried switching between Z.AI and Anthropic’s API, you know the pain. You set your environment variables, everything works great, then you open a new terminal and… nothing. Gone. You’re back to square one.
I got tired of typing the same export commands every single time I opened VS Code. So I figured out a way to make it stick. Two simple commands: use-zai and use-anthropic. That’s it. Works everywhere, survives restarts, and you never have to think about it again.
The Actual Problem
When you do this:
export ANTHROPIC_BASE_URL="https://api.z.ai/api/anthropic"
export ANTHROPIC_AUTH_TOKEN="sk-..."
It only works in that one terminal session. Close the tab? Open a new workspace? Your settings vanish. Claude can’t find the model. You get errors. You waste five minutes figuring out what went wrong.
The reason is simple: export is temporary. It doesn’t survive when you restart your shell.
What We’re Actually Going to Do
Instead of manually typing those commands over and over, we’re going to:
- Make two tiny scripts (one for Z.AI, one for Anthropic)
- Add aliases to your shell so you can run them from anywhere
- Use
sourceto make sure they actually modify your current terminal session
After this, switching is literally just typing use-zai or use-anthropic. Done.
Setting This Up (for Mac/Zsh)
This assumes you’re on macOS and using Zsh (which has been the default since like 2019). If you’re on Linux or using Bash, the steps are basically the same, just edit ~/.bashrc instead of ~/.zshrc.
Step 1: Make a folder for scripts
mkdir -p ~/scripts
cd ~/scripts
Nothing fancy. Just a place to keep these so they’re not floating around.
Step 2: Create the Z.AI script
nano use-zai.sh
Paste this in (and replace YOUR_ZAI_API_KEY_HERE with your actual key):
#!/bin/bash
export ANTHROPIC_BASE_URL="https://api.z.ai/api/anthropic"
export ANTHROPIC_AUTH_TOKEN="YOUR_ZAI_API_KEY_HERE"
echo "Terminal configured for Z.AI."
Save it: Ctrl+O, hit Enter, then Ctrl+X to exit.
Step 3: Create the Anthropic script
nano use-anthropic.sh
Paste this:
#!/bin/bash
unset ANTHROPIC_BASE_URL
unset ANTHROPIC_AUTH_TOKEN
echo "Terminal reset to default Anthropic API."
The unset command just removes those variables entirely, so Claude falls back to its default endpoint.
Save and exit.
Step 4: Add aliases to your shell
nano ~/.zshrc
Scroll to the bottom and add these lines:
# Claude API switching
alias use-zai="source ~/scripts/use-zai.sh"
alias use-anthropic="source ~/scripts/use-anthropic.sh"
Important: we’re using source here because it runs the script in your current shell session. If you just run the script normally, it executes in a subshell and the environment variables disappear immediately.
Save, exit, then reload your shell:
source ~/.zshrc
And that’s it. You’re done.
How to Actually Use This
To switch to Z.AI:
Open any terminal, type:
use-zai
Then launch VS Code from that terminal:
code .
You can verify it worked by checking:
echo $ANTHROPIC_BASE_URL
Should show: https://api.z.ai/api/anthropic
To switch back to Anthropic:
use-anthropic
code .
Check with:
echo $ANTHROPIC_BASE_URL
Should be blank (which means it’s using the default).
A Quick Note About Models
Switching between Opus, Sonnet, and Haiku is separate from this. You do that inside VS Code with Cmd/Ctrl+Shift+P → “Claude: Select Model”.
This setup just controls which API endpoint you’re hitting. You can use any model with either Z.AI or Anthropic.
Troubleshooting
Aliases not working?
Make sure you ran source ~/.zshrc after editing it. If that doesn’t work, just close and reopen your terminal.
Forgot to add your API key?
Go back and edit use-zai.sh. Replace the placeholder, save, then run source ~/.zshrc again.
Still getting errors?
Check if you already have other Claude-related scripts or environment variables set. Old setups can conflict with this one.
That’s basically it. No more re-typing exports. No more “model not found” errors. Just two commands and you’re good to go.