fix: correct cache_hit_rate calculation and fix Vercel stream tool call handling#10994
Open
fix: correct cache_hit_rate calculation and fix Vercel stream tool call handling#10994
Conversation
Two bugs in prompt_cache_metrics telemetry: 1. Duplicate emission: prompt_cache_metrics was emitted twice per API request — once using `actualInputTokens` and again using `fullUsage.prompt_tokens`. This doubled all event counts in PostHog and produced conflicting values. 2. Wrong denominator: cache_hit_rate was calculated as `cacheReadTokens / prompt_tokens`, but the Anthropic adapter maps `prompt_tokens` to only non-cached input tokens (`input_tokens`), excluding cache reads and writes. When caching works well, this produces ratios >> 1 (observed max: 89,892). The correct total is `prompt_tokens + cache_read_tokens + cache_write_tokens`. Fix: remove the first duplicate emission and compute total_prompt_tokens as the sum of all three token types. cache_hit_rate is now a proper 0-1 ratio. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
|
Docs Review: No documentation updates needed. This PR contains internal telemetry fixes (correcting |
…nverter The Vercel AI SDK streams tool calls as tool-input-start → tool-input-delta → tool-input-end → tool-call. Previously, tool-input-start was ignored (returned null) and tool-call emitted the full tool call at the end, which meant streaming consumers never saw the tool call id on intermediate chunks. Now tool-input-start emits the initial chunk with id and function name (matching OpenAI's streaming format), and tool-call returns null to avoid duplicating args already streamed via tool-input-delta. Generated with [Continue](https://continue.dev) Co-Authored-By: Continue <noreply@continue.dev>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix cache_hit_rate telemetry: The
prompt_cache_metricsevent was emitted twice per completion, and the cache hit rate denominator used onlyprompt_tokens(which maps to Anthropic'sinput_tokens— non-cached only). This caused ratios >> 1 when caching worked well (max observed: 89,892). Fixed by removing the duplicate emission and using the correct total:prompt_tokens + cache_read_tokens + cache_write_tokens.Fix Vercel AI SDK tool call streaming: The Vercel AI SDK streams tool calls as
tool-input-start→tool-input-delta→tool-input-end→tool-call. Previouslytool-input-startwas ignored andtool-callemitted the full call at the end, so streaming consumers never saw the tool callidon intermediate chunks. Nowtool-input-startemits the initial chunk withidand function name (matching OpenAI's streaming format), andtool-callis a no-op to avoid duplicating args.Test plan
vercelStreamConverter.test.ts(15 tests)@ai-sdk/xaidep, env-only issue)