Tool calling
Let a model call functions in your code, such as an invoice lookup, with the full loop, parallel calls, streaming and supported models.
With tool calling, a model can ask your code to run a function, for example to look up an invoice in your accounting system, and then answer with the result. Use it when the answer depends on data or actions that only your systems have.
The model never runs anything itself. It returns the name of a function and its arguments, and your code decides whether and how to run it. Everything on this page was tested against the live API on 11.11.2026.
How it works
- Send
messagesandtools. Each tool is a function with a name, a description and a JSON Schema for its parameters. - If the answer contains
tool_calls, run each function with the arguments it names. - Send the conversation again: add the assistant message with its
tool_calls, then onetoolmessage per call with the matchingtool_call_id. - Repeat until the model answers without
tool_calls.
Complete example
Nadia in accounting at Alpenblick Treuhand AG wants to know whether two invoices have been paid. A dictionary stands in for the accounting system.
Step 1 sends the question and the tool:
curl https://api.peakprivacy.ch/v1/ai/chat/completions \
-H "Authorization: Bearer $PEAKPRIVACY_API_KEY" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d '{
"model": "deepseek-v4-flash",
"messages": [{"role": "user", "content": "Has invoice RE-2026-0142 been paid?"}],
"tools": [{
"type": "function",
"function": {
"name": "get_invoice_status",
"description": "Look up the payment status of an invoice by its number.",
"parameters": {
"type": "object",
"properties": {
"invoice_number": {"type": "string", "description": "Invoice number, for example RE-2026-0142"}
},
"required": ["invoice_number"]
}
}
}]
}'The model asks for a call instead of answering. content is empty or holds a short sentence such as I'll look that up for you.
{
"choices": [{
"finish_reason": "tool_calls",
"message": {
"role": "assistant",
"content": "",
"tool_calls": [{
"id": "chatcmpl-tool-9617f8d39177fa97",
"type": "function",
"function": {"name": "get_invoice_status", "arguments": "{\"invoice_number\": \"RE-2026-0142\"}"}
}]
}
}]
}Step 2 sends the conversation back with the result. In your code, use the id from step 1. Any id works as long as both messages use the same one.
curl https://api.peakprivacy.ch/v1/ai/chat/completions \
-H "Authorization: Bearer $PEAKPRIVACY_API_KEY" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d '{
"model": "deepseek-v4-flash",
"messages": [
{"role": "user", "content": "Has invoice RE-2026-0142 been paid?"},
{"role": "assistant", "content": "", "tool_calls": [{
"id": "call_1",
"type": "function",
"function": {"name": "get_invoice_status", "arguments": "{\"invoice_number\": \"RE-2026-0142\"}"}
}]},
{"role": "tool", "tool_call_id": "call_1", "content": "{\"status\": \"paid\", \"paid_on\": \"2026-10-28\"}"}
],
"tools": [{
"type": "function",
"function": {
"name": "get_invoice_status",
"description": "Look up the payment status of an invoice by its number.",
"parameters": {
"type": "object",
"properties": {
"invoice_number": {"type": "string", "description": "Invoice number, for example RE-2026-0142"}
},
"required": ["invoice_number"]
}
}
}]
}'choices[0].message.content now holds the answer: Yes — invoice **RE-2026-0142** has been paid, on **2026-10-28**.
Output of the Python script. The JavaScript script prints the arguments as JSON. The wording of the answer varies from run to run.
Calling get_invoice_status({'invoice_number': 'RE-2026-0142'})
Calling get_invoice_status({'invoice_number': 'RE-2026-0143'})
Here are the results:
| Invoice | Status | Amount | Details |
|---|---|---|---|
| RE-2026-0142 | ✅ Paid | CHF 1,234.50 | Paid on 2026-10-28 |
| RE-2026-0143 | ⏳ Open | CHF 860.00 | Due on 2026-11-30 |
So only **RE-2026-0142** has been paid. **RE-2026-0143** is still open, with payment due by November 30, 2026.The same script ran unchanged with mistral-small-swiss, gpt-swiss, qwen-3-fast, gpt-4.1 and claude-sonnet.
Parallel tool calls
Asked about two invoices, most models return two tool_calls in one message, as above. Answer every call with its own tool message before you send the next request. gpt-swiss asks for one call per round, and the loop handles that too.
Streaming with tools
With "stream": true, tool calls arrive in pieces in choices[0].delta.tool_calls. Collect them by index: the first piece carries id and function.name, the following pieces add to function.arguments.
stream = client.chat.completions.create(
model="mistral-small-swiss",
messages=[{"role": "user", "content": "Have invoices RE-2026-0142 and RE-2026-0143 been paid?"}],
tools=TOOLS,
stream=True,
)
calls = {} # index -> collected tool call
for chunk in stream:
if not chunk.choices:
continue # the final usage chunk
delta = chunk.choices[0].delta
if delta.content:
print(delta.content, end="", flush=True)
for part in delta.tool_calls or []:
call = calls.setdefault(part.index, {"id": None, "name": "", "arguments": ""})
if part.id:
call["id"] = part.id
if part.function and part.function.name:
call["name"] += part.function.name
if part.function and part.function.arguments:
call["arguments"] += part.function.arguments
for call in calls.values():
print(call){'id': 'Sw79DsxfJ', 'name': 'get_invoice_status', 'arguments': '{"invoice_number": "RE-2026-0142"}'}
{'id': 'QaMh0m0eJ', 'name': 'get_invoice_status', 'arguments': '{"invoice_number": "RE-2026-0143"}'}These snippets reuse client and the tool list from the complete example. Once the stream has ended, send the collected calls back as in the loop, as {"id": …, "type": "function", "function": {"name": …, "arguments": …}}.
Known limitation: gpt-swiss streams incomplete tool-call arguments. Workaround: call gpt-swiss without stream when you send tools, or stream with a model marked Yes under Which models support it.
Which models support it
| Model | Level | Tool calls | Parallel calls | Streaming |
|---|---|---|---|---|
deepseek-v4-flash, glm5 | Schweiz | Yes | Yes | Yes ¹ |
mistral-small-swiss | Schweiz | Yes | Yes | Yes |
gpt-swiss | Schweiz | Yes | One per round | No: arguments arrive incomplete |
gpt-eu, deepseek-r1 | EU | Yes | Yes | Yes ¹ |
qwen-3, qwen-3-fast | EU | Yes | Yes | Yes |
gemma-3 | EU | No: tools are ignored | – | – |
gpt-4o, gpt-4.1 | Extern | Yes | Yes | Yes |
gpt-5 | Extern | Only with "reasoning_effort": "none", otherwise 500. Or use /responses. | Yes | Yes |
claude-sonnet, claude-opus | Extern | Yes | Yes | Yes |
gemini-3.5-flash, gemini-3-pro-preview | Extern | No: tools are ignored | – | – |
¹ Text that the model writes before its tool calls can lose its first words to delta.reasoning_content, see Reasoning › Streaming. The tool calls arrive complete.
"Tools are ignored" means: the request succeeds, but no tool call comes back. The model answers in text, says it has no access to your data, or returns an empty answer. Check finish_reason: only tool_calls means the model wants a call.
Request fields
tools: only"type": "function".parametersis a JSON Schema object.tool_choiceandparallel_tool_calls: dropped without an error, as are the olderfunctionsandfunction_call. You can't force or forbid a call: in our tests,"tool_choice": "none"still produced a call. To rule out calls, send the request withouttools. To make calls more likely, say in the system message when the model should use a tool.- Messages: an
assistantmessage carriestool_calls, and atoolmessage carriestool_call_idand a stringcontent. Encode objects as JSON. - Anonymisation: with
"anonymize": true,toolmessages are processed like user messages, and the arguments intool_callscome back with the original values. See Anonymisation.
Responses endpoint
Tools also work on POST /v1/ai/responses for models with meta.capabilities.responses: true. Tool definitions are flat, calls arrive as function_call items in output, and you return results as function_call_output items. previous_response_id is dropped, so send the whole conversation each time. The snippet reuses client, json and get_invoice_status from the complete example.
tools = [{
"type": "function",
"name": "get_invoice_status",
"description": "Look up the payment status of an invoice by its number.",
"parameters": {
"type": "object",
"properties": {"invoice_number": {"type": "string"}},
"required": ["invoice_number"],
},
}]
inputs = [{"role": "user", "content": "Has invoice RE-2026-0142 been paid?"}]
response = client.responses.create(model="gpt-5", input=inputs, tools=tools)
for item in response.output:
if item.type == "function_call":
result = get_invoice_status(**json.loads(item.arguments))
inputs.append({"type": "function_call", "call_id": item.call_id,
"name": item.name, "arguments": item.arguments})
inputs.append({"type": "function_call_output", "call_id": item.call_id,
"output": json.dumps(result)})
final = client.responses.create(model="gpt-5", input=inputs, tools=tools)
print(final.output_text) # Yes. Invoice **RE-2026-0142** was paid on **October 28, 2026**, for **CHF 1,234.50**.Pitfalls
- Answer every call. A conversation with a tool call but no matching
toolmessage failed with500ongpt-4.1andclaude-sonnet. Retrying won't help. Fix the messages. - Treat arguments as untrusted input.
argumentsis a JSON string written by the model. Parse it, validate the values, and ask a person before your code changes or sends anything. - Limit the rounds. A model can keep asking for calls. Stop after a few rounds, as the loop above does.
- Tools cost tokens. Every tool definition is sent with every request and counts in
prompt_tokens. Step 1 of the example used about 330 prompt tokens for a one-line question.
See also
Getting JSON back
Get validated JSON from any model without response_format, with a precise system prompt, pydantic or zod, one retry and test results per model.
Answer questions from your documents
Upload documents, find the ones that fit a question and let a model answer with sources, tested in curl, Python and JavaScript.