跳转到主要内容

Tool Calling

本文档只收录 已在 Crazyrouter 生产环境实际请求验证成功 的 Tool Calling 用法。 验证时间:
  • 2026-03-22
已验证模型:
  • gpt-5.4
  • claude-sonnet-4-6
  • gemini-3-pro-preview

已验证能力矩阵

模型协议端点成功标志
gpt-5.4OpenAI Chat CompletionsPOST /v1/chat/completions返回 tool_calls
claude-sonnet-4-6Anthropic MessagesPOST /v1/messages返回 tool_use block
gemini-3-pro-previewGemini NativePOST /v1beta/models/{model}:generateContent返回 functionCall

GPT-5.4

cURL
curl https://crazyrouter.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-5.4",
    "messages": [
      {
        "role": "user",
        "content": "Use the get_time tool for Asia/Shanghai. Do not answer without calling the tool."
      }
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_time",
          "description": "Get the current time for a timezone",
          "parameters": {
            "type": "object",
            "properties": {
              "timezone": {
                "type": "string"
              }
            },
            "required": ["timezone"],
            "additionalProperties": false
          }
        }
      }
    ],
    "tool_choice": "required"
  }'
生产验证返回的关键字段:
{
  "tool_calls": [
    {
      "type": "function",
      "function": {
        "name": "get_time",
        "arguments": "{\"timezone\":\"Asia/Shanghai\"}"
      }
    }
  ]
}

Claude Sonnet 4.6

cURL
curl https://crazyrouter.com/v1/messages \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-sonnet-4-6",
    "max_tokens": 256,
    "tools": [
      {
        "name": "get_time",
        "description": "Get the current time for a timezone",
        "input_schema": {
          "type": "object",
          "properties": {
            "timezone": {
              "type": "string"
            }
          },
          "required": ["timezone"]
        }
      }
    ],
    "tool_choice": {
      "type": "any"
    },
    "messages": [
      {
        "role": "user",
        "content": "Use the get_time tool for Asia/Shanghai. Do not answer directly."
      }
    ]
  }'
生产验证返回的关键字段:
{
  "stop_reason": "tool_use",
  "content": [
    {
      "type": "tool_use",
      "name": "get_time",
      "input": {
        "timezone": "Asia/Shanghai"
      }
    }
  ]
}
首轮联调建议:
  • 对 Claude 原生 /v1/messages,优先显式加上 tool_choice
  • 提示词里明确要求 “必须调用工具,不要直接回答”
  • 这样更容易稳定复现 tool_use

Gemini 3 Pro Preview

cURL
curl "https://crazyrouter.com/v1beta/models/gemini-3-pro-preview:generateContent?key=YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [
          {
            "text": "Use the get_time function for Asia/Shanghai."
          }
        ]
      }
    ],
    "tools": [
      {
        "functionDeclarations": [
          {
            "name": "get_time",
            "description": "Get the current time for a timezone",
            "parameters": {
              "type": "object",
              "properties": {
                "timezone": {
                  "type": "string"
                }
              },
              "required": ["timezone"]
            }
          }
        ]
      }
    ]
  }'
生产验证返回的关键字段:
{
  "candidates": [
    {
      "content": {
        "parts": [
          {
            "functionCall": {
              "name": "get_time",
              "args": {
                "timezone": "Asia/Shanghai"
              }
            }
          }
        ]
      }
    }
  ]
}

何时使用哪种协议

  • 如果你的项目已经在使用 OpenAI SDK,优先用 gpt-5.4 的 OpenAI 兼容方式
  • 如果你在做 Claude 原生接入,优先用 /v1/messages,并在首轮验证时加 tool_choice
  • 如果你在做 Gemini 原生能力接入,优先用 Gemini Native API

相关文档