Usage patterns for tool use (function calling)
Usage patterns for tool use (function calling)
Usage patterns for tool use (function calling)
The tool use feature of the Chat endpoint comes with a set of capabilities that enable developers to implement a variety of tool use scenarios. This section describes the different patterns of tool use implementation supported by these capabilities. Each pattern can be implemented on its own or in combination with the others.
First, import the Cohere library and create a client.
We’ll use the same search_docs tool as in the previous example.
The model can determine that more than one tool call is required, where it will call multiple tools in parallel. This can be calling the same tool multiple times or different tools for any number of calls.
In the example below, the user asks for documentation about tool use and structured outputs. This requires calling the search_docs tool twice, once per topic. This is reflected in the model’s response, where two parallel tool calls are generated.
Example response:
State management
When tools are called in parallel, we append the messages list with one single assistant message containing all the tool calls and one tool message for each tool call.
The sequence of messages is represented in the diagram below.
A key attribute of tool use systems is the model’s ability to choose the right tools for a task. This includes the model’s ability to decide to not use any tool, and instead, respond to a user message directly.
In the example below, the user asks a simple arithmetic question. The model determines that it does not need to use any of the available tools (only one, search_docs, in this case), and instead, directly answers the user.
Example response:
State management
When the model opts to respond directly to the user, there will be no items 2 and 3 above (the tool calling and tool response messages). Instead, the final assistant message will contain the model’s direct response to the user.
Note: you can force the model to directly answer every time using the tool_choice parameter, described here
The Chat endpoint supports multi-step tool use, which enables the model to perform sequential reasoning. This is especially useful in agentic workflows that require multiple steps to complete a task.
As an example, suppose a tool use application has access to a web search tool. Given the question “What was the revenue of the most valuable company in the US in 2023?”, it will need to perform a series of steps in a specific order:
To illustrate this, we’ll use our search_docs tool and ask a question that usually requires multiple searches (first for tool use basics, then for tool_choice).
Here’s the function definition for the tool:
And here is the corresponding tool schema:
Next, we implement the four-step tool use workflow as described in the previous page.
The key difference here is the second (tool calling) and third (tool execution) steps are put in a while loop, which means that a sequence of this pair can happen for a number of times. This stops when the model decides in the tool calling step that no more tool calls are needed, which then triggers the fourth step (response generation).
In this example, the user asks for an explanation of tool use and how to force tool usage, with citations.
The model may decide it needs to look up documentation about tool use first, then do a second search for specifics about forcing tool usage via tool_choice.
This is reflected in the model’s response, where multiple tool calling-result pairs can be generated in a sequence.
Example response:
State management
In a multi-step tool use scenario, instead of just one occurence of assistant-tool messages, there will be a sequence of assistant-tool messages to reflect the multiple steps of tool calling involved.
This feature is only compatible with the Command R7B and newer models.
As shown in the previous examples, during the tool calling step, the model may decide to either:
You can, however, force the model to choose one of these options. This is done via the tool_choice parameter.
tool_choice parameter to REQUIRED.tool_choice parameter to NONE.By default, if you don’t specify the tool_choice parameter, then it is up to the model to decide whether to make tool calls or respond directly.
State management
Here’s the sequence of messages when tool_choice is set to REQUIRED.
Here’s the sequence of messages when tool_choice is set to NONE.
Building chatbots requires maintaining the memory or state of a conversation over multiple turns. To do this, we can keep appending each turn of a conversation to the messages list.
As an example, here’s the messages list from the first turn of a conversation.
Then, in the second turn, when provided with a rather vague follow-up user message, the model correctly infers that the context is still about tool use, and searches for information about tool_choice.
Example response:
State management
The sequence of messages is represented in the diagram below.