Skip to content

eyehol3/claude-glm-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

claude-glm-proxy

A lightweight local proxy that lets you use Z.ai GLM models inside Claude Code as a selectable model option, alongside the regular Claude models.

Claude Code only talks to the Anthropic API. This proxy sits in between: regular requests pass through to Anthropic unchanged, while any request for a model containing glm gets rerouted to Z.ai.

Claude Code → localhost:8787 → api.anthropic.com   (Claude models)
                             → api.z.ai             (GLM models)

The proxy auto-starts when Claude Code launches (via a SessionStart hook) and stays running in the background. No service registration, no platform-specific setup.

Requirements

  • Python 3.6+
  • A Z.ai API key with GLM access

Install

git clone https://github.com/eyehol3/claude-glm-proxy.git
cd claude-glm-proxy
./install.sh

The script copies model_proxy.py to ~/.config/model-proxy/ and creates a config file for your API key. Then it prints the JSON to merge into ~/.claude/settings.json.

After running:

  1. Set your API key in ~/.config/model-proxy/model-proxy.env
  2. Add the settings to ~/.claude/settings.json (the script prints them)

Usage

claude --model glm    # start with GLM

Inside a session, use /model to switch between Claude and GLM.

How it works

model_proxy.py is a zero-dependency Python HTTP proxy. On each request it reads the model field from the JSON body and matches it against a routing table:

Model contains Backend Path prefix
glm api.z.ai /api/anthropic
(anything else) api.anthropic.com (none)

The proxy rewrites auth headers per-backend automatically. API keys are read from ~/.config/model-proxy/model-proxy.env.

Claude Code's SessionStart hook handles lifecycle: it checks localhost:8787/health, starts the proxy if needed, waits a second, then verifies it's actually up. If it fails to start you'll see an error in the Claude Code banner. No launchd/systemd/cron needed.

Logs: ~/.config/model-proxy/model-proxy.log

Adding more backends

Extend MODEL_ROUTES in model_proxy.py:

MODEL_ROUTES = {
    "glm": ("api.z.ai", "/api/anthropic"),
    "mymodel": ("my-api.example.com", "/v1"),
}

Add the corresponding API key env var in model_proxy.py and set it in model-proxy.env.

Uninstall

  1. Remove the env keys (ANTHROPIC_BASE_URL, ANTHROPIC_CUSTOM_MODEL_OPTION*) and the SessionStart hook from ~/.claude/settings.json
  2. Kill the proxy and delete the files:
pkill -f model_proxy.py
rm -rf ~/.config/model-proxy

Claude Code will go back to talking directly to Anthropic's API.

About

Local proxy that adds Z.ai GLM as a model option in Claude Code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors