Skip to content

ClaudeAdapter token usage: residual fallback to accumulated totals when no stream events arrive #127

Description

@harrryyd

Problem

PR #126 fixes the main token usage over-reporting bug by capturing per-request usage from message_start/message_delta stream events. However, completeTurn still falls back to accumulated totals from result.usage when lastKnownTokenUsage is undefined — which happens when no stream events arrive (e.g., non-streaming mode, or API calls that dont stream).

// ClaudeAdapter.ts completeTurn (approx line 1506-1533)
const accumulatedSnapshot = normalizeClaudeTokenUsage(
  result?.usage,
  resultContextWindow ?? context.lastKnownContextWindow,
);
// Falls back to context.lastKnownTokenUsage, 
// which could be accumulatedSnapshot if no stream events

Acceptance criteria

  • Determine when stream events might be absent (non-streaming mode? edge cases?)
  • Either handle the fallback accurately or document that it is acceptable
  • Consider whether result.usage can be used differently in non-streaming scenarios

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions