Skip to content
All docs

Guides

Tool calls

Both models call tools in OpenAI’s format: you describe functions, the model asks for one, you run it and send back the result.

Checked 26 October 2026

The loop

  1. Send tools: each one a function with a name, a description and JSON Schema parameters.
  2. When the model wants one, the reply’s finish_reason is tool_calls and message.tool_calls lists them, with arguments as a JSON string.
  3. Run each one, and add a tool message with its tool_call_id and the result.
  4. Send the conversation again. Repeat until finish_reason is stop.
Python
import json
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://nokeep.ai/v1",
    api_key=os.environ["NOKEEP_API_KEY"],
)

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Today's weather in a city",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]

def get_weather(city):
    return {"city": city, "high_c": 21, "sky": "clear"}

messages = [{"role": "user", "content": "Should I bring a jacket in Lisbon today?"}]
while True:
    reply = client.chat.completions.create(model="deepseek-v4.1-flash-uncensored", messages=messages, tools=tools)
    message = reply.choices[0].message
    messages.append(message)
    if not message.tool_calls:
        print(message.content)
        break
    for call in message.tool_calls:
        result = get_weather(**json.loads(call.function.arguments))
        messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})

tool_choice

ModelTakes
deepseek-v4.1-flash-uncensoredauto, none, required, or a named function. Also parallel_tool_calls.
glm-5.3-flash-uncensoredauto or none. It picks its own tools; anything else is 400 unsupported_tool_choice.

Good to know

  • Every tool message needs the tool_call_id it answers, or the call is refused with 400 invalid_messages.
  • Tool schemas travel with every request and count as input tokens.
  • The models don’t run anything themselves. Your code runs the tools, so it decides what they can reach.