tool_calls with JSON arguments. You then execute the functions and send the results back so the model can produce a final answer.
Set your API key as an environment variable and install the SDK.
Request a tool call
Pass your function definitions intools. The model returns a tool_call when it needs one.
Return the tool result
Execute the requested function, then append the assistant message and atool message (matched by tool_call_id) and call the API again. The model uses the result to produce its final response. Repeat this loop for multi-step agent workflows. The examples below reuse the tools definition from above.
Parallel tool calls
Mercury 2.5 can return multiple tool calls in one response when the operations are independent. Execute them concurrently to reduce latency, then append onetool message for each call before requesting the final response.
Python
Tool calling parameters
tool_choiceUsetool_choiceto enforce the tool calling behavior that you desire. By default,tool_choiceis set to"auto". This means that the model will choose to either emit tool calls or output text. If set to"none", tool calls are prevented. If set to"required", the model is forced to emit a tool call in that turn. Use this to enforce the behavior you desire. Just addtool_choiceto the payload to use it. See below for an example:
strictUsestrictto force the model to output the correct tool calling schema that you have specified. We recommend setting this field toTruefor best results. To do this, add"strict": Trueto your tool definition. See below for an example.
Coding agent tool calling demo
See how you can use tool calling with Mercury 2.5 with the following simple coding agent example. We assume a small codebase of the following structure:Define tools
First, define your tools. Here, we define two tools for a read-only coding agent:list_directory and read_file.