Repository navigation
fix(llm-analytics): bill Gemini URL context tool-use tokens as input - #262
Merged
turnipdabeets merged 2 commits intoNov 19, 2026
Merged
turnipdabeets merged 2 commits into
turnipdabeets merged 2 commits into
Conversation
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.
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
approved these changes
Nov 19, 2026
turnipdabeets
left a comment
Contributor
There was a problem hiding this comment.
Thanks @pcbeingused333 !
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
In the Req LLM analytics plugin, a Gemini
generateContentresponse'susageMetadata.toolUsePromptTokenCountwas added to$ai_output_tokens. That count is the content retrieved by built-in tools, reported separately frompromptTokenCount. 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 carriesurlContextMetadata. 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.exssend the sameusageMetadata(prompt 12, candidates 25, tool use 78), once withurlContextMetadata(expects input 90, output 25) and once withgroundingMetadata(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
If releasing new changes
sampo addto generate a changeset fileAgent 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.