工具与结构化输出
让模型调用你的函数,或者按你需要的 JSON 格式精确回答。
函数调用
在 tools 中描述你的函数。模型会在回答中给出它想发起的调用;你的代码执行这些调用,再把结果发回去。
const reply = await client.chat.completions.create({
model: "anthropic/claude-sonnet-5.5",
max_tokens: 1024,
messages: [{ role: "user", content: "What's the price of $NOVA?" }],
tools: [
{
type: "function",
function: {
name: "token_price",
description: "The latest price of a token on BNB Chain, in USD",
parameters: {
type: "object",
properties: { ticker: { type: "string" } },
required: ["ticker"],
},
},
},
],
});
const call = reply.choices[0].message.tool_calls?.[0];工具在 Responses 和 Messages 上的用法相同,只是采用各个格式自己的结构。在模型页面按工具调用筛选,就能看到哪些模型支持工具。
结构化输出
用 response_format 要求模型返回符合你的 schema 的 JSON:
{
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "trade_summary",
"strict": true,
"schema": {
"type": "object",
"properties": {
"trades": { "type": "integer" },
"net_bnb": { "type": "number" }
},
"required": ["trades", "net_bnb"],
"additionalProperties": false
}
}
}
}不提供的工具
所有 Agent 的调用都运行在同一个共享账户上,所以会在那里保存文件、记忆或沙盒的工具,以及在一次调用中运行其他模型的工具,都会被拒绝并返回 400 tool_not_served:
- 托管的代码执行、shell 和代码解释器
- 文件搜索、文件、记忆和文本编辑器工具
- 图片生成工具,以及调用其他模型的工具(要生成图片,请调用
POST /images) - X 搜索,它按帖子计费,请求无法为它设置上限
在你自己机器上由你的代码运行的工具始终可用。联网搜索和网页抓取是提供的:参见联网搜索。
限制工具循环
像联网搜索这样的托管工具会循环运行,预留会覆盖每一个可能的步骤。设置
max_tool_calls(或工具的
max_uses)可以让预留保持很小。没有步数限制的循环会被拒绝。