Skip to content

Commit f0ad5d5

Browse files
Type Claude usage parsing with SDK types
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent e9cfd8e commit f0ad5d5

1 file changed

Lines changed: 10 additions & 20 deletions

File tree

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

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
type SDKResultMessage,
1818
type SettingSource,
1919
type SDKUserMessage,
20+
ModelUsage,
21+
NonNullableUsage,
2022
} from "@anthropic-ai/claude-agent-sdk";
2123
import {
2224
ApprovalRequestId,
@@ -272,32 +274,22 @@ function asRuntimeItemId(value: string): RuntimeItemId {
272274
return RuntimeItemId.make(value);
273275
}
274276

275-
function maxClaudeContextWindowFromModelUsage(modelUsage: unknown): number | undefined {
276-
if (!modelUsage || typeof modelUsage !== "object") {
277-
return undefined;
278-
}
277+
function maxClaudeContextWindowFromModelUsage(
278+
modelUsage: Record<string, ModelUsage> | undefined,
279+
): number | undefined {
280+
if (!modelUsage) return undefined;
279281

280282
let maxContextWindow: number | undefined;
281-
for (const value of Object.values(modelUsage as Record<string, unknown>)) {
282-
if (!value || typeof value !== "object") {
283-
continue;
284-
}
285-
const contextWindow = (value as { contextWindow?: unknown }).contextWindow;
286-
if (
287-
typeof contextWindow !== "number" ||
288-
!Number.isFinite(contextWindow) ||
289-
contextWindow <= 0
290-
) {
291-
continue;
292-
}
283+
for (const value of Object.values(modelUsage)) {
284+
const contextWindow = value.contextWindow;
293285
maxContextWindow = Math.max(maxContextWindow ?? 0, contextWindow);
294286
}
295287

296288
return maxContextWindow;
297289
}
298290

299291
function normalizeClaudeTokenUsage(
300-
value: unknown,
292+
value: NonNullableUsage | undefined,
301293
contextWindow?: number,
302294
): ThreadTokenUsageSnapshot | undefined {
303295
if (!value || typeof value !== "object") {
@@ -1333,8 +1325,6 @@ const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
13331325
errorMessage?: string,
13341326
result?: SDKResultMessage,
13351327
) {
1336-
const resultUsage =
1337-
result?.usage && typeof result.usage === "object" ? { ...result.usage } : undefined;
13381328
const resultContextWindow = maxClaudeContextWindowFromModelUsage(result?.modelUsage);
13391329
if (resultContextWindow !== undefined) {
13401330
context.lastKnownContextWindow = resultContextWindow;
@@ -1346,7 +1336,7 @@ const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
13461336
// Instead, use the last known context-window-accurate usage from task_progress
13471337
// events and treat the accumulated total as totalProcessedTokens.
13481338
const accumulatedSnapshot = normalizeClaudeTokenUsage(
1349-
resultUsage,
1339+
result?.usage,
13501340
resultContextWindow ?? context.lastKnownContextWindow,
13511341
);
13521342
const accumulatedTotalProcessedTokens =

0 commit comments

Comments
 (0)