mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
fix(grok): bill search calls from xAI usage success counts
When a Grok response usage carries server_side_tool_usage_details, take SearchCount from web_search_calls, x_search_calls and document_search_calls instead of counting output items. xAI documents server_side_tool_usage as the successful calls that determine billing, so failed calls and server-side tools that return nothing are no longer billed. Classes the item counter does not count and the x_posts_fetched / x_users_fetched item counts are not read. Without server_side_tool_usage_details the existing item counting is unchanged. The usage is read from the JSON body, the SSE body and the terminal stream event, for /v1/responses, /v1/messages and /v1/chat/completions, streaming and not. ResponsesUsage keeps the raw object so the buffered paths, which count from a decoded response, can read it.
This commit is contained in:
1 parent
ef29203002
commit
2c587f78b0
6 files changed
+199
No files matched your search
@@ -510,6 +510,10 @@ type ResponsesUsage struct {
|
||||
// Optional detailed breakdown
|
||||
InputTokensDetails *ResponsesInputTokensDetails `json:"input_tokens_details,omitempty"`
|
||||
OutputTokensDetails *ResponsesOutputTokensDetails `json:"output_tokens_details,omitempty"`
|
||||
|
||||
// ServerSideToolUsageDetails is xAI's per-class success count object,
|
||||
// kept raw so Grok search counting can read it from a decoded response.
|
||||
ServerSideToolUsageDetails json.RawMessage `json:"server_side_tool_usage_details,omitempty"`
|
||||
}
|
||||
|
||||
func (u *ResponsesUsage) UnmarshalJSON(data []byte) error {
|
||||
|
||||
@@ -13,10 +13,18 @@ import (
|
||||
// Counts: web_search_call, x_search_call, tool_search_call, and function_call
|
||||
// named tool_search / web_search / x_search, and custom_tool_call items the xAI
|
||||
// server runs itself (apicompat.IsServerSideSearchCall: call_id xs_ / as_).
|
||||
//
|
||||
// When usage.server_side_tool_usage_details is present, xAI's reported success
|
||||
// counts replace the item count (see grokSearchCountFromUsage).
|
||||
func countGrokNativeSearchCallsFromJSONBytes(body []byte) int {
|
||||
if len(body) == 0 || !gjson.ValidBytes(body) {
|
||||
return 0
|
||||
}
|
||||
for _, path := range []string{"response.usage", "usage"} {
|
||||
if n, ok := grokSearchCountFromUsage(gjson.GetBytes(body, path)); ok {
|
||||
return n
|
||||
}
|
||||
}
|
||||
// Responses envelopes normally expose either top-level output (JSON mode)
|
||||
// or response.output (terminal SSE payload). Compatibility layers can retain
|
||||
// both copies; counting both would bill the same search twice. Prefer the
|
||||
@@ -35,10 +43,47 @@ func countGrokNativeSearchCallsFromSSEBody(body string) int {
|
||||
total := 0
|
||||
forEachOpenAISSEDataPayload(body, func(data []byte) {
|
||||
total += countGrokNativeSearchCallsInSSEDataDedup(data, seen)
|
||||
total = grokSearchCountWithTerminalUsage(total, data)
|
||||
})
|
||||
return total
|
||||
}
|
||||
|
||||
// grokSearchCountFromUsage reads xAI's per-class success counts from a usage
|
||||
// object. xAI documents these counts as what determines billing for per-call
|
||||
// tools (https://docs.x.ai/developers/tools/tool-usage-details#server-side-tool-calls-vs-tool-usage).
|
||||
// Only the classes the item counter also counts are read: web_search_calls
|
||||
// (web_search_call items), x_search_calls (xs_ custom_tool_call items) and
|
||||
// document_search_calls (as_ custom_tool_call items). Other classes and the
|
||||
// X Search item counts (x_posts_fetched, x_users_fetched) are not read. ok is
|
||||
// false when server_side_tool_usage_details is absent, so callers keep the
|
||||
// item count.
|
||||
func grokSearchCountFromUsage(usage gjson.Result) (int, bool) {
|
||||
details := usage.Get("server_side_tool_usage_details")
|
||||
if !details.IsObject() {
|
||||
return 0, false
|
||||
}
|
||||
total := details.Get("web_search_calls").Int() +
|
||||
details.Get("x_search_calls").Int() +
|
||||
details.Get("document_search_calls").Int()
|
||||
if total < 0 {
|
||||
total = 0
|
||||
}
|
||||
return int(total), true
|
||||
}
|
||||
|
||||
// grokSearchCountWithTerminalUsage returns xAI's usage-reported count when data
|
||||
// is a terminal stream event carrying server_side_tool_usage_details (the
|
||||
// request total per the xAI X Search docs), else keeps the running total.
|
||||
func grokSearchCountWithTerminalUsage(total int, data []byte) int {
|
||||
switch strings.TrimSpace(gjson.GetBytes(data, "type").String()) {
|
||||
case "response.completed", "response.incomplete", "response.done":
|
||||
if n, ok := grokSearchCountFromUsage(gjson.GetBytes(data, "response.usage")); ok {
|
||||
return n
|
||||
}
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
// countGrokNativeSearchCallsInSSEData counts search tool calls in one SSE
|
||||
// payload without cross-event dedup. Prefer countGrokNativeSearchCallsInSSEDataDedup
|
||||
// for live streams so item.done + response.completed do not double-bill.
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
//go:build unit
|
||||
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// Usage fixtures follow the public xAI Responses captures: x_search_calls and the
|
||||
// x_posts_fetched / x_users_fetched item counts as in big-AGI grok-4.3_x-search, and the
|
||||
// web_search_calls + x_search_calls + code_interpreter_calls mix as in grok-4.3_web+x-search+code.
|
||||
// document_search_calls is listed in the xAI ModelOutput schema (server_side_tool_usage_details).
|
||||
const (
|
||||
grokUsageXSearchDetails = `{"x_search_calls":3,"x_posts_fetched":13,"x_users_fetched":3}`
|
||||
grokUsageWebXCodeDetails = `{"web_search_calls":2,"x_search_calls":1,"code_interpreter_calls":1,"x_posts_fetched":6}`
|
||||
grokUsageDocumentDetails = `{"document_search_calls":5}`
|
||||
grokUsageUnrelatedDetails = `{"code_interpreter_calls":1,"file_search_calls":1,"mcp_calls":1,"x_posts_fetched":7,"x_users_fetched":2}`
|
||||
grokFailedXSearchItemJSON = `{"type":"custom_tool_call","id":"ctc_4","call_id":"xs_999","name":"x_thread_fetch","input":"{}","status":"failed"}`
|
||||
)
|
||||
|
||||
func grokUsageJSON(details string) string {
|
||||
usage := `{"input_tokens":5,"output_tokens":2,"total_tokens":7`
|
||||
if details != "" {
|
||||
usage += `,"server_side_tool_usage_details":` + details
|
||||
}
|
||||
return usage + "}"
|
||||
}
|
||||
|
||||
type grokUsageCase struct {
|
||||
name string
|
||||
items []string
|
||||
details string
|
||||
want int
|
||||
}
|
||||
|
||||
func grokUsageCases() []grokUsageCase {
|
||||
return []grokUsageCase{
|
||||
{"x_search_calls only, posts and users fetched not read", []string{grokXSearchItemJSON}, grokUsageXSearchDetails, 3},
|
||||
{"web_search_calls, x_search_calls summed, code_interpreter_calls not read", []string{grokXSearchItemJSON}, grokUsageWebXCodeDetails, 3},
|
||||
{"document_search_calls", []string{grokXSearchItemJSON}, grokUsageDocumentDetails, 5},
|
||||
{"all three fields", []string{grokXSearchItemJSON}, `{"web_search_calls":1,"x_search_calls":2,"document_search_calls":3}`, 6},
|
||||
{"only unrelated classes: usage wins with zero", []string{grokXSearchItemJSON, grokASearchItemJSON}, grokUsageUnrelatedDetails, 0},
|
||||
{"usage absent: items counted", []string{grokXSearchItemJSON, grokASearchItemJSON, grokClientToolJSON}, "", 2},
|
||||
{"usage lower than items: failed call not billed", []string{grokXSearchItemJSON, grokFailedXSearchItemJSON}, `{"x_search_calls":1}`, 1},
|
||||
{"usage zero, items present", []string{grokXSearchItemJSON}, `{"x_search_calls":0}`, 0},
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrokSearchCountFromUsageJSON(t *testing.T) {
|
||||
for _, tc := range grokUsageCases() {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
output := "[" + strings.Join(tc.items, ",") + "]"
|
||||
top := `{"output":` + output + `,"usage":` + grokUsageJSON(tc.details) + "}"
|
||||
nested := `{"response":{"output":` + output + `,"usage":` + grokUsageJSON(tc.details) + "}}"
|
||||
require.Equal(t, tc.want, countGrokNativeSearchCallsFromJSONBytes([]byte(top)))
|
||||
require.Equal(t, tc.want, countGrokNativeSearchCallsFromJSONBytes([]byte(nested)))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrokSearchCountFromUsageSSEBody(t *testing.T) {
|
||||
for _, tc := range grokUsageCases() {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
sse := withGrokUsage(grokCustomToolCallStream(tc.items, "[]"), tc.details)
|
||||
require.Equal(t, tc.want, countGrokNativeSearchCallsFromSSEBody(sse))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGrokSearchCountWithTerminalUsage(t *testing.T) {
|
||||
usage := `"usage":` + grokUsageJSON(`{"x_search_calls":4}`)
|
||||
for _, eventType := range []string{"response.completed", "response.incomplete", "response.done"} {
|
||||
data := []byte(fmt.Sprintf(`{"type":%q,"response":{"output":[],%s}}`, eventType, usage))
|
||||
require.Equal(t, 4, grokSearchCountWithTerminalUsage(1, data), eventType)
|
||||
}
|
||||
for _, eventType := range []string{"response.created", "response.in_progress", "response.output_item.done"} {
|
||||
data := []byte(fmt.Sprintf(`{"type":%q,"response":{%s}}`, eventType, usage))
|
||||
require.Equal(t, 1, grokSearchCountWithTerminalUsage(1, data), eventType)
|
||||
}
|
||||
require.Equal(t, 1, grokSearchCountWithTerminalUsage(1, []byte(`{"type":"response.completed","response":{"usage":{"input_tokens":1}}}`)))
|
||||
}
|
||||
|
||||
func withGrokUsage(sse, details string) string {
|
||||
return strings.Replace(sse, `"usage":{"input_tokens":5,"output_tokens":2,"total_tokens":7}`, `"usage":`+grokUsageJSON(details), 1)
|
||||
}
|
||||
|
||||
func TestGrokSearchCountFromUsageAcrossEndpoints(t *testing.T) {
|
||||
account := healthyGrokOAuthGatewayTestAccount(4243, "access-token")
|
||||
for _, tc := range grokUsageCases() {
|
||||
sse := withGrokUsage(grokCustomToolCallStream(tc.items, "[]"), tc.details)
|
||||
for _, path := range []string{"/v1/messages", "/v1/chat/completions"} {
|
||||
for _, streaming := range []bool{false, true} {
|
||||
t.Run(fmt.Sprintf("%s %s stream=%v", tc.name, path, streaming), func(t *testing.T) {
|
||||
got := forwardGrokCustomToolCallStream(t, path, streaming, account, sse)
|
||||
require.Equal(t, tc.want, got.searchCount)
|
||||
})
|
||||
}
|
||||
}
|
||||
t.Run(fmt.Sprintf("%s /v1/responses stream", tc.name), func(t *testing.T) {
|
||||
require.Equal(t, tc.want, forwardGrokResponsesSearchCount(t, true, sse))
|
||||
})
|
||||
t.Run(fmt.Sprintf("%s /v1/responses json", tc.name), func(t *testing.T) {
|
||||
body := `{"id":"resp_1","object":"response","model":"grok-4.7","status":"completed","output":[` +
|
||||
strings.Join(tc.items, ",") + `],"usage":` + grokUsageJSON(tc.details) + "}"
|
||||
require.Equal(t, tc.want, forwardGrokResponsesSearchCount(t, false, body))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func forwardGrokResponsesSearchCount(t *testing.T, streaming bool, upstreamBody string) int {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
body := []byte(fmt.Sprintf(`{"model":"grok","input":"search","stream":%t}`, streaming))
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = httptest.NewRequest(http.MethodPost, "/v1/responses", bytes.NewReader(body))
|
||||
account := healthyGrokOAuthGatewayTestAccount(9903, "access-token")
|
||||
repo := &mockAccountRepoForPlatform{accountsByID: map[int64]*Account{account.ID: account}}
|
||||
contentType := "application/json"
|
||||
if streaming {
|
||||
contentType = "text/event-stream"
|
||||
}
|
||||
upstream := &httpUpstreamRecorder{resp: &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Header: http.Header{"Content-Type": []string{contentType}},
|
||||
Body: io.NopCloser(strings.NewReader(upstreamBody)),
|
||||
}}
|
||||
svc := &OpenAIGatewayService{
|
||||
httpUpstream: upstream,
|
||||
grokTokenProvider: NewGrokTokenProvider(repo, nil),
|
||||
accountRepo: repo,
|
||||
}
|
||||
result, err := svc.forwardGrokResponses(context.Background(), c, account, body, "grok", streaming, time.Now())
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
return result.SearchCount
|
||||
}
|
||||
@@ -909,6 +909,7 @@ func (s *OpenAIGatewayService) handleChatStreamingResponse(
|
||||
}
|
||||
if countSearch {
|
||||
searchCount += countGrokNativeSearchCallsInSSEDataDedup([]byte(payload), streamSearchSeen)
|
||||
searchCount = grokSearchCountWithTerminalUsage(searchCount, []byte(payload))
|
||||
}
|
||||
|
||||
var event apicompat.ResponsesStreamEvent
|
||||
|
||||
@@ -1117,6 +1117,7 @@ func (s *OpenAIGatewayService) handleAnthropicStreamingResponse(
|
||||
}
|
||||
if countSearch {
|
||||
searchCount += countGrokNativeSearchCallsInSSEDataDedup([]byte(payload), streamSearchSeen)
|
||||
searchCount = grokSearchCountWithTerminalUsage(searchCount, []byte(payload))
|
||||
}
|
||||
|
||||
var event apicompat.ResponsesStreamEvent
|
||||
|
||||
@@ -660,6 +660,7 @@ func (s *OpenAIGatewayService) handleStreamingResponseWithReasoning(ctx context.
|
||||
}
|
||||
imageCounter.AddSSEData(dataBytes)
|
||||
searchCounter += countGrokNativeSearchCallsInSSEDataDedup(dataBytes, streamSearchSeen)
|
||||
searchCounter = grokSearchCountWithTerminalUsage(searchCounter, dataBytes)
|
||||
|
||||
// Correct Codex tool calls if needed (apply_patch -> edit, etc.)
|
||||
if correctedData, corrected := s.toolCorrector.CorrectToolCallsInSSEBytes(dataBytes); corrected {
|
||||
|
||||
Reference in new issue
Block a user