Skip to main content
Mercury 2.5 supports OpenAI-compatible tool calling on the chat completions endpoint, including parallel tool calls. You define functions the model may call; when the model decides tools are needed, it returns one or more 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 in tools. The model returns a tool_call when it needs one.

Return the tool result

Execute the requested function, then append the assistant message and a tool 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 one tool message for each call before requesting the final response.
Python
Use sequential calls when a later operation depends on the result of an earlier one.

Tool calling parameters

  • tool_choice Use tool_choice to enforce the tool calling behavior that you desire. By default, tool_choice is 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 add tool_choice to the payload to use it. See below for an example:
  • strict Use strict to force the model to output the correct tool calling schema that you have specified. We recommend setting this field to True for best results. To do this, add "strict": True to 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.

Prompt your model

We prompt the model to search the codebase for a specific function and return its definition to the user.

Run your agent

Results

When we run the above code, we get the following results: Step 1: Model’s tool call:
Tool call output:
Step 2: Model’s tool call:
Tool call output:
Step 3: Model’s output:
See the full demo’s code in this cookbook.