Skip to content

fix(ai): preserve Anthropic usage metadata - #38751

Merged
rekram1-node merged 2 commits into
v2from
anthropic-usage
Jul 24, 2026
Merged

rekram1-node merged 2 commits into
v2from
anthropic-usage

Conversation

@rekram1-node

Copy link
Copy Markdown
Collaborator

Problem

Anthropic usage events were decoded with a closed Effect Schema.Struct, so every provider field outside four token counters was silently stripped before usage mapping. This dropped current fields such as server-tool usage and output_tokens_details.thinking_tokens, and made the documented raw provider-metadata escape hatch lossy for future Anthropic additions.

Solution

  • Decode Anthropic usage with Schema.StructWithRest plus Record<string, unknown> so unknown top-level fields survive untyped.
  • Apply the same open shape to known nested server_tool_use and output_tokens_details objects so future nested counters also survive.
  • Keep known fields typed:
    • server_tool_use.web_search_requests
    • output_tokens_details.thinking_tokens
  • Map thinking_tokens to canonical Usage.reasoningTokens while keeping outputTokens inclusive.
  • Preserve reasoning tokens and raw provider fields when merging usage split across message_start and message_delta.

Tests

The Anthropic stream fixture verifies:

  • canonical input, output, total, and reasoning token counts;
  • known server-tool usage;
  • unknown top-level usage fields;
  • unknown nested server-tool and output-detail fields;
  • merging raw fields from initial and terminal usage events.

Verification

  • bun test from packages/ai: 452 passed, 28 skipped
  • focused Anthropic suite: 44 passed
  • bun typecheck from packages/ai: clean
  • git diff --check: clean

@rekram1-node
rekram1-node merged commit c7d7f61 into v2 Jul 24, 2026
7 checks passed
@rekram1-node
rekram1-node deleted the anthropic-usage branch July 24, 2026 21:23
jinhuang712 pushed a commit to jinhuang712/opencode that referenced this pull request Sep 26, 2026
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.

1 participant