Skip to content

[Bug]: Pi adapter mixes mutually exclusive message cache fields and session totals in live tokenUsage #40747

Closed

Description

@iiijiashu

Summary

The Pi provider adapter emits tokenUsage updates using mutually exclusive message-level fields (input vs cacheRead) alongside get_session_stats session-cumulative totals (totals.input / totals.cacheRead), which diverges from the normalized OrchestrationV2ProviderTurnTokenUsage contract where cachedInputTokens is a subset of inputTokens.

Code Analysis (verified on origin/main cd3a57ac72)

  1. packages/provider-pi/src/server/usage.ts:75-80 explicitly documents Pi's raw counters:
    // Pi's input/cache counters are already disjoint.
    uncachedInputTokens: tokenCount(usage.input),
    cachedInputTokens: tokenCount(usage.cacheRead),
    cacheCreationTokens: tokenCount(usage.cacheWrite),
  2. packages/provider-pi/src/server/adapter.ts:1043-1060 (reportLiveUsage, streaming updates):
    • Reads inputTokens = nonNegativeInteger(usage, "input") and cachedInputTokens = nonNegativeInteger(usage, "cacheRead") directly without summing input + cacheRead (+ cacheWrite).
    • Consequently cachedInputTokens can exceed inputTokens (e.g. input = 100, cacheRead = 2000), so any consumer computing cachedInputTokens / inputTokens gets > 100%.
  3. packages/provider-pi/src/server/adapter.ts:1006-1024 (tokenUsageFromStats, turn completion):
    • Overwrites the per-message/per-turn fields with session-wide totals.input and totals.cacheRead from get_session_stats, mixing message-level live snapshots with session-level cumulative totals.

Expected Behavior

Normalize Pi's OrchestrationV2ProviderTurnTokenUsage consistently with Claude/Codex/OpenCode/Antigravity (total inputTokens including cached/cache-creation tokens when computing subset ratios, and avoiding mixing disjoint message-level live counts with session-wide totals).

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions