Tool calling

Describe your functions in tools. When the model decides to use one, the response contains tool_calls instead of text. You run the function, send the result back as a tool message, and the model continues.

1. Define tools

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get the current weather for a city.",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]

messages = [{"role": "user", "content": "What's the weather in Austin?"}]
response = client.chat.completions.create(
    model="moonshotai/kimi-k3.1-pro",
    messages=messages,
    tools=tools,
)

2. Run the call and return the result

import json

message = response.choices[0].message
messages.append(message)

for call in message.tool_calls or []:
    args = json.loads(call.function.arguments)
    result = get_weather(**args)  # your function
    messages.append({
        "role": "tool",
        "tool_call_id": call.id,
        "content": json.dumps(result),
    })

final = client.chat.completions.create(
    model="moonshotai/kimi-k3.1-pro",
    messages=messages,
    tools=tools,
)
print(final.choices[0].message.content)

Controlling tool use

  • "tool_choice": "auto" (default): the model decides.
  • "tool_choice": "none": never call a tool.
  • "tool_choice": "required": always call at least one tool.
  • "tool_choice": {"type": "function", "function": {"name": "get_weather"}}: call this tool.

A response can contain several tool calls. Run all of them and include every result in your next request.