Tool Calling
Let models call your functions
Tool calling lets a model ask your application to run a function — look up an order, query a database, call a weather API — and weave the result into its answer. Token Hub forwards the standard OpenAI tools parameter to every tool-capable model in the catalog.
Defining tools
pythontools = [{ "type": "function", "function": { "name": "get_weather", "description": "Current weather for a city", "parameters": { "type": "object", "properties": { "city": {"type": "string"}, "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} }, "required": ["city"] } } }] resp = client.chat.completions.create( model="glm-5-3", messages=[{"role": "user", "content": "Should I bring an umbrella in Lisbon?"}], tools=tools, )
The tool loop
When the model wants a tool, the response's finish_reason is"tool_calls" and the message contains structured calls. You execute them, then send results back as role: "tool" messages:
pythonmsg = resp.choices[0].message if msg.tool_calls: followup = [ {"role": "user", "content": "Should I bring an umbrella in Lisbon?"}, msg, {"role": "tool", "tool_call_id": msg.tool_calls[0].id, "content": '{"forecast": "light rain", "high": 18}'}, ] final = client.chat.completions.create( model="glm-5-3", messages=followup, tools=tools) print(final.choices[0].message.content)
Forcing or blocking tool use
tool_choice controls behavior: "auto" (default) lets the model decide, "none" disables tools, and{"type": "function", "function": {"name": "get_weather"}}forces a specific call.
Model support
Function calling is a per-model capability — each model page lists it under Supported functionality. Programmatically, the public catalog exposes it assupports_tools. Validate tool schemas before sending: malformed definitions are the most common cause of 400 bad_request errors.