Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(pi): turn token usage counts cached tokens as input and stays per turn #40762 (GitHub #40758)

Mergedjuliusmarmingemerged 3 commits intomain fromrefs/pull/40758/headmerged by juliusmarminge View on GitHub

Showing 3 changed files with 90 additions and 21 deletions (loading 1).

x
0 of 3 viewed, 3 left
34 additions and 21 deletionsapps/server/src/orchestration-v2/testkit/fixtures/simple/pi_output.tsModified
View file
@@ -24,32 +24,45 @@
return field(frame, "data");
​
-/** Settled Pi usage uses each recorded get_session_stats response in order, not the last streaming total. */
+/**
+ * Settled Pi usage takes the context size from each turn's get_session_stats response and the
+ * input breakdown from the turn's last streamed message usage, since stats tokens are session totals.
+ */
export function assertPiSettledTokenUsage(
result: OrchestratorV2ScenarioResult,
transcript: ProviderReplayTranscript,
) {
- const stats = transcript.entries.flatMap((entry) =>
- entry.type === "emit_inbound" &&
- field(entry.frame, "type") === "response" &&
- field(entry.frame, "command") === "get_session_stats"
- ? [field(entry.frame, "data")]
- : [],
- );
- const turns = projectionFor(result, transcript.scenario).providerTurns;
- assert.lengthOf(turns, stats.length);
- for (const [index, turn] of turns.entries()) {
- const contextUsage = field(stats[index], "contextUsage");
- const tokens = field(stats[index], "tokens");
- const { updatedAt: _updatedAt, ...tokenUsage } = turn.tokenUsage ?? { updatedAt: "" };
- assert.deepEqual(tokenUsage, {
- usedTokens: field(contextUsage, "tokens"),
- maxTokens: field(contextUsage, "contextWindow"),
- inputTokens: field(tokens, "input"),