anomalyco / anomalyco/opencode

V2: align Anthropic streaming usage aggregation with upstream SDKs

Open
#40,713 1 comment 0 reactions 1 assignee View on GitHub

@rekram1-node is already working on this.

Since Aug 5, 2026.

2.0
Dominant language
TypeScript
Stars
209k
Forks
27.5k
PR merge metrics
PR metrics pending

Description

Problem

The native Anthropic Messages parser currently normalizes usage independently on message_start and message_delta, then merges normalized Usage values. Anthropic usage is split across those events, and newer responses can include usage.iterations for compaction, message, advisor, and fallback inference.

This makes correct aggregation harder than it needs to be and risks losing or undercounting partial usage details. Vercel AI SDK instead accumulates raw Anthropic usage across stream events and normalizes once when message_stop arrives.

Follow-up scope

  • Accumulate typed raw Anthropic usage in parser state across message_start and message_delta.
  • Normalize once at message_stop rather than merging partially normalized Usage objects.
  • Match Anthropic and AI SDK nullability for streamed usage fields.
  • Define and test usage.iterations accounting:
    • include executor compaction and message work
    • keep advisor_message separately attributable
    • avoid double-counting fallback responses
    • handle cache-read and cache-creation counters consistently
  • Preserve the complete provider payload in providerMetadata.anthropic.
  • Keep normalized Usage invariants intact when only partial counters are available.

Acceptance criteria

  • Fixtures cover split start/delta usage, compaction, advisor, and fallback cases.
  • Normalized totals match the current Anthropic/AI SDK semantics.
  • No provider metadata has to be reparsed to reconstruct normalized usage.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.