
# Function calling / tool 调用如何计入 OpenAI 账单
OpenAI API 使用 Function calling(也称 tool 调用)时,模型会根据提示和工具定义决定调用哪些外部函数。账单主要按模型标准定价(输入/输出 Token $/M)收取,但自定义工具定义会额外占用输入 Token,内置工具(如 Web search、File search)有单独固定单价。
此指南针对开发者和对账人员,帮助你快速决策是否使用、是否会超支,以及如何用官方工具核对账单。
核心概念与术语
- Function calling:模型接收用户提示 + 你预定义的工具列表(JSON Schema 格式),自主决定调用哪些工具并返回调用参数。
- Tool:你实现的外部函数(get_weather、send_email 等),模型调用后返回结果。
- Tool call:模型返回的调用记录(包含 name、arguments),这是账单关键。
- Input tokens:Prompt + 工具定义 + 搜索内容等全部计入。
- Output tokens:最终返回内容 + 工具调用结果。
- Prompt 缓存:重复上下文可节省成本(缓存输入价格远低于普通输入)。
- Responses API / Chat Completions API:主流方式,官方定价以模型为准。
决策表
| 场景 | 计入方式 | 是否额外固定费用 | 典型账单构成 | 适用人群 |
|---|---|---|---|---|
| 普通对话,无工具 | 纯输入 + 输出 Token | 无 | Model input $/M + output $/M | 所有用户 |
| 自定义 Function calling | 工具定义 Token + 调用输出 Token | 无 | 工具定义输入 Token + 结果输出 Token | 需要调用外部 API 的应用 |
| 内置工具(Web search) | 工具调用固定 + 搜索内容 Token | 是 ($10/1k calls 或 $2.50/1k calls) | 工具调用费 + model input $/M | 需要实时搜索或文件检索的场景 |
| 多轮 Agent(循环调用工具) | 每轮工具定义 + 结果 Token + 调用费 | 是(内置工具) | 工具调用费 + 所有 Token | 复杂 Agent 系统 |
| 工具数量大(>20 个) | 工具定义 Token + 可能超 context 限 | 无(但易超支) | 工具定义输入 Token | 工具库庞大的项目 |
数据以 OpenAI 官方定价为准(https://platform.openai.com/docs/pricing),实际以当日 Dashboard 为准。
实操清单:分步可核对
1. 准备工具定义
使用 JSON Schema 定义 name、description、parameters。示例:
{
"type": "function",
"name": "get_weather",
"description": "...",
"parameters": { ... },
"strict": true
}
2. 发送请求
在 Chat Completions 或 Responses API 中包含 tools 参数。
收到响应后检查 response.output 是否有 type: "function_call"。
3. 执行工具并返回结果
调用你的函数,构造 function_call_output,再发起下一轮请求(多轮必须循环处理)。
4. 查账单
- 登录 OpenAI Dashboard 查看 Usage 页。
- 过滤模型名称 + “tool calls” 或 “web search tool calls”。
- 查看 input_tokens_details 和 output_tokens 明细。
5. 核对 Token 计数
使用官方 Token counting API 预估:
- 输入含 tools 时会自动计入。
- 可在 Playground 测试不同工具组合。
6. 优化
- 用 Tool search 延迟加载工具(gpt-5.4+ 支持)。
- 缩短工具描述,限前 20 个工具。
- 开启 Prompt 缓存(cache_rate > 70% 时可省 90%+)。
内链参考:查看官方 API 文档 和 价格页 确认最新单价。
常见坑与风险边界
- 工具定义 Token 被重复计入:每轮请求都携带工具定义,多轮 Agent 会快速累积输入 Token。
- 内置工具调用费:Web search 工具每次调用额外 $10/1k calls(搜索内容 Token 按模型价),File search 仅 Responses API 生效 $2.50/1k calls。
- Agent 循环风险:一个复杂任务可能产生 10+ 轮调用,Token 开支远超普通对话。
- Token 限额误判:工具定义也会占用 context 窗口,超限时返回 error。
- 缓存优化忽略:不开启缓存时,重复工具描述会浪费大量输入 Token。
注意:以上为通用知识,非法律意见。请以 OpenAI 官方 Dashboard 和定价页实时数据为准,账单以实际调用记录为准。
站内路径
- 官方 API 价格:查看 GPT Token 单价和缓存价格
- 官方 API 文档:Function calling 完整指南和代码示例
- API 使用路径:迁移和集成建议
- 账单核对指南:如何在 Dashboard 精确对账
- 计费优化指南:Prompt 缓存与 Token 节省技巧
风险与边界
Function calling 和工具调用可能导致账单超出预期,尤其是多轮 Agent 系统。以下为边界条件:
- 工具定义 Token 计入输入但不产生单独固定费用。
- 内置工具(Web search)有额外固定调用费,搜索内容 Token 按模型价。
- 超过模型 context 窗口或 Rate Limit 时可能出现 error,影响账单稳定性。
- 实时 API 或 Batch API 计费规则不同(以官方最新版为准)。
- OpenAI 可能调整定价,建议定期查阅 https://platform.openai.com/docs/pricing。
此内容仅供参考,非投资或法律建议。实际账单以 OpenAI 官方系统为准。
English summary
Function calling (tool calling) in the OpenAI API is counted primarily through standard token billing, but with important nuances. Tool definitions are injected into the prompt and charged as input tokens, while the actual tool call results (after you execute them) are returned as output tokens in the next request. Built-in tools like web search incur a separate fixed fee per call ($10 per 1k calls) plus the model’s input token rate for any retrieved content.
In multi-turn agent workflows, each loop adds tokens from tool definitions, results, and calls, so costs can grow quickly if not optimized. Use the official token counting API to pre-estimate usage and enable Prompt caching for repeated contexts to reduce input costs by up to 90%. Built-in tools (File search, Web search) have dedicated pricing tiers that appear separately in the billing dashboard.
Always verify with your specific model (e.g., gpt-5.x series) and check the dashboard line items for “tool calls” or “web search tool calls”. For heavy agent usage, monitor cache hit rates and limit upfront tool definitions to stay under context limits. Pricing and features are subject to change—review the official dashboard and pricing page for the latest details before production deployment.
This setup gives developers full flexibility to connect models to external systems while keeping billing transparent and controllable.