See Strict mode for tool calling for model-specific schema requirements.
How it works
- Define the tool: Provide a name, description, and input parameters for each tool you want the model to access.
- Send the request: The prompt is sent along with available tool definitions in your API call.
- Select a tool: The model determines whether a tool can help answer the request. If so, it returns the tool name and arguments.
- Execute the tool: The client application receives the model’s tool call request, executes the specified tool (such as calling an external API), and retrieves the result.
- Generate the final response: Send the tool result to the model so it can continue the conversation.
Basic tool calling
1
Set up the client
Import the required libraries and initialize the Cerebras client.
2
Define the function
Define the function that the application executes. This example uses a calculator that performs basic arithmetic.
3
Define the tool schema
Define the tool name, description, and parameters that the model can use.
For schemas in the supported JSON Schema subset,
strict: true uses constrained decoding to guarantee that tool-call arguments conform to the schema.4
Send the request
Send the messages and tool schema. The response can include a tool call.
5
Handle the tool call
Check the response for a tool call. If one is present, execute the requested function and return its result to the model.
Strict mode for tool calling
For schemas that use the supported JSON Schema subset, strict mode guarantees that tool-call arguments conform to the schema.Why strict mode matters for tools
Without strict mode, tool calls can contain:- Incorrect parameter types, such as
"2"instead of2 - Missing required parameters
- Unexpected parameters
- Malformed argument JSON
Enabling strict mode
Setstrict to true inside the function object of your tool definition:
Python
Schema requirements
When using strict mode, you must setadditionalProperties: false. This is required for every object in your schema.
For information about schema limitations that apply when using strict mode, see Limitations in Strict Mode.
For
kimi-k2.7-code, either set the same strict value on every function in the request or omit it from every function. For qwen-3.8-27b, do not use pattern, minLength, or maxLength in strict tool schemas.Strict mode with parallel tool calling
Strict mode works with parallel tool calling. When multiple tools are called simultaneously, each tool call’s arguments will conform to its respective schema:Python
Multi-turn tool calling
Most real-world workflows require more than one tool invocation. Multi-turn tool calling lets a model call a tool, incorporate its output, and then, within the same conversation, decide whether it needs to call the tool (or another tool) again to finish the task.- Append each tool result to
messagesand ask the model to continue. - Let the model determine whether it needs another tool call.
- Continue calling
client.chat.completions.create()until the response does not containtool_calls.
Parallel tool calling
Parallel tool calling lets a model request multiple independent tool calls in one response, which can reduce latency. For example, if a user asks “Is Toronto warmer than Montreal?”, the model needs to check the weather in both cities. Rather than making two separate requests, parallel tool calling enables the model to request both operations at once, reducing latency and improving efficiency. Use parallel tool calling when:- A request requires multiple independent data points, such as weather in different cities.
- Tool calls do not depend on the results of other tool calls.
Enable parallel tool calling
You can explicitly control this behavior using theparallel_tool_calls parameter:
Example: Weather comparison
The following example requests weather data for two cities in parallel.1
Define the weather tool
Define a weather function and its tool schema.
2
Send the request
Send a request that requires weather data for two cities.
3
Handle multiple tool calls
Iterate through every entry in the response’s
tool_calls array.
