Skip to content

fix(llm-analytics): bill Gemini URL context tool-use tokens as input - #262

Merged
turnipdabeets merged 2 commits into
PostHog:mainfrom
pcbeingused333:fix/gemini-tool-use-prompt-tokens
Nov 19, 2026
Merged

turnipdabeets merged 2 commits into
PostHog:mainfrom
pcbeingused333:fix/gemini-tool-use-prompt-tokens

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Motivation and Context

In the Req LLM analytics plugin, a Gemini generateContent response's usageMetadata.toolUsePromptTokenCount was added to $ai_output_tokens. That count is the content retrieved by built-in tools, reported separately from promptTokenCount. URL context retrieval is billed as input, and Google Search retrieval is not billed as input, so the old total over-counted output for both.

Same fix as posthog-python#1164: the count is now added to $ai_input_tokens, and only when a candidate carries urlContextMetadata. Otherwise (for example Google Search) it stays out of the totals. A response that used both tools cannot be split from these counts. Includes a changeset.

How did you test it?

Two new tests in test/posthog/integrations/llm_analytics/req_test.exs send the same usageMetadata (prompt 12, candidates 25, tool use 78), once with urlContextMetadata (expects input 90, output 25) and once with groundingMetadata (expects input 12, output 25). Both fail before the change (output was 103) and the 14 tests in the file pass after it (mix test test/posthog/integrations/llm_analytics/req_test.exs, mix format --check-formatted).

Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

Agent context

Autonomy: Human-driven (agent-assisted)

Prepared with Claude Code (Claude). The tests above were run locally with mocked responses, with no manual testing against the live Gemini API.

toolUsePromptTokenCount was added to output tokens. URL context retrieval is billed as input, and Google Search retrieval is not billed, so add it to input only when a candidate carries urlContextMetadata.
@pcbeingused333
pcbeingused333 requested a review from a team as a code owner November 19, 2026 20:32
Move gemini_url_context?/1 below the last response_properties/1 clause so
the clauses stay together and Elixir doesn't warn about ungrouped clauses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@turnipdabeets turnipdabeets left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pcbeingused333 !

@turnipdabeets
turnipdabeets merged commit c6d3504 into PostHog:main Nov 19, 2026
29 checks passed
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.

2 participants