Tool calling

Let a model request functions that your application controls and executes.

Describe your tools

Provide function definitions in tools, with a name, description, and JSON Schema for arguments. The model returns a proposed tool call; your application decides whether and how to execute it.

The example defines a local, read-only function. It does not give the model access to your machine.

import json
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["BULKTOKEN_API_KEY"], base_url="https://bulktoken.ai/v1")
messages = [{"role": "user", "content": "What is the status of order demo-123?"}]
tools = [{
    "type": "function",
    "function": {
        "name": "get_order_status",
        "description": "Look up the status of one order.",
        "parameters": {
            "type": "object",
            "properties": {"order_id": {"type": "string"}},
            "required": ["order_id"],
        },
    },
}]
response = client.chat.completions.create(
    model="glm-5.3", messages=messages, tools=tools,
)
message = response.choices[0].message
messages.append(message.model_dump(exclude_none=True))

if message.tool_calls:
    for call in message.tool_calls:
        args = json.loads(call.function.arguments)
        if call.function.name != "get_order_status" or args.get("order_id") != "demo-123":
            raise ValueError("Unsupported tool or order")
        result = {"order_id": "demo-123", "status": "processing"}
        messages.append({
            "role": "tool", "tool_call_id": call.id,
            "content": json.dumps(result),
        })
    final = client.chat.completions.create(model="glm-5.3", messages=messages)
    print(final.choices[0].message.content)
else:
    print(message.content)

Complete the tool round trip

  1. Preserve the assistant message, including tool_calls and any reasoning fields returned by the model.
  2. Parse and validate each function name and its JSON arguments.
  3. Execute the allowed operation in your application.
  4. Append a role: "tool" message with the matching tool_call_id and a string result.
  5. Send the updated conversation to the same model for the answer.

A model can return no calls, one call, or multiple calls. Production agents may need several rounds; give them explicit execution and iteration limits.

Application responsibilities

Tool arguments are generated input, not trusted instructions. Check authorization and validate arguments before accessing customer data or taking an action. Keep write operations under your application’s normal permission and confirmation rules.

For streaming tool calls, assemble argument fragments before parsing JSON or executing a function. Do not execute each fragment as it arrives.