Skip to main content
Tool calls (also known as function calls) give an LLM access to external tools. The LLM does not call the tools directly. Instead, it suggests the tool to call. The user then calls the tool separately and provides the results back to the LLM. Finally, the LLM formats the response into an answer to the user’s original question. OpenRouter standardizes the tool calling interface across models and providers, making it easy to integrate external tools with any supported model. Supported Models: You can find models that support tool calling by filtering on openrouter.ai/models?supported_parameters=tools. If you prefer to learn from a full end-to-end example, keep reading.

Tool calling with the Agent SDK

The OpenRouter Agent SDK (@openrouter/agent for TypeScript, openrouter_agent for Python) runs the tool-calling loop for you. You define a tool with a name, description, input schema, and an execute function. When the model requests the tool, the SDK validates the arguments, runs your function, sends the result back to the model, and repeats until the model gives a final answer. You don’t need to append output items, match tool call IDs, or write your own agentic loop. The example below uses the same Project Gutenberg book search tool as the rest of this page.
Output

How tool calling works under the hood

The rest of this page does by hand what the Agent SDK does for you: it sends the tools, runs the requested calls, returns the results to the model, and loops until the model gives a final answer.

Request Body Examples

Tool calling with OpenRouter involves three key steps. Here are the essential request body formats for each step, first for the Responses API (POST /api/v1/responses) and then for Chat Completions (POST /api/v1/chat/completions).

Responses API

Step 1: Inference Request with Tools
Step 2: Tool Execution (Client-Side)
After receiving a response whose output contains a function_call item, execute the requested tool locally and prepare the result:
Step 3: Inference Request with Tool Results

Chat Completions API

On Chat Completions, function tools are nested under function, the model returns tool_calls on an assistant message, and tool results go back as role: "tool" messages.
Step 1: Inference Request with Tools
Step 2: Tool Execution (Client-Side)
After receiving the model’s response with tool_calls, execute the requested tool locally and prepare the result:
Step 3: Inference Request with Tool Results
Note: On both APIs, the tools parameter must be included in every request (Steps 1 and 3) so the router can validate the tool schema on each call.

Tool Calling Example

Here is an end-to-end example that gives LLMs the ability to call an external API — in this case Project Gutenberg, to search for books. First, let’s do some basic setup:

Define the Tool

Next, we define the tool that we want to call. Remember, the tool is going to get requested by the LLM, but the code we are writing here is ultimately responsible for executing the call and returning the results to the LLM.
Note that the “tool” is just a normal function. We then write a JSON “spec” compatible with the OpenAI function calling parameter. We’ll pass that spec to the LLM so that it knows this tool is available and how to use it. It will request the tool when needed, along with any arguments. We’ll then marshal the tool call locally, make the function call, and return the results to the LLM.

Tool use and tool results

Let’s make the first OpenRouter API call to the model:
response.output contains function_call items with a call_id, name, and JSON-encoded arguments. Production code should verify that at least one exists; this example assumes one does:
The input array now has:
  1. Our original request
  2. The LLM’s reasoning items, when present, and function_call items
  3. A function_call_output item with the tool result
Now, we can make a second OpenRouter API call, and hopefully get our result!
The output will be something like:
We did it! We’ve successfully used a tool in a prompt.

A Simple Agentic Loop

In the example above, the calls are made explicitly and sequentially. To handle a wide variety of user inputs and tool calls, you can use an agentic loop. Here’s an example of a simple agentic loop (using the same tools, instructions, and initial input as above):

Advanced patterns

Tool Calling Best Practices covers interleaved thinking between tool calls, function definition guidelines, streaming tool calls, tool choice configuration, parallel tool calls, multi-tool workflows, and reliability tracking. For more details on the Responses API input format and tool parameters, see the Responses API reference.