Skip to content

Commit 23e23f1

Browse files
committed
fix(server): count Codex usage after resume and report Claude reasoning tokens
Codex only counted a turn's usage when a prior cumulative total existed, so the first turn after resume or rollback reported unavailable and any counter reset marked the turn partial. Real logs show Codex sends the newest response usage as `last` alongside `total`, so use `last` as the delta whenever there is no prior total or the total shrank. Claude results carry `output_tokens_details.thinking_tokens`. Report it as reasoningTokens so Claude and Codex turns compare on the same fields. Also lists Antigravity with the providers whose usage stays unavailable.
1 parent c8cf4b3 commit 23e23f1

5 files changed

Lines changed: 186 additions & 80 deletions

File tree

‎apps/server/src/provider/Layers/ClaudeAdapter.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2553,6 +2553,7 @@ describe("ClaudeAdapterLive", () => {
25532553
cache_read_input_tokens: 90,
25542554
cache_creation_input_tokens: 10,
25552555
output_tokens: 50,
2556+
output_tokens_details: { thinking_tokens: 30 },
25562557
},
25572558
modelUsage: {
25582559
[SYNTHETIC_CLAUDE_CAPABLE_MODEL]: {
@@ -2585,6 +2586,7 @@ describe("ClaudeAdapterLive", () => {
25852586
inputTokens: 500,
25862587
cachedInputTokens: 90,
25872588
cacheCreationTokens: 10,
2589+
reasoningTokens: 30,
25882590
outputTokens: 50,
25892591
hasSubagents: false,
25902592
});

‎apps/server/src/provider/Layers/ClaudeAdapter.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,8 @@ function normalizeClaudeTurnTokenUsage(
664664
const cachedInputTokens = finiteNonNegativeInteger(usage.cache_read_input_tokens);
665665
const cacheCreationTokens = finiteNonNegativeInteger(usage.cache_creation_input_tokens);
666666
const rawOutputTokens = finiteNonNegativeInteger(usage.output_tokens);
667+
const outputDetails = usage.output_tokens_details as Record<string, unknown> | undefined;
668+
const thinkingTokens = finiteNonNegativeInteger(outputDetails?.thinking_tokens);
667669
const cachedInputContribution = usage.cache_read_input_tokens == null ? 0 : cachedInputTokens;
668670
const cacheCreationContribution =
669671
usage.cache_creation_input_tokens == null ? 0 : cacheCreationTokens;
@@ -697,6 +699,9 @@ function normalizeClaudeTurnTokenUsage(
697699
usageScope: "main_agent",
698700
...(cachedInputTokens !== undefined ? { cachedInputTokens } : {}),
699701
...(cacheCreationTokens !== undefined ? { cacheCreationTokens } : {}),
702+
...(thinkingTokens !== undefined && rawOutputTokens !== undefined
703+
? { reasoningTokens: Math.min(rawOutputTokens, thinkingTokens) }
704+
: {}),
700705
hasSubagents,
701706
} as const;
702707
if (

‎apps/server/src/provider/Layers/CodexAdapter.test.ts‎

Lines changed: 105 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,16 @@ function codexTokenUsageEvent(input: {
605605
readonly cacheCreationTokens: number;
606606
readonly outputTokens: number;
607607
readonly reasoningTokens: number;
608+
readonly last?: {
609+
readonly inputTokens: number;
610+
readonly cachedInputTokens: number;
611+
readonly cacheCreationTokens: number;
612+
readonly outputTokens: number;
613+
readonly reasoningTokens: number;
614+
};
608615
}): ProviderEvent {
609616
const totalTokens = input.inputTokens + input.outputTokens;
617+
const last = input.last ?? input;
610618
return {
611619
id: asEventId(input.id),
612620
kind: "notification",
@@ -628,12 +636,12 @@ function codexTokenUsageEvent(input: {
628636
totalTokens,
629637
},
630638
last: {
631-
inputTokens: input.inputTokens,
632-
cachedInputTokens: input.cachedInputTokens,
633-
cacheWriteInputTokens: input.cacheCreationTokens,
634-
outputTokens: input.outputTokens,
635-
reasoningOutputTokens: input.reasoningTokens,
636-
totalTokens,
639+
inputTokens: last.inputTokens,
640+
cachedInputTokens: last.cachedInputTokens,
641+
cacheWriteInputTokens: last.cacheCreationTokens,
642+
outputTokens: last.outputTokens,
643+
reasoningOutputTokens: last.reasoningTokens,
644+
totalTokens: last.inputTokens + last.outputTokens,
637645
},
638646
},
639647
},
@@ -738,7 +746,67 @@ lifecycleLayer("CodexAdapterLive lifecycle", (it) => {
738746
}),
739747
);
740748

741-
it.effect("does not count old Codex history after resume or rollback", () =>
749+
it.effect("counts the last response when Codex resets its running total mid-turn", () =>
750+
Effect.gen(function* () {
751+
const { adapter, runtime } = yield* startLifecycleRuntime();
752+
const completedFiber = yield* adapter.streamEvents.pipe(
753+
Stream.filter((event) => event.type === "turn.completed"),
754+
Stream.runHead,
755+
Effect.forkChild,
756+
);
757+
758+
yield* runtime.emit(codexTurnEvent("turn/started", "turn-reset"));
759+
yield* runtime.emit(
760+
codexTokenUsageEvent({
761+
id: "evt-reset-1",
762+
turnId: "turn-reset",
763+
inputTokens: 5_000,
764+
cachedInputTokens: 4_000,
765+
cacheCreationTokens: 100,
766+
outputTokens: 500,
767+
reasoningTokens: 200,
768+
last: {
769+
inputTokens: 100,
770+
cachedInputTokens: 80,
771+
cacheCreationTokens: 10,
772+
outputTokens: 20,
773+
reasoningTokens: 8,
774+
},
775+
}),
776+
);
777+
// Codex restarted its cumulative total. The new total is smaller than
778+
// the previous one, so only `last` is counted for this update.
779+
yield* runtime.emit(
780+
codexTokenUsageEvent({
781+
id: "evt-reset-2",
782+
turnId: "turn-reset",
783+
inputTokens: 150,
784+
cachedInputTokens: 90,
785+
cacheCreationTokens: 5,
786+
outputTokens: 30,
787+
reasoningTokens: 12,
788+
}),
789+
);
790+
yield* runtime.emit(codexTurnEvent("turn/completed", "turn-reset"));
791+
792+
const completed = yield* Fiber.join(completedFiber);
793+
NodeAssert.equal(completed._tag, "Some");
794+
if (completed._tag === "Some" && completed.value.type === "turn.completed") {
795+
NodeAssert.deepStrictEqual(completed.value.payload.tokenUsage, {
796+
usageStatus: "complete",
797+
usageScope: "main_agent",
798+
inputTokens: 250,
799+
cachedInputTokens: 170,
800+
cacheCreationTokens: 15,
801+
outputTokens: 50,
802+
reasoningTokens: 20,
803+
hasSubagents: false,
804+
});
805+
}
806+
}),
807+
);
808+
809+
it.effect("uses the last response usage when no prior Codex total exists", () =>
742810
Effect.gen(function* () {
743811
const adapter = yield* CodexAdapter;
744812
yield* adapter.startSession({
@@ -756,6 +824,8 @@ lifecycleLayer("CodexAdapterLive lifecycle", (it) => {
756824
Effect.forkChild,
757825
);
758826

827+
// Resumed thread: the cumulative total already holds old history, so the
828+
// first update must count only `last`.
759829
yield* runtime.emit(codexTurnEvent("turn/started", "turn-resumed"));
760830
yield* runtime.emit(
761831
codexTokenUsageEvent({
@@ -766,6 +836,13 @@ lifecycleLayer("CodexAdapterLive lifecycle", (it) => {
766836
cacheCreationTokens: 100,
767837
outputTokens: 200,
768838
reasoningTokens: 80,
839+
last: {
840+
inputTokens: 300,
841+
cachedInputTokens: 120,
842+
cacheCreationTokens: 30,
843+
outputTokens: 60,
844+
reasoningTokens: 24,
845+
},
769846
}),
770847
);
771848
yield* runtime.emit(codexTurnEvent("turn/completed", "turn-resumed"));
@@ -792,6 +869,8 @@ lifecycleLayer("CodexAdapterLive lifecycle", (it) => {
792869
Stream.runHead,
793870
Effect.forkChild,
794871
);
872+
// Rollback drops the baseline and Codex shrinks its total, so the first
873+
// update after it counts only `last` again.
795874
yield* runtime.emit(codexTurnEvent("turn/started", "turn-after-rollback"));
796875
yield* runtime.emit(
797876
codexTokenUsageEvent({
@@ -802,6 +881,13 @@ lifecycleLayer("CodexAdapterLive lifecycle", (it) => {
802881
cacheCreationTokens: 105,
803882
outputTokens: 210,
804883
reasoningTokens: 84,
884+
last: {
885+
inputTokens: 50,
886+
cachedInputTokens: 20,
887+
cacheCreationTokens: 5,
888+
outputTokens: 10,
889+
reasoningTokens: 4,
890+
},
805891
}),
806892
);
807893
yield* runtime.emit(codexTurnEvent("turn/completed", "turn-after-rollback"));
@@ -817,8 +903,13 @@ lifecycleLayer("CodexAdapterLive lifecycle", (it) => {
817903
),
818904
[
819905
{
820-
usageStatus: "unavailable",
906+
usageStatus: "complete",
821907
usageScope: "main_agent",
908+
inputTokens: 300,
909+
cachedInputTokens: 120,
910+
cacheCreationTokens: 30,
911+
outputTokens: 60,
912+
reasoningTokens: 24,
822913
hasSubagents: false,
823914
},
824915
{
@@ -832,8 +923,13 @@ lifecycleLayer("CodexAdapterLive lifecycle", (it) => {
832923
hasSubagents: false,
833924
},
834925
{
835-
usageStatus: "unavailable",
926+
usageStatus: "complete",
836927
usageScope: "main_agent",
928+
inputTokens: 50,
929+
cachedInputTokens: 20,
930+
cacheCreationTokens: 5,
931+
outputTokens: 10,
932+
reasoningTokens: 4,
837933
hasSubagents: false,
838934
},
839935
],

0 commit comments

Comments
 (0)