Skip to content

Docs / Tool calling

Tool calling

Define tools in the OpenAI or Anthropic format, run the call, send the result back. Limits, token counting and the token saver.

Get an API key

Every model in the catalogue accepts tools. You define them in the format you already use, the model answers with a call, you run it and send the result back. Yunshu Router passes your tool definitions to the model unchanged.

Limits

Limit Value
Tools per request 512
Formats tools on Chat Completions and Messages; function tools on Responses

OpenAI format

import json
from openai import OpenAI

client = OpenAI(base_url="https://api.yunshurouter.com/v1", api_key="tq-...")

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Current weather for a city.",
        "parameters": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    },
}]
messages = [{"role": "user", "content": "What is the weather in Jakarta?"}]

first = client.chat.completions.create(model="deepseek-v4-pro", messages=messages, tools=tools)
reply = first.choices[0].message

if reply.tool_calls:
    messages.append(reply)
    for call in reply.tool_calls:
        args = json.loads(call.function.arguments)
        result = {"city": args["city"], "temp_c": 31}  # your own function
        messages.append({
            "role": "tool",
            "tool_call_id": call.id,
            "content": json.dumps(result),
        })
    final = client.chat.completions.create(model="deepseek-v4-pro", messages=messages, tools=tools)
    print(final.choices[0].message.content)
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://api.yunshurouter.com/v1", apiKey: "tq-..." });

const tools: OpenAI.ChatCompletionTool[] = [{
  type: "function",
  function: {
    name: "get_weather",
    description: "Current weather for a city.",
    parameters: {
      type: "object",
      properties: { city: { type: "string" } },
      required: ["city"],
    },
  },
}];
const messages: OpenAI.ChatCompletionMessageParam[] = [
  { role: "user", content: "What is the weather in Jakarta?" },
];

const first = await client.chat.completions.create({ model: "deepseek-v4-pro", messages, tools });
const reply = first.choices[0].message;

if (reply.tool_calls?.length) {
  messages.push(reply);
  for (const call of reply.tool_calls) {
    if (call.type !== "function") continue;
    const args = JSON.parse(call.function.arguments);
    messages.push({
      role: "tool",
      tool_call_id: call.id,
      content: JSON.stringify({ city: args.city, temp_c: 31 }),
    });
  }
  const final = await client.chat.completions.create({ model: "deepseek-v4-pro", messages, tools });
  console.log(final.choices[0].message.content);
}

tool_choice (auto, none, required or one named function) is passed to the model.

Anthropic format

import anthropic

client = anthropic.Anthropic(base_url="https://api.yunshurouter.com", api_key="tq-...")

tools = [{
    "name": "get_weather",
    "description": "Current weather for a city.",
    "input_schema": {
        "type": "object",
        "properties": {"city": {"type": "string"}},
        "required": ["city"],
    },
}]
messages = [{"role": "user", "content": "What is the weather in Jakarta?"}]

first = client.messages.create(model="deepseek-v4-pro", max_tokens=1024, tools=tools, messages=messages)

if first.stop_reason == "tool_use":
    messages.append({"role": "assistant", "content": first.content})
    results = []
    for block in first.content:
        if block.type == "tool_use":
            results.append({
                "type": "tool_result",
                "tool_use_id": block.id,
                "content": '{"city": "Jakarta", "temp_c": 31}',
            })
    messages.append({"role": "user", "content": results})
    final = client.messages.create(model="deepseek-v4-pro", max_tokens=1024, tools=tools, messages=messages)
    print(final.content[0].text)
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({ baseURL: "https://api.yunshurouter.com", apiKey: "tq-..." });

const tools: Anthropic.Tool[] = [{
  name: "get_weather",
  description: "Current weather for a city.",
  input_schema: {
    type: "object",
    properties: { city: { type: "string" } },
    required: ["city"],
  },
}];
const messages: Anthropic.MessageParam[] = [
  { role: "user", content: "What is the weather in Jakarta?" },
];

const first = await client.messages.create({ model: "deepseek-v4-pro", max_tokens: 1024, tools, messages });

if (first.stop_reason === "tool_use") {
  messages.push({ role: "assistant", content: first.content });
  const results: Anthropic.ToolResultBlockParam[] = first.content
    .filter((b): b is Anthropic.ToolUseBlock => b.type === "tool_use")
    .map((b) => ({ type: "tool_result", tool_use_id: b.id, content: '{"city":"Jakarta","temp_c":31}' }));
  messages.push({ role: "user", content: results });
  const final = await client.messages.create({ model: "deepseek-v4-pro", max_tokens: 1024, tools, messages });
  console.log(final.content);
}

Responses format

On /v1/responses, function tools and namespace groups of function tools are sent to the model. Hosted tools (web_search, file_search, local_shell, custom) are dropped without an error. Return results as function_call_output items in the next input. See Responses API.

Streaming tool calls

With stream: true, tool calls arrive in pieces in the format you called: delta.tool_calls on Chat Completions, input_json_delta on Messages, and response.function_call_arguments.delta on Responses. The SDK stream helpers put them together for you. See Streaming.

Tool definitions cost tokens

Tool definitions are part of the prompt and are billed as input tokens on every request. An agent with many tools can spend thousands of tokens on definitions alone.

POST /v1/messages/count_tokens counts tool definitions, tool results and images, so you can check the size before you send. See Anthropic-compatible API.

Token saver for long tool outputs

Agents often send back long tool outputs: diffs, logs, file listings. The token saver shortens these before they are sent to the model and billed. It is off by default and switched on per API key in the console.

  • It changes only tool outputs, never your own messages or the system prompt.
  • To switch it off for one request, send x-yunshu-token-saver: off.
  • GET /v1/generation reports the estimated tokens saved in tokens.saved (null when the saver did not run).

Cached tool calls

When an identical request is answered from our cache, tool calls and the stop reason are returned exactly as the first time, streamed or not. See Prompt caching.