Skip to content

LangChain callback handler double-counts Gemini reasoning tokens in output cost #1298

Done

Description

@MaiAditya

Problem

With langchain-google-genai (2.1.7 and later, including 4.5.2), usage_metadata.output_tokens already includes the thinking tokens (candidates_token_count + thoughts_token_count), and output_token_details.reasoning repeats them. CallbackHandler._parse_usage_model forwards both unchanged, as $ai_output_tokens and $ai_reasoning_tokens.

Cost ingestion (mustAddReasoningCost in nodejs/src/ingestion/pipelines/ai/costs/output-costs.ts) adds $ai_reasoning_tokens on top of $ai_output_tokens for gemini-2.5-* and gemini-3* models, because the Gemini wrapper reports output as candidates only. Thinking tokens from LangChain Gemini events are therefore priced twice.

Example (from the langchain-google-genai docstring): 49 candidate tokens + 777 thinking tokens, output_tokens: 826. PostHog prices 1603 output tokens instead of 826.

Thinking is on by default for Gemini 2.5 / 3, so this affects every such LangChain or LangGraph call.

Expected

$ai_output_tokens for these events matches what the Gemini wrapper sends (candidates only), with $ai_reasoning_tokens still reported.

I have a fix with tests ready and will open a PR.

Activity

  1. added
    bug
    on Nov 24, 2026
  2. posthog commented on Nov 24, 2026

    @posthog
    Contributor

    PostHog self-driving picked up this issue. Status: Research is done.

    Somebody may already be working on it, so ask before you start a fix.

    Report (team access only): https://us.posthog.com/project/2/inbox/reports/01a20c86-e1dd-77ad-810b-e9b5c950398a?link_source=github_comment

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugteam/llm-analytics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions