Skip to content

Fix/responses web search open page billing - #8510

Open
tutu702 wants to merge 2 commits into
QuantumNous:mainfrom
tutu702:fix/responses-web-search-open-page-billing
Open

tutu702 wants to merge 2 commits into
QuantumNous:mainfrom
tutu702:fix/responses-web-search-open-page-billing

Conversation

@tutu702

@tutu702 tutu702 commented Dec 1, 2026 •

Copy link
Copy Markdown

Agent: Claude Code 2.1.332

本 PR 由 AI 辅助完成。代码、测试和本说明由 Claude Code 生成,提交前由 fork 维护者人工复核。

Closes #8507

Request summary

报告者用官方镜像验证:普通 OpenAI Responses 渠道把 web_search_call 里的 open_page 也算作付费搜索。请求按上游回执只搜索了 1 次,消费日志和钱包却按 3 次工具调用结算。

Summary

Responses 处理路径现在只对 action.type 是 search 的 web_search_call 计一次搜索。open_page 和 find_in_page 读的是搜索已经返回的页面,OpenAI 不对它们收工具调用费,定价表里也没有对应条目。

 relay/channel/openai/relay_responses.go  OaiResponsesHandler
   for _, output := range responsesResponse.Output
-    switch output.Type
-    case dto.BuildInCallWebSearchCall:
-      info.CountBillableToolCall(dto.BuildInCallWebSearchCall, "")
+  for i := range responsesResponse.Output
+    info.CountBillableResponsesToolCalls(&responsesResponse.Output[i])
 service/responses_usage.go  ResponsesUsageAccumulator.Observe
   case dto.ResponsesOutputTypeItemDone
-    switch event.Item.Type
-    case dto.BuildInCallWebSearchCall, dto.BuildInCallFileSearchCall, dto.BuildInCallFunctionCall:
-      a.info.CountBillableToolCall(event.Item.Type, event.Item.Name)
+    switch event.Item.Type
+    case dto.ResponsesOutputTypeImageGenerationCall:
+      ...
+    default:
+      a.info.CountBillableResponsesToolCalls(event.Item)

新的计费入口 relay/common.CountBillableResponsesToolCalls 处理一个 Responses 输出项:只有 search 动作计一次搜索,open_page 和 find_in_page 不计。动作类型由 relaykit/dto.ResponsesOutput.WebSearchActionType() 读取,它把 NormalizeResponsesWebSearchAction 的 find 别名归一到 find_in_page。

两条边界保持原样:

  • 上游不返回 action 或返回未知动作类型时,仍然计费。上游不报动作的渠道维持原有口径。
  • info.WebSearchBillingKey 非空的渠道每一项都计。这些渠道的上游用自己的价格表区分搜索和页面抓取,计费归它们自己。

流式路径共用 ResponsesUsageAccumulator,WebSocket Responses 也走同一个累加器,所以同样受益。Azure、xAI、DashScope、DeepSeek 的回执覆盖逻辑不受影响,它们用自己的 hook 覆盖同一批计数。

Evidence

同一个响应的改动前后。 用 issue #8507 的原始响应体(1 个 search 动作、2 个 open_page 动作、回执 num_requests=1)跑非流式处理器:

$ go test ./relay/channel/openai/ -run TestEvidenceIssuePayload -v
    receipt num_requests=1, billed web_search calls=3     # 改动前
    receipt num_requests=1, billed web_search calls=1     # 改动后

流式路径。 1 个 search 动作加 1 个 open_page 动作:

$ go test ./relay/channel/openai/ -run TestEvidenceStreamPayload -v
    stream: one search action plus one open_page action, billed web_search calls=2   # 改动前
    stream: one search action plus one open_page action, billed web_search calls=1   # 改动后

issue #8507 里报告者在官方镜像 calciumion/new-api:v1.0.0-rc.52 上观察到同样的偏差:控制组(只有 search)扣 5100 quota,异常组(search 加两个 open_page)扣 15100 quota,other.tool_surcharges 记录 {"name":"web_search","count":3,"price":10}。本次改动后,异常组与控制组同样扣 5100 quota。

自动化测试,实际输出。

$ go test ./relay/common/ ./relay/channel/openai/ ./service/
ok  	github.com/QuantumNous/new-api/relay/common	0.028s
ok  	github.com/QuantumNous/new-api/relay/channel/openai	0.022s
ok  	github.com/QuantumNous/new-api/service	14.819s

$ go build ./...
(无输出)

$ cd relaykit && GOWORK=off go build ./... && GOWORK=off go test ./...
ok  	github.com/QuantumNous/new-api/relaykit/relayconvert/internal/oai_responses	0.026s
ok  	github.com/QuantumNous/new-api/relaykit/relayconvert/internal/toolconv	(cached)
(其余包同样通过)

新增用例:

  • relay/common/tool_usage_test.go 的 TestCountBillableResponsesToolCallsWebSearchAction:search 动作计一次,open_page 和 find_in_page 不计,缺失动作和未知动作仍然计费。
  • relay/common/tool_usage_test.go 的 TestCountBillableResponsesToolCallsWebSearchKeepsVendorSearchCounting:设置了 WebSearchBillingKey 的渠道每一项都计。
  • relay/channel/openai/relay_responses_billing_test.go 的 TestOaiResponsesHandlerCountsSearchActionsOnly 和 TestOaiResponsesStreamHandlerCountsSearchActionsOnly:非流式和流式处理器各一例。

上游语义依据。 OpenAI 的 Web search 指南写明 search 动作产生工具调用费,open_page 和 find_in_page 是推理模型的动作。定价表的 built-in tools 只有 "Web search $10.00 / 1k calls" 一行,没有页面打开的条目。OpenAI SDK 的 Response.tool_usage.web_search.num_requests 注释写明该计数排除 "actions such as opening a page"。

Merge Danger

Door: two-way。没有数据迁移,没有表结构改动。回滚这个提交即可恢复改动前的计数口径。

Blast Radius: billing。普通 OpenAI Responses 渠道中,带 open_page 动作的响应少收附加费,每个 open_page 少收 $10/1k。Token 费用不变,其余渠道不受影响。

Summary by CodeRabbit

  • Bug Fixes
    • Web-search billing now counts search actions without counting follow-up page-opening or in-page-search actions when those actions aren’t billed separately.
    • Streaming and non-streaming responses now apply the same web-search billing behavior.
    • Other billable tool calls, including file searches and function calls, continue to be counted.

tutu702 and others added 2 commits December 1, 2026 12:59
…items

The Responses handler counted every web_search_call output item as one
billable web search, so a response that searched once and opened two
pages billed three searches. OpenAI charges the search action only:
opening a page and searching inside a page read a page the search
already returned and carry no search charge, and the pricing table has
no line for them.
The file_search_call and function_call counts are already covered by
TestOaiResponsesHandlerCountsOutputCallsNotDeclarations.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The PR centralizes Responses tool-call billing and uses each output item's action to exclude open_page and find_in_page from unkeyed web-search charges. It applies the shared counter to response handling and usage accumulation, while retaining image-generation handling.

Changes

Responses tool billing

Layer / File(s) Summary
Action-aware billable call counting
relaykit/dto/openai_response.go, relay/common/tool_usage.go
WebSearchActionType extracts and normalizes the action type. CountBillableResponsesToolCalls counts web-search, file-search, and function-call items. For web searches without a billing key, open_page and find_in_page are not counted; other action values are counted.
Response and usage billing
relay/channel/openai/relay_responses.go, service/responses_usage.go
Response handling and usage accumulation use the shared counter. Image-generation items retain their existing conditional counting path.
Billing behavior tests
relay/common/tool_usage_test.go, relay/channel/openai/relay_responses_billing_test.go
Tests cover web-search action billing, channel-specific deepseek_search counting, and streaming and non-streaming Responses billing.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • Issue 6230: The PR changes Responses tool-call counting based on output items, which overlaps with the issue's tool-counting objective. It does not address the issue's xAI usage-detail or reasoning-token topics.

Suggested reviewers: calcium-ion, kosukework

Merge Risk: Minimal · up to 44a99

This change stops ordinary OpenAI Responses from billing page opens and in-page finds as searches, which fixes the overbilling reported in the linked issue. Keyed channels keep their current counting. The tests cover the changed behavior, and no merge-blocking risk is evident.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Fix/responses web search open page billing" matches the main change. The PR stops billing web-search page actions (open_page and find_in_page) in ordinary OpenAI Responses, so only search a…
Linked Issues check Passed [#8507] The issue requires one search charge for a Responses reply with one search action and two open_page actions, with page opens adding no search fee. At the reviewed head, `CountBillableRespo…
Out of Scope Changes check Passed The whole-PR diff against the merge base changes relay/common/tool_usage.go to add CountBillableResponsesToolCalls and billableWebSearchAction. It adds tests for these functions and for the Open…
Full details: Title check

Explanation

The title "Fix/responses web search open page billing" matches the main change. The PR stops billing web-search page actions (open_page and find_in_page) in ordinary OpenAI Responses, so only search actions are billed. The title is short and specific, but it uses a branch-style "Fix/" prefix and a slash-separated form that reads like a branch name rather than a sentence.

Full details: Linked Issues check

Explanation

[#8507] The issue requires one search charge for a Responses reply with one search action and two open_page actions, with page opens adding no search fee. At the reviewed head, CountBillableResponsesToolCalls in relay/common/tool_usage.go counts a web_search_call only when billableWebSearchAction returns true. That function returns false for open_page and find_in_page. The non-streaming handler (relay_responses.go) and the shared ResponsesUsageAccumulator (service/responses_usage.go) both route items through this function. This yields one charge for the reported case. Missing or unknown actions remain billable. Channels with a non-empty WebSearchBillingKey keep counting every item. The PR adds unit tests for these cases and for the non-streaming and streaming handlers. I did not run the tests. The PR description reports their passing output. The issue asks for no coding work beyond this billing path, so no requirement is unmet. Streaming and WebSocket coverage goes beyond the issue's stated verification, but it applies the same counting rule and does not conflict with the issue.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff against the merge base changes relay/common/tool_usage.go to add CountBillableResponsesToolCalls and billableWebSearchAction. It adds tests for these functions and for the OpenAI Responses handlers. It also adds WebSearchActionType in relaykit/dto and updates the call sites in relay_responses.go and service/responses_usage.go. All these changes implement the web-search action billing rule from #8507. The non-web-search item types (file_search_call, function_call) keep their previous counting through the same function. The streaming default branch reaches only those three types, so its counted output is unchanged for them. I did not inspect the vendor hook files. The PR description claims Azure, xAI, DashScope, and DeepSeek overrides are unaffected. This claim is not verified, but it does not indicate an unrelated change. No unrelated change was found.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks each search in flight,
Page opens leave the count just right,
One search hops into the ledger,
Streams and replies count together,
The carrot tally shines tonight.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b8583312-bae8-4bf0-bab3-15d7761baf88
Commits

Reviewing files that changed from the base of the PR and between 04dfcbb and 44a995b.

Files selected for processing: 6

  • relay/channel/openai/relay_responses.go
  • relay/channel/openai/relay_responses_billing_test.go
  • relay/common/tool_usage.go
  • relay/common/tool_usage_test.go
  • relaykit/dto/openai_response.go
  • service/responses_usage.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 普通 OpenAI Responses 将 open_page 算作付费搜索,回执 1 次实际扣 3 次

1 participant