OpenAI-compatible local API proxy for Z.ai / GLM Web (chat.z.ai).
Allows you to use GLM (GLM Chat with native thinking/reasoning and web search toggles) with OpenCode or any OpenAI-compatible client.
This project is for educational and research purposes only. It is not affiliated with or endorsed by Zhipu AI or Z.ai.
- OpenAI API Compatibility: Exposes
http://127.0.0.1:1341/v1/chat/completionsand/v1/models. - Thinking / Reasoning: Streams thinking / reasoning process into
reasoning_contentdelta chunks in real-time. - UI Setting Respect: Uses
glm-chat, which automatically respects whatever you toggle in the Z.ai Web UI (Thinking mode, Web Search). - Tool Calling: Translates tool schemas and parses
<tool_call>outputs into OpenAI function call structures for agent tools (write,edit,bash,read). - Real-Time Token Tracking: Reports native token usage (
prompt_tokens,completion_tokens,total_tokens). - Chat Management: Send
/clear,/reset, or/newin chat to start a clean conversation session. - Lightweight: Pure Python (
aiohttp) + Tampermonkey script with no heavy automation frameworks.
pip install -r requirements.txt- Install Tampermonkey or Violentmonkey in your browser.
- Create a new userscript and paste the contents of
glm-bridge.user.js. - Open chat.z.ai and log in.
- You will see a badge at the bottom-right: Bridge: Connected (Ready) once the proxy is running.
python glm-proxy.pyOptions:
--host 127.0.0.1: Listening host (default:127.0.0.1).--port 1341: Listening port (default:1341).
Add this provider to your OpenCode config (opencode.jsonc):
{
"provider": {
"glm-proxy": {
"api": "openai",
"name": "GLM Web Proxy",
"options": {
"baseURL": "http://127.0.0.1:1341/v1",
"apiKey": "nah",
"timeout": 3600000,
"chunkTimeout": 3600000
},
"models": {
"glm-chat": {
"id": "glm-chat",
"name": "GLM Chat (Web Proxy)",
"tool_call": true,
"reasoning": true,
"temperature": true
},
"glm-thinking": {
"id": "glm-thinking",
"name": "GLM Thinking (Web Proxy)",
"tool_call": true,
"reasoning": true,
"temperature": true
}
}
}
}
}- Base URL:
http://127.0.0.1:1341/v1 - API Key:
nah - Models:
glm-chat: Recommended default (respects active toggles in Web UI)glm-thinking: Forces thinking / reasoning mode via API
from openai import OpenAI
client = OpenAI(
base_url="http://127.0.0.1:1341/v1",
api_key="nah",
)
response = client.chat.completions.create(
model="glm-chat",
messages=[{"role": "user", "content": "Hello GLM"}],
stream=True,
)
for chunk in response:
reasoning = getattr(chunk.choices[0].delta, "reasoning_content", None)
if reasoning:
print(reasoning, end="", flush=True)
content = chunk.choices[0].delta.content or ""
print(content, end="", flush=True)- Keep the browser tab open while using the proxy.
- If the badge shows disconnected, click it to reconnect immediately.
- To reset manually, send
/clear,/reset, or/newdirectly from your client prompt (or runwindow.deleteCurrentChat()in the browser console).
MIT