Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(store): track cost source alongside session cost totals

Steven Enamakela15cc53168fe
diff --git a/app/src/store/chatRuntimeSlice.ts b/app/src/store/chatRuntimeSlice.ts
index 38ba346a63..d6498e9439 100644
--- a/app/src/store/chatRuntimeSlice.ts
+++ b/app/src/store/chatRuntimeSlice.ts
@@ -3,6 +3,7 @@ import debug from 'debug';
import { mapDisplayItems } from '../features/conversations/derived/mapDisplayItems';
import { threadApi } from '../services/api/threadApi';
+import type { CostSource } from '../services/chatService';
import type { DerivedTranscriptPage } from '../types/derivedTranscript';
import type {
AgentRun,
@@ -494,11 +495,39 @@ export interface SubAgentUsage {
agentId: string;
inputTokens: number;
outputTokens: number;
+ /** Sum of the known costs; read with `costSource`. */
costUsd: number;
+ /** `unknown` once any run's cost was not known: then show no price. */
+ costSource: CostSource;
/** How many times this archetype was spawned across the session. */
runs: number;
}
+const COST_SOURCE_RANK: Record<CostSource, number> = { charged: 0, estimated: 1, unknown: 2 };
+
+/** The less certain of two cost sources. */
+export function worseCostSource(a: CostSource, b: CostSource): CostSource {
+ return COST_SOURCE_RANK[b] > COST_SOURCE_RANK[a] ? b : a;
+}
+
+/**
+ * Folds one reported cost into a running total. A `null` cost (the core did
+ * not know it) adds nothing and makes the total unknown; a number keeps its
+ * reported source (an older core sends no source: its number is a charge).
+ */
+function foldCost(
+ total: { costUsd: number; costSource: CostSource },
+ costUsd: number | null | undefined,
+ costSource: CostSource | undefined
+): void {
+ if (costUsd === null) {
+ total.costSource = 'unknown';
+ return;
+ }
+ total.costUsd += nonNeg(costUsd);
+ total.costSource = worseCostSource(total.costSource, costSource ?? 'charged');
+}
+
/** Running per-session totals accumulated from `chat:done` events (#703). */
export interface SessionTokenUsage {
inputTokens: number;
@@ -509,8 +538,14 @@ export interface SessionTokenUsage {
lastTurnOutputTokens: number;
/** Cached-input tokens accumulated across the session. */
cachedTokens: number;
- /** Total USD cost accumulated across the session (parent + sub-agents). */
+ /** Known USD cost accumulated across the session (parent + sub-agents). */
costUsd: number;
+ /**
+ * How certain `costUsd` is: `charged` (all provider-billed), `estimated`
+ * (some list-price estimate), or `unknown` (some turn's cost is not known,
+ * so no price is shown at all).
+ */
+ costSource: CostSource;
/**
* Most recent known model context window (tokens). `0` until a turn reports a
* real value; the UI falls back to a default when unknown.
@@ -538,6 +573,7 @@ export function emptySessionTokenUsage(): SessionTokenUsage {
lastTurnOutputTokens: 0,
cachedTokens: 0,
costUsd: 0,
+ costSource: 'charged',
contextWindow: 0,
lastTurnContextUsed: 0,
subAgents: {},
@@ -555,7 +591,9 @@ interface ChatTurnUsagePayload {
*/
subAgentSpendOnly?: boolean;
cachedTokens?: number;
- costUsd?: number;
+ /** `null` when the core did not know the cost (never a guess). */
+ costUsd?: number | null;
+ costSource?: CostSource;
contextWindow?: number;
/**
* Tokens the orchestrator's context held after the turn's final model call
@@ -570,7 +608,7 @@ interface ChatTurnUsagePayload {
agentId: string;
inputTokens: number;
outputTokens: number;
- costUsd: number;
+ costUsd: number | null;
}>;
}
@@ -584,7 +622,7 @@ function applyTurnUsage(usage: SessionTokenUsage, payload: ChatTurnUsagePayload)
usage.inputTokens += inTok;
usage.outputTokens += outTok;
usage.cachedTokens += nonNeg(payload.cachedTokens);
- usage.costUsd += nonNeg(payload.costUsd);
+ foldCost(usage, payload.costUsd, payload.costSource);
// A detached sub-agent's spend arrives on its own `subagent_completed`, after
// the parent turn's `chat_done` has already been counted. It is more spend on
// the SAME turn, not another turn, so counting it would inflate the turn
@@ -614,11 +652,12 @@ function applyTurnUsage(usage: SessionTokenUsage, payload: ChatTurnUsagePayload)
inputTokens: 0,
outputTokens: 0,
costUsd: 0,
+ costSource: 'charged',
runs: 0,
};
existing.inputTokens += subIn;
existing.outputTokens += subOut;
- existing.costUsd += nonNeg(sub.costUsd);
+ foldCost(existing, sub.costUsd, undefined);
existing.runs += 1;
usage.subAgents[sub.agentId] = existing;
}