Tool calling
Describe your own functions and the model can ask to call them. Your code runs the function and sends back the result. This is how agents look things up, take actions and work through a task step by step.
How it works
- You send the conversation and a list of
tools, each with a name, a description and its parameters as JSON Schema. - The model replies with
tool_callsinstead of text when it wants to use one. - Your code runs each function and adds the result to the conversation as a
toolmessage. - You send the conversation again. The model uses the results to answer, or calls more tools.
Example
python
import json
from openai import OpenAI
client = OpenAI(base_url="https://api.continuumlabs.ai/v1", api_key="cl-your-key")
def check_abn(abn):
# Your own code. Here, a stand-in.
return {"abn": abn, "status": "active", "entity": "Example Pty Ltd"}
tools = [{
"type": "function",
"function": {
"name": "check_abn",
"description": "Look up an Australian Business Number and return its registration status.",
"parameters": {
"type": "object",
"properties": {"abn": {"type": "string", "description": "11 digits, spaces allowed"}},
"required": ["abn"],
},
},
}]
messages = [{"role": "user", "content": "Is ABN 51 824 753 556 active?"}]
while True:
reply = client.chat.completions.create(model="glm-5.2", messages=messages, tools=tools)
message = reply.choices[0].message
if not message.tool_calls:
print(message.content)
break
messages.append(message)
for call in message.tool_calls:
result = check_abn(**json.loads(call.function.arguments))
messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})Check the model's arguments in code
Treat a tool call as a request, not an instruction. Check the arguments before you act on them: required fields present, values allowed, identifiers real. If something is wrong, send the error back as the tool result. The model will usually correct itself.
Every agent in the cookbook works this way. The contract review agent is a complete example.
Tips
- Write descriptions for the model, not for people. Say exactly when to use the tool.
- Keep the list short. A handful of well-described tools beats twenty vague ones.
- Set
tool_choice: "required"to force a tool call, or name a function to force that one. - Some models make several tool calls in one reply. Handle every item in
tool_calls.
