Skip to main content
Tool calling, also known as tool use or function calling, lets a model request functions that your application defines. Your application executes each requested function and returns the result to the model. See Strict mode for tool calling for model-specific schema requirements.

How it works

  1. Define the tool: Provide a name, description, and input parameters for each tool you want the model to access.
  2. Send the request: The prompt is sent along with available tool definitions in your API call.
  3. Select a tool: The model determines whether a tool can help answer the request. If so, it returns the tool name and arguments.
  4. 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.
  5. 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.
If you have not configured an API key, complete the Quickstart first.
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.
The example produces output similar to the following:

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 of 2
  • Missing required parameters
  • Unexpected parameters
  • Malformed argument JSON
Strict mode prevents these schema violations for supported schemas.

Enabling strict mode

Set strict to true inside the function object of your tool definition:
Python

Schema requirements

When using strict mode, you must set additionalProperties: 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.
  1. Append each tool result to messages and ask the model to continue.
  2. Let the model determine whether it needs another tool call.
  3. Continue calling client.chat.completions.create() until the response does not contain tool_calls.
The following example extends the calculator example. Complete steps 1 through 3 in Basic tool calling before running it.

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 the parallel_tool_calls parameter:
To disable parallel tool calling and force sequential execution:

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.