Workflow:Cohere ai Cohere python Tool Use Agentic Chat
| Knowledge Sources | |
|---|---|
| Domains | LLMs, Function_Calling, Agentic_AI, API_Client |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
End-to-end process for implementing agentic tool use with Cohere models, enabling the model to call external functions and incorporate their results into responses.
Description
This workflow implements the tool use pattern (also known as function calling) where the model decides which tools to invoke based on the user's query, generates structured tool call arguments, and incorporates tool execution results into a coherent final response. The SDK supports V2 tool definitions with JSON Schema parameter specifications, strict tool mode for guaranteed schema compliance, and multi-turn tool call loops for complex agentic workflows.
Usage
Execute this workflow when building applications that need the model to interact with external systems (databases, APIs, calculators, file systems) or when the model must produce structured outputs conforming to a specific schema. Tool use enables grounded, factual responses by giving the model access to real-time data sources.
Execution Steps
Step 1: Define Tool Schemas
Create ToolV2 objects that describe the available functions. Each tool has a type (always "function"), a function name, description, and a JSON Schema definition of its parameters. The model uses these descriptions to determine when and how to call each tool.
Key considerations:
- The ToolV2 type wraps a ToolV2Function with name, description, and parameters fields
- Parameters follow JSON Schema specification with type, properties, and required fields
- Clear, descriptive function names and descriptions improve the model's tool selection accuracy
- The strict_tools parameter forces tool call arguments to exactly match the JSON Schema
Step 2: Send the Initial Chat Request with Tools
Call chat() or chat_stream() with the message history and the tools list. The model analyzes the user's query and decides whether to call one or more tools.
Key considerations:
- The tool_choice parameter controls tool calling behavior (auto, required, none)
- When tool_choice is "required", the model must call at least one tool
- Multiple tools can be called in a single response (parallel tool use)
- The thinking parameter can be combined with tools for reasoning transparency
Step 3: Detect and Parse Tool Calls
Examine the response for tool_calls in the assistant message. Each ToolCallV2 contains an id, type ("function"), and a function object with the tool name and JSON-encoded arguments.
Key considerations:
- Tool calls may be absent if the model determines no tools are needed
- Each tool call has a unique id field (auto-generated UUID if not provided, via the overrides module)
- The function.arguments field is a JSON string that must be parsed
- In streaming mode, tool call arguments arrive incrementally via delta events
Step 4: Execute the Tool Functions
Parse the tool call arguments and invoke the corresponding local functions. This step is entirely client-side; the SDK does not execute tools automatically.
Key considerations:
- Validate parsed arguments before execution for safety
- Handle tool execution errors gracefully; error messages can be sent back to the model
- Tool execution can be parallelized when multiple independent tool calls are requested
- Record tool results for inclusion in the next chat turn
Step 5: Send Tool Results Back
Construct tool result messages (role: "tool") containing the tool_call_id and the execution output. Append these to the conversation history along with the assistant's tool call message, then make another chat request.
Key considerations:
- The ToolMessageV2 type requires tool_call_id to match the id from the tool call
- Tool content can be text strings or structured content items
- The conversation must include the assistant message with tool_calls followed by the tool result messages
- The model may generate additional tool calls in a multi-step agentic loop
Step 6: Process the Final Response
After the model has all the information it needs (no more tool calls), it generates the final text response incorporating the tool results. Extract the text content, citations, and metadata.
Key considerations:
- The agentic loop continues until the model responds without tool_calls
- Citations may reference tool outputs as sources
- Set a maximum iteration limit to prevent infinite tool call loops
- The final response follows the same structure as a standard chat response