fix: correct cache_hit_rate calculation and fix Vercel stream tool call handling (#10994)

* fix: correct cache_hit_rate calculation and remove duplicate emission

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>

* fix: emit tool call id/name from tool-input-start in Vercel stream converter

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>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Continue <noreply@continue.dev>
This commit is contained in:
Nate Sesti 2026-03-04 07:57:41 -08:00 • committed by GitHub
parent 56798ad241
commit ec7030d20b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 49 additions and 53 deletions

View file

@ -383,31 +383,18 @@ export function recordStreamTelemetry(options: {
cacheWriteTokens,
});
// Emit prompt_cache_metrics for the Prompt Cache Performance dashboard
if (actualInputTokens > 0) {
// Emit prompt_cache_metrics for the Prompt Cache Performance dashboard.
// total_prompt_tokens must include ALL input token types: non-cached input
// tokens (prompt_tokens) + cache reads + cache writes. Anthropic's
// `input_tokens` field only counts non-cached tokens, so using it alone as
// the denominator produces ratios >> 1 when caching works well.
if (fullUsage?.prompt_tokens_details) {
const totalPromptTokens =
(fullUsage.prompt_tokens ?? 0) + cacheReadTokens + cacheWriteTokens;
const cacheHitRate =
totalPromptTokens > 0 ? cacheReadTokens / totalPromptTokens : 0;
posthogService.capture("prompt_cache_metrics", {
model: model.model,
cache_read_tokens: cacheReadTokens,
cache_write_tokens: cacheWriteTokens,
total_prompt_tokens: actualInputTokens,
cache_hit_rate: cacheReadTokens / actualInputTokens,
tool_count: tools?.length ?? 0,
});
}
} catch {}
// Report prompt cache metrics to PostHog
if (fullUsage?.prompt_tokens_details) {
const cacheReadTokens =
fullUsage.prompt_tokens_details.cache_read_tokens ?? 0;
const cacheWriteTokens =
fullUsage.prompt_tokens_details.cache_write_tokens ?? 0;
const totalPromptTokens = fullUsage.prompt_tokens ?? 0;
const cacheHitRate =
totalPromptTokens > 0 ? cacheReadTokens / totalPromptTokens : 0;
try {
void posthogService.capture("prompt_cache_metrics", {
model: model.model,
cache_read_tokens: cacheReadTokens,
cache_write_tokens: cacheWriteTokens,
@ -415,8 +402,8 @@ export function recordStreamTelemetry(options: {
cache_hit_rate: cacheHitRate,
tool_count: tools?.length ?? 0,
});
} catch {}
}
}
} catch {}
return cost;
}

View file

@ -37,7 +37,7 @@ describe("convertVercelStreamPart", () => {
expect(result?.choices[0].delta.content).toBe("Let me think...");
});
test("converts tool-call to chat chunk", () => {
test("returns null for tool-call (handled by tool-input-start/delta)", () => {
const part: VercelStreamPart = {
type: "tool-call",
toolCallId: "call_abc123",
@ -47,17 +47,7 @@ describe("convertVercelStreamPart", () => {
const result = convertVercelStreamPart(part, options);
expect(result).not.toBeNull();
expect(result?.choices[0].delta.tool_calls).toHaveLength(1);
expect(result?.choices[0].delta.tool_calls?.[0]).toEqual({
index: 0,
id: "call_abc123",
type: "function",
function: {
name: "readFile",
arguments: JSON.stringify({ filepath: "/path/to/file" }),
},
});
expect(result).toBeNull();
});
test("converts tool-input-delta to chat chunk", () => {
@ -170,7 +160,7 @@ describe("convertVercelStreamPart", () => {
expect(result).toBeNull();
});
test("returns null for tool-input-start", () => {
test("converts tool-input-start to initial tool call chunk with id and name", () => {
const part: VercelStreamPart = {
type: "tool-input-start",
id: "call_abc123",
@ -179,7 +169,17 @@ describe("convertVercelStreamPart", () => {
const result = convertVercelStreamPart(part, options);
expect(result).toBeNull();
expect(result).not.toBeNull();
expect(result?.choices[0].delta.tool_calls).toHaveLength(1);
expect(result?.choices[0].delta.tool_calls?.[0]).toEqual({
index: 0,
id: "call_abc123",
type: "function",
function: {
name: "readFile",
arguments: "",
},
});
});
});
@ -191,6 +191,9 @@ describe("convertVercelStream", () => {
{ type: "start-step" },
{ type: "text-delta", id: "text-1", text: "Hello " },
{ type: "text-delta", id: "text-1", text: "world" },
{ type: "tool-input-start", id: "call_1", toolName: "test" },
{ type: "tool-input-delta", id: "call_1", delta: '{"arg":"value"}' },
{ type: "tool-input-end", id: "call_1" },
{
type: "tool-call",
toolCallId: "call_1",
@ -221,16 +224,20 @@ describe("convertVercelStream", () => {
chunks.push(chunk);
}
// Should only get chunks for: text-delta (2), tool-call (1), finish (1) = 4 chunks
// start-step and finish-step are filtered out
expect(chunks).toHaveLength(4);
// Should get chunks for: text-delta (2), tool-input-start (1), tool-input-delta (1), finish (1) = 5
// start-step, tool-input-end, tool-call, and finish-step are filtered out
expect(chunks).toHaveLength(5);
expect(chunks[0].choices[0].delta.content).toBe("Hello ");
expect(chunks[1].choices[0].delta.content).toBe("world");
expect(chunks[2].choices[0].delta.tool_calls?.[0].id).toBe("call_1");
expect(chunks[2].choices[0].delta.tool_calls?.[0].function?.name).toBe(
"test",
);
expect(chunks[3].usage).toBeDefined();
expect(chunks[3].choices[0].delta.tool_calls?.[0].function?.arguments).toBe(
'{"arg":"value"}',
);
expect(chunks[4].usage).toBeDefined();
});
test("throws error when stream contains error event", async () => {
@ -262,11 +269,6 @@ describe("convertVercelStream", () => {
{ type: "start-step" },
{ type: "source", source: {} },
{ type: "file", file: { name: "test.txt", content: "content" } },
{
type: "tool-input-start",
id: "call_1",
toolName: "test",
},
{ type: "tool-result", toolCallId: "call_1", result: {} },
];

View file

@ -91,17 +91,19 @@ export function convertVercelStreamPart(
model,
});
case "tool-call":
case "tool-input-start":
// Emit the initial chunk with id and function name, matching OpenAI's
// streaming format where the first tool call chunk carries the id/name.
return chatChunkFromDelta({
delta: {
tool_calls: [
{
index: 0,
id: part.toolCallId,
id: part.id,
type: "function" as const,
function: {
name: part.toolName,
arguments: JSON.stringify(part.input),
arguments: "",
},
},
],
@ -124,6 +126,12 @@ export function convertVercelStreamPart(
model,
});
case "tool-call":
// tool-call is emitted after tool-input-start/delta/end have already
// streamed the complete tool call. Emitting it again would duplicate
// the arguments. Skip it since streaming events already handled it.
return null;
case "finish":
if (part.totalUsage) {
const inputTokens =
@ -178,7 +186,6 @@ export function convertVercelStreamPart(
case "reasoning-end":
case "source":
case "file":
case "tool-input-start":
case "tool-input-end":
case "tool-result":
case "start-step":