Tool calling (also called function calling) is what turns a chat model into an *agent*. You describe functions the model can call, it decides when to call them and with what arguments, your code runs them, and the result goes back to the model. DeepSeek V4 and GLM-5.1 both support the standard OpenAI tools interface, so the pattern you already know works unchanged.
Define a tool
import json, os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["TURILOOP_API_KEY"],
base_url="https://api.turiloop.com/v1",
)
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]The tool-call loop
def run(user_msg: str) -> str:
messages = [{"role": "user", "content": user_msg}]
r = client.chat.completions.create(
model="deepseek-v4-pro",
messages=messages,
tools=tools,
)
msg = r.choices[0].message
if msg.tool_calls:
messages.append(msg)
for call in msg.tool_calls:
args = json.loads(call.function.arguments)
result = get_weather(args["city"]) # your real function
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps(result),
})
r = client.chat.completions.create(
model="deepseek-v4-pro", messages=messages, tools=tools,
)
return r.choices[0].message.content
return msg.contentThat's the whole agent loop: you ask, the model requests a tool, you run it, you feed the result back, and the model answers.
Picking the model
- GLM-5.1 is a strong, low-cost default for tool-heavy agent loops. Switch by changing
modelto"glm-5.1". - DeepSeek V4-Pro when the agent needs harder reasoning between tool calls.
- Kimi K2.6 when the agent carries a long, stable context (its cache pricing makes the repeated system prompt cheap).
Practical tips
- Keep tool descriptions tight and unambiguous. That's what the model reads to decide when to call them.
- Validate arguments before executing; models occasionally produce malformed JSON.
- Cap the loop iterations so a confused agent can't spin forever.
All three models live behind one OpenAI-compatible key on Turiloop, so you can A/B the same agent across DeepSeek, GLM and Kimi by changing one string. Pay-as-you-go, international card.