An r/ZaiGLM post asked: can anyone use web_search with GLM? GLM accepts the OpenAI tool-calling shape, so any HTTP search API plugs in. This tutorial wires Scavio as a tool.
Prerequisites
- GLM API key (Zhipu)
- Scavio API key
- Python 3.10+
Walkthrough
Step 1: Install zhipuai
Official Python SDK.
pip install zhipuaiStep 2: Define a Scavio tool function
Plain function, returns JSON.
import os, requests
def scavio_search(query: str) -> dict:
return requests.post('https://api.scavio.dev/api/v1/search',
headers={'x-api-key': os.environ['SCAVIO_API_KEY']},
json={'query': query}).json()Step 3: Register tool with GLM
OpenAI-style tools schema.
tools = [{
'type': 'function',
'function': {
'name': 'scavio_search',
'description': 'Live web search — SERP results plus AI Overview citations',
'parameters': {'type': 'object', 'properties': {'query': {'type': 'string'}}, 'required': ['query']}
}
}]Step 4: Call GLM with tool list
Standard chat-completion with tools.
from zhipuai import ZhipuAI
client = ZhipuAI(api_key=os.environ['GLM_API_KEY'])
resp = client.chat.completions.create(
model='glm-4-plus',
messages=[{'role': 'user', 'content': 'best mcp practices 2026'}],
tools=tools,
)Step 5: Handle tool calls
If GLM returns tool_calls, run scavio_search and feed back.
for tc in resp.choices[0].message.tool_calls or []:
if tc.function.name == 'scavio_search':
args = json.loads(tc.function.arguments)
result = scavio_search(args['query'])
# Feed result back as tool message and re-call GLM.Python Example
# GLM agent now has live web search. Per-call cost: $0.0043 Scavio + GLM tokens.JavaScript Example
// Same shape in TS using the OpenAI SDK with GLM's base_url.Expected Output
GLM agent responds to time-sensitive questions with grounded answers. Without the tool, GLM falls back to training-data knowledge.