@@ -370,6 +370,87 @@ export function filterModelMessages(
370370 );
371371 }
372372
373+function toRecord(value: unknown): Record<string, unknown> | null {
374+ return typeof value === "object" && value !== null
375+ ? (value as Record<string, unknown>)
376+ : null;
377+}
378+
379+function readFiniteNumber(
380+ record: Record<string, unknown>,
381+ key: string,
382+): number | null {
383+ const value = record[key];
384+ return typeof value === "number" && Number.isFinite(value) ? value : null;
385+}
386+
387+/**
388+ * Return an assistant message's usage when the persisted shape is valid.
389+ *
390+ * Session rows are treated as untrusted at runtime because older builds or
391+ * external tooling may have stored assistant messages without a `usage`
392+ * payload. Invalid or missing usage is ignored instead of crashing session
393+ * loading or stats calculations.
394+ *
395+ * @param message - Message to inspect.
396+ * @returns The assistant usage payload, or `null` when it is missing/invalid.
397+ */
398+export function getAssistantUsage(
399+ message: PersistedMessage | Message,
400+): AssistantMessage["usage"] | null {
401+ if (message.role !== "assistant") {
402+ return null;
403+ }
404+
405+ const messageRecord = toRecord(message);
406+ const usageRecord = toRecord(messageRecord?.usage);
407+ const costRecord = toRecord(usageRecord?.cost);
408+ if (!usageRecord || !costRecord) {
409+ return null;
410+ }
411+
412+ const input = readFiniteNumber(usageRecord, "input");
413+ const output = readFiniteNumber(usageRecord, "output");
414+ const cacheRead = readFiniteNumber(usageRecord, "cacheRead");
415+ const cacheWrite = readFiniteNumber(usageRecord, "cacheWrite");
416+ const totalTokens = readFiniteNumber(usageRecord, "totalTokens");
417+ const costInput = readFiniteNumber(costRecord, "input");
418+ const costOutput = readFiniteNumber(costRecord, "output");
419+ const costCacheRead = readFiniteNumber(costRecord, "cacheRead");
420+ const costCacheWrite = readFiniteNumber(costRecord, "cacheWrite");
421+ const costTotal = readFiniteNumber(costRecord, "total");
422+
423+ if (
424+ input === null ||
425+ output === null ||
426+ cacheRead === null ||
427+ cacheWrite === null ||
428+ totalTokens === null ||
429+ costInput === null ||
430+ costOutput === null ||
431+ costCacheRead === null ||
432+ costCacheWrite === null ||
433+ costTotal === null
434+ ) {
435+ return null;
436+ }
437+
438+ return {
439+ input,
440+ output,
441+ cacheRead,
442+ cacheWrite,
443+ totalTokens,
444+ cost: {
445+ input: costInput,
446+ output: costOutput,
447+ cacheRead: costCacheRead,
448+ cacheWrite: costCacheWrite,
449+ total: costTotal,
450+ },
451+ };
452+}
453+
373454 /**
374455 * Append a UI-only message to a session's history.
375456 *
@@ -659,12 +740,13 @@ export function computeStats(
659740 let totalCost = 0;
660741
661742 for (const msg of messages) {
662- if (msg.role === "assistant") {
663- const am = msg as AssistantMessage;
664- totalInput += am.usage.input;
665- totalOutput += am.usage.output;
666- totalCost += am.usage.cost.total;
743+ const usage = getAssistantUsage(msg);
744+ if (!usage) {
745+ continue;
667746 }
747+ totalInput += usage.input;
748+ totalOutput += usage.output;
749+ totalCost += usage.cost.total;
668750 }
669751
670752 return { totalInput, totalOutput, totalCost };