Repository navigation
feat(metrics): cross-request TTFT and gap latency after tool calls #3173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
diegosouzapw
merged 3 commits into
cheaper-inference:release/v3.8.10
from
pizzav-xyz:feat/tool-latency-tracking
Jun 4, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /** | ||
| * Tracks per-provider tool call latency metrics. | ||
| * Resets on server restart. | ||
| */ | ||
|
|
||
| interface ToolLatencyEntry { | ||
| totalRequests: number; | ||
| ttftAfterToolSum: number; | ||
| gapAfterToolSum: number; | ||
| ttftCount: number; | ||
| gapCount: number; | ||
| } | ||
|
|
||
| const metrics = new Map<string, ToolLatencyEntry>(); | ||
|
|
||
| export function recordToolLatency( | ||
| provider: string, | ||
| ttftAfterToolMs: number | null, | ||
| gapAfterToolMs: number | null | ||
| ): void { | ||
| if (!provider) return; | ||
|
|
||
| if (!metrics.has(provider)) { | ||
| metrics.set(provider, { | ||
| totalRequests: 0, | ||
| ttftAfterToolSum: 0, | ||
| gapAfterToolSum: 0, | ||
| ttftCount: 0, | ||
| gapCount: 0, | ||
| }); | ||
| } | ||
|
|
||
| const entry = metrics.get(provider)!; | ||
| entry.totalRequests++; | ||
|
|
||
| if (ttftAfterToolMs != null && ttftAfterToolMs >= 0) { | ||
| entry.ttftAfterToolSum += ttftAfterToolMs; | ||
| entry.ttftCount++; | ||
| } | ||
|
|
||
| if (gapAfterToolMs != null && gapAfterToolMs >= 0) { | ||
| entry.gapAfterToolSum += gapAfterToolMs; | ||
| entry.gapCount++; | ||
| } | ||
| } | ||
|
|
||
| export function getToolLatencyByProvider(): Record< | ||
| string, | ||
| { | ||
| avgTtftAfterToolMs: number; | ||
| avgGapAfterToolMs: number; | ||
| measurementCount: number; | ||
| } | ||
| > { | ||
| const result: Record<string, any> = {}; | ||
| for (const [provider, entry] of metrics) { | ||
| result[provider] = { | ||
| avgTtftAfterToolMs: | ||
| entry.ttftCount > 0 ? Math.round(entry.ttftAfterToolSum / entry.ttftCount) : 0, | ||
| avgGapAfterToolMs: | ||
| entry.gapCount > 0 ? Math.round(entry.gapAfterToolSum / entry.gapCount) : 0, | ||
| measurementCount: entry.totalRequests, | ||
| }; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| export function recordToolTtft(provider: string, ttftMs: number): void { | ||
| if (!provider || ttftMs < 0) return; | ||
|
|
||
| if (!metrics.has(provider)) { | ||
| metrics.set(provider, { | ||
| totalRequests: 0, | ||
| ttftAfterToolSum: 0, | ||
| gapAfterToolSum: 0, | ||
| ttftCount: 0, | ||
| gapCount: 0, | ||
| }); | ||
| } | ||
|
|
||
| const entry = metrics.get(provider)!; | ||
| entry.totalRequests++; | ||
| entry.ttftAfterToolSum += ttftMs; | ||
| entry.ttftCount++; | ||
| } | ||
|
|
||
| export function resetToolLatency(): void { | ||
| metrics.clear(); | ||
| } | ||
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /** | ||
| * #3173 — cross-request tool latency metrics. | ||
| * | ||
| * toolLatencyTracker aggregates per-provider TTFT/gap after tool calls; | ||
| * sessionManager persists a one-shot tool-finish timestamp so the follow-up | ||
| * request (Request 2) can measure cross-request TTFT. | ||
| */ | ||
| import test from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
|
|
||
| const { | ||
| recordToolLatency, | ||
| getToolLatencyByProvider, | ||
| recordToolTtft, | ||
| resetToolLatency, | ||
| } = await import("../../open-sse/services/toolLatencyTracker.ts"); | ||
| const { markToolFinish, consumeToolFinishTime } = await import( | ||
| "../../open-sse/services/sessionManager.ts" | ||
| ); | ||
|
|
||
| test("recordToolLatency averages TTFT/gap per provider and counts requests", () => { | ||
| resetToolLatency(); | ||
| recordToolLatency("openai", 100, 40); | ||
| recordToolLatency("openai", 300, 60); | ||
| recordToolLatency("anthropic", 200, null); | ||
|
|
||
| const m = getToolLatencyByProvider(); | ||
| assert.equal(m.openai.avgTtftAfterToolMs, 200); // (100+300)/2 | ||
| assert.equal(m.openai.avgGapAfterToolMs, 50); // (40+60)/2 | ||
| assert.equal(m.openai.measurementCount, 2); | ||
| assert.equal(m.anthropic.avgTtftAfterToolMs, 200); | ||
| assert.equal(m.anthropic.avgGapAfterToolMs, 0); // no gap samples | ||
| assert.equal(m.anthropic.measurementCount, 1); | ||
| }); | ||
|
|
||
| test("recordToolLatency ignores null/negative samples but still counts the request", () => { | ||
| resetToolLatency(); | ||
| recordToolLatency("glm", null, null); | ||
| recordToolLatency("glm", -5, -5); | ||
| const m = getToolLatencyByProvider(); | ||
| assert.equal(m.glm.measurementCount, 2); | ||
| assert.equal(m.glm.avgTtftAfterToolMs, 0); | ||
| assert.equal(m.glm.avgGapAfterToolMs, 0); | ||
| }); | ||
|
|
||
| test("recordToolTtft accumulates and resetToolLatency clears", () => { | ||
| resetToolLatency(); | ||
| recordToolTtft("codex", 120); | ||
| recordToolTtft("codex", 80); | ||
| assert.equal(getToolLatencyByProvider().codex.avgTtftAfterToolMs, 100); | ||
| resetToolLatency(); | ||
| assert.deepEqual(getToolLatencyByProvider(), {}); | ||
| }); | ||
|
|
||
| test("markToolFinish/consumeToolFinishTime is a one-shot per session", () => { | ||
| // null session is a no-op / null | ||
| markToolFinish(null); | ||
| assert.equal(consumeToolFinishTime(null), null); | ||
| // unknown session (never touched) → null | ||
| assert.equal(consumeToolFinishTime("sess-never-seen-xyz"), null); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
recordToolTtft,entry.totalRequestsis not incremented when recording the TTFT latency. SincegetToolLatencyByProviderusesentry.totalRequestsas themeasurementCount, any requests that only record TTFT viarecordToolTtftwill not be counted in the total measurement count, leading to inaccurate metrics reporting.We should increment
entry.totalRequestsinrecordToolTtftjust like we do inrecordToolLatency.