billedToolCalls and groundingMetadata missing from generateContent response, but AI Studio logs show billed searches (gemini-3.5-flash)

When gemini-3.5-flash runs Google Search grounding during thinking, the generateContent response body contains no usageMetadata.billedToolCalls and no groundingMetadata — yet the AI Studio log for the same responseId shows billedToolCalls with a non-zero successfulToolCallCount, and the model’s output text cites vertexaisearch.cloud.google.com/grounding-api-redirect/... URLs (which only exist when grounding ran). This makes it impossible to track billed search costs per request from the API response.

Setup

  • Model: gemini-3.5-flash
  • Endpoint: generativelanguage.googleapis.com v1beta generateContent (non-streaming), called via the Vercel AI SDK (@ai-sdk/google)
  • Tools: google_search enabled
  • temperature: 0, default thinking (response shows thoughtsTokenCount: 2001)
  • Request date: 2026-07-08 ~16:53 ()
  • responseId: BhBOasX7GfyPjuMP0KyN8A4

What the API returned (raw response body)

"usageMetadata": {
  "promptTokenCount": 1964,
  "candidatesTokenCount": 797,
  "totalTokenCount": 4762,
  "promptTokensDetails": [{ "modality": "TEXT", "tokenCount": 1964 }],
  "thoughtsTokenCount": 2001,
  "serviceTier": "standard"
}
  • No billedToolCalls field.
  • candidates[0] has no groundingMetadata block at all (verified on the raw HTTP body, not an SDK-parsed copy — the body still carries fields like serviceTier and thoughtSignature).
  • The candidate’s text does contain multiple https://vertexaisearch.cloud.google.com/grounding-api-redirect/... source URLs, so search grounding clearly executed.

What AI Studio logs show for the same responseId

The log entry for BhBOasX7GfyPjuMP0KyN8A4 in the AI Studio logs dashboard includes:

"usageMetadata": {
    "promptTokenCount": 1964,
    "candidatesTokenCount": 797,
    "totalTokenCount": 4762,
    "promptTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 1964
      }
    ],
    "thoughtsTokenCount": 2001,
    "billedToolCalls": [
      {
        "tool": "GOOGLE_SEARCH_RETRIEVAL",
        "successfulToolCallCount": 1
      }
    ]
  }

For reference, other calls of ours show entries like:

"billedToolCalls": [
  { "tool": "GOOGLE_SEARCH_RETRIEVAL", "successfulToolCallCount": 5 }
]

Questions

  1. Is omitting billedToolCalls from the generateContent response body intended when searches run during thinking? If so, is there any way to get the billed search count programmatically per request (short of migrating to the Interactions API for grounding_tool_count)?
  2. Why is groundingMetadata (including webSearchQueries) null/absent on this response even though the output cites grounding-redirect URLs? That removes the only documented fallback for counting searches.
  3. If this is a known gap, is exposing billedToolCalls in the response (matching what AI Studio logs already record server-side) on the roadmap? We bill customers per request and currently have to record these searches as 0.

Hi Celine,

Thanks for this.

We’ve had an internal discussion and it looks like the AI Studio logs are revealing a field which may be removed in the future. I wouldn’t rely on this.

We did try reproducing this through our own Python uSDK & the groundingMetadata.webSearchQueries field was returned as expected. Wondering if it’s possible for you to try switching to that & check if you see it then?

Thanks for looking into this!

On the SDK suggestion: I don’t think this is SDK-dependent — I verified against the raw HTTP response body (bypassing any SDK), and both usageMetadata.billedToolCalls and groundingMetadata are absent from the wire response itself (repro responseId: BhBOasX7GfyPjuMP0KyN8A4). Happy to re-test with the Python google-genai SDK, but since the fields aren’t in the response body, I’d expect the same result.

On reproducing: the key condition is that the search runs during the thinking phase, not during the final answer. When grounding executes in the normal answer phase, groundingMetadata (including webSearchQueries) comes back as expected — I see that too. The gap only appears when the model searches while thinking: the output text still cites vertexaisearch.cloud.google.com grounding-redirect URLs (proving search ran), and AI Studio logs show the billed calls, but the response body has no groundingMetadata at all. Could you retry with thinking enabled and a prompt that triggers search mid-thinking?

The underlying question: we need a supported way to count billed searches per request for cost tracking. Given that:

  • billedToolCalls is internal and may be removed (per your reply),
  • groundingMetadata is absent when search runs during thinking,
  • the AI Studio dataset export doesn’t include usageMetadata, so there’s no reconciliation path,

What is the recommended surface for this? I did find that the Interactions API returns usage.grounding_tool_count (e.g. [{type: "google_search", count: N}]) correctly even when searches interleave with thinking. Is that the intended/supported path going forward, or is there a plan to expose an equivalent field on generateContentAPI?

My suspicion is that the interactions API is the way forward for this, but I will double check & get back to you.