Skip to content

fix(zen): emit cost chunk in client-facing format, not upstream format#16817

Open
kitlangton wants to merge 2 commits intoanomalyco:devfrom
kitlangton:fix/zen-cost-chunk-format
Open

fix(zen): emit cost chunk in client-facing format, not upstream format#16817
kitlangton wants to merge 2 commits intoanomalyco:devfrom
kitlangton:fix/zen-cost-chunk-format

Conversation

@kitlangton
Copy link
Contributor

Summary

  • The cost chunk appended at the end of streaming SSE responses was always emitted in the upstream provider's format (e.g. Anthropic event: ping), regardless of what format the client requested
  • When a client connects via OpenAI-compatible format but the upstream is Anthropic, the Anthropic-format ping fails SDK schema validation → AI_TypeValidationError
  • Moved buildCostChunk out of per-provider usage parsers into a standalone function keyed on the output format (opts.format), so the cost event always matches what the client expects

Details

The handler already tracks both formats — providerInfo.format (upstream) and opts.format (client-facing) — and converts all stream chunks between them. The cost chunk was the only one that bypassed this conversion because buidlCostChunk lived on the upstream provider's usage parser.

Also fixes the buidlCostChunkbuildCostChunk typo.

Test plan

  • Stream via Zen with Anthropic upstream + OpenAI-compatible client format — cost chunk should now be valid OAI-compat JSON
  • Stream via Zen with matching formats (e.g. Anthropic→Anthropic) — no change in behavior
  • Verify cost data still arrives in the Zen web UI

The cost chunk appended at the end of streaming responses was always
emitted in the upstream provider's SSE format (e.g. Anthropic `event: ping`)
regardless of what format the client requested. When a client connects via
OpenAI-compatible format but the upstream is Anthropic, the Anthropic-format
ping fails SDK schema validation → AI_TypeValidationError.

Move `buildCostChunk` out of per-provider usage parsers into a standalone
function keyed on the output format (`opts.format`), so the cost event
always matches what the client expects.

Also fixes the `buidlCostChunk` → `buildCostChunk` typo.
@kitlangton kitlangton marked this pull request as ready for review March 10, 2026 00:50
@kitlangton kitlangton requested a review from fwang March 10, 2026 00:50
- `is_tream` → `is_stream`
- `calculateOccuredCost` → `calculateOccurredCost`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant