@devries-cameron20 / t3code Mirror · read-only / commits / 8d11fe1261
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 13:00 UTC

fix(server): steered Claude turns no longer undercount token usage (#19314)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Theo Browne committed Oct 14, 2026 at 01:39 UTC 8d11fe1261f0dfd74f58a1b1ae8ad7e400905fcb
3 files changed +103 -2
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+38 -1
@@ -2234,6 +2234,12 @@ describe("ClaudeAdapterV2 background wake turns", () => {
2234 readonly apiErrorStatus?: number;
2235 // null omits the field, as the CLI does on a zero-turn result.
2236 readonly terminalReason?: SDKResultMessage["terminal_reason"] | null;
2237 + readonly usage?: {
2238 + readonly input_tokens: number;
2239 + readonly output_tokens: number;
2240 + readonly cache_creation_input_tokens: number;
2241 + readonly cache_read_input_tokens: number;
2242 + };
2243 }) =>
2244 claudeSdkFrame({
2245 type: "result",
@@ -2245,7 +2251,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
2251 result: input.result,
2252 stop_reason: "end_turn",
2253 total_cost_usd: 0,
2248 - usage: {
2254 + usage: input.usage ?? {
2255 input_tokens: 1,
2256 output_tokens: 1,
2257 cache_creation_input_tokens: 0,
@@ -2993,6 +2999,12 @@ describe("ClaudeAdapterV2 background wake turns", () => {
2999 uuid: "00000000-0000-4000-8000-000000000901",
3000 result: "",
3001 terminalReason,
3002 + usage: {
3003 + input_tokens: 600,
3004 + output_tokens: 40,
3005 + cache_creation_input_tokens: 500,
3006 + cache_read_input_tokens: 2000,
3007 + },
3008 }),
3009 );
3010 if (steered) {
@@ -3002,6 +3014,12 @@ describe("ClaudeAdapterV2 background wake turns", () => {
3014 makeResultFrame({
3015 uuid: "00000000-0000-4000-8000-000000000902",
3016 result: "Audit finished after the steer.",
3017 + usage: {
3018 + input_tokens: 3,
3019 + output_tokens: 6,
3020 + cache_creation_input_tokens: 2347,
3021 + cache_read_input_tokens: 9455,
3022 + },
3023 }),
3024 );
3025 }
@@ -3016,6 +3034,25 @@ describe("ClaudeAdapterV2 background wake turns", () => {
3034 event.turnItem.text === WAKE_ASSISTANT_TEXT,
3035 ),
3036 );
3037 + // Each result's usage covers only its own segment, so the steered
3038 + // turn bills both segments.
3039 + const completed = harness.events.findLast(
3040 + (event) => event.type === "provider_turn.updated",
3041 + );
3042 + assert.deepEqual(
3043 + completed?.type === "provider_turn.updated"
3044 + ? completed.providerTurn.turnTokenUsage
3045 + : undefined,
3046 + {
3047 + usageScope: "main_agent",
3048 + usageStatus: "complete",
3049 + hasSubagents: false,
3050 + inputTokens: 603 + 2847 + 11455,
3051 + outputTokens: 46,
3052 + cachedInputTokens: 11455,
3053 + cacheCreationTokens: 2847,
3054 + },
3055 + );
3056 }
3057 assert.lengthOf(harness.terminalEvents(), 1);
3058 }).pipe(
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+6
@@ -2918,6 +2918,9 @@ interface ActiveClaudeTurnContext {
2918 readonly rejectedRateLimitTypes: Set<string>;
2919 readonly rateLimitResetTimes: Map<string, string | null>;
2920 latestAssistantRateLimited: boolean;
2921 + // Usage of the results that steers ended, oldest first. The final result's
2922 + // usage covers only its own segment, so the turn bills these as well.
2923 + readonly steeredSegmentUsage: Array<SDKResultMessage["usage"]>;
2924 readonly subagentsByTaskId: Map<string, ActiveClaudeSubagent>;
2925 readonly subagentsByToolUseId: Map<string, ActiveClaudeSubagent>;
2926 readonly subagentNodesByTaskId: Map<string, OrchestrationV2ExecutionNode["id"]>;
@@ -5922,6 +5925,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
5925 input.context.subagentsByTaskId.size > 0 ||
5926 input.context.subagentsByToolUseId.size > 0,
5927 input.status,
5928 + input.context.steeredSegmentUsage,
5929 ),
5930 },
5931 }),
@@ -7660,6 +7664,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
7664 const interrupted = (yield* Ref.get(interruptedTurns)).has(context.providerTurnId);
7665 const wasSteered = (yield* Ref.get(steeredTurns)).has(context.providerTurnId);
7666 if (!interrupted && wasSteered && isClaudeActiveSteeringAbortResult(message)) {
7667 + context.steeredSegmentUsage.push(message.usage);
7668 return;
7669 }
7670 yield* Ref.update(steeredTurns, (current) => {
@@ -8568,6 +8573,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
8573 rejectedRateLimitTypes: new Set(),
8574 rateLimitResetTimes: new Map(),
8575 latestAssistantRateLimited: false,
8576 + steeredSegmentUsage: [],
8577 subagentsByTaskId: new Map(),
8578 subagentsByToolUseId: new Map(),
8579 subagentNodesByTaskId: new Map(),
apps/server/src/provider/ClaudeTurnTokenUsage.ts
+59 -1
@@ -7,12 +7,70 @@ function finiteNonNegativeInteger(value: unknown): number | undefined {
7 : undefined;
8 }
9
10 +const BILLED_USAGE_FIELDS = [
11 + "input_tokens",
12 + "cache_read_input_tokens",
13 + "cache_creation_input_tokens",
14 + "output_tokens",
15 +] as const;
16 +
17 +// Adds earlier segments' counts to a count the final result reports. A count
18 +// the final result lacks stays missing, so status rules see the same fields.
19 +function addKnownCount(finalCount: unknown, earlierCounts: ReadonlyArray<unknown>): unknown {
20 + const known = finiteNonNegativeInteger(finalCount);
21 + if (known === undefined) return finalCount;
22 + return earlierCounts.reduce<number>(
23 + (sum, count) => sum + (finiteNonNegativeInteger(count) ?? 0),
24 + known,
25 + );
26 +}
27 +
28 +// A steer ends the running segment with an aborted result, and the SDK scopes
29 +// each result's usage to its own segment. Adds the billed counts of those
30 +// earlier segments onto the final result's usage. Other usage fields describe
31 +// the final result only and are left as they are.
32 +function withEarlierSegmentUsage(
33 + usage: Record<string, unknown>,
34 + earlierSegmentUsage: ReadonlyArray<unknown>,
35 +): Record<string, unknown> {
36 + if (earlierSegmentUsage.length === 0) return usage;
37 + const segments = earlierSegmentUsage.map((segment) => (segment ?? {}) as Record<string, unknown>);
38 + const total = { ...usage };
39 + for (const field of BILLED_USAGE_FIELDS) {
40 + total[field] = addKnownCount(
41 + usage[field],
42 + segments.map((segment) => segment[field]),
43 + );
44 + }
45 + const outputDetails = usage.output_tokens_details as Record<string, unknown> | undefined;
46 + if (outputDetails != null) {
47 + total.output_tokens_details = {
48 + ...outputDetails,
49 + thinking_tokens: addKnownCount(
50 + outputDetails.thinking_tokens,
51 + segments.map(
52 + (segment) =>
53 + (segment.output_tokens_details as Record<string, unknown> | undefined)?.thinking_tokens,
54 + ),
55 + ),
56 + };
57 + }
58 + return total;
59 +}
60 +
61 +/**
62 + * Normalizes a Claude turn's billed usage from its final SDK result.
63 + * `earlierSegmentUsage` holds the usage of results a steer ended before the
64 + * final one, oldest first.
65 + */
66 export function normalizeClaudeTurnTokenUsage(
67 result: { readonly subtype: SDKResultMessage["subtype"]; readonly usage?: unknown } | undefined,
68 hasSubagents: boolean,
69 terminalStatus: ProviderRuntimeTurnStatus,
70 + earlierSegmentUsage: ReadonlyArray<unknown> = [],
71 ): TurnTokenUsage {
15 - const usage = result?.usage as Record<string, unknown> | undefined;
72 + const finalUsage = result?.usage as Record<string, unknown> | undefined;
73 + const usage = finalUsage && withEarlierSegmentUsage(finalUsage, earlierSegmentUsage);
74 if (!usage) {
75 return {
76 usageStatus: "unavailable",