Skip to content

Commit d736380

Browse files
committed
fix: treat zero-rate usage cost as unknown
1 parent 57c9d6d commit d736380

3 files changed

Lines changed: 15 additions & 39 deletions

File tree

‎src/infra/session-cost-usage.test.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,14 @@ describe("session cost usage", () => {
194194
});
195195
});
196196

197-
it("preserves an operator-configured zero-cost model as a complete $0, not missing", async () => {
198-
const root = await makeSessionCostRoot("cost-intentional-free");
197+
it("counts token usage for a configured all-zero model as missing because pricing is still unknown", async () => {
198+
const root = await makeSessionCostRoot("cost-configured-zero-unknown");
199199
const sessionsDir = path.join(root, "agents", "main", "sessions");
200200
await fs.mkdir(sessionsDir, { recursive: true });
201201

202-
// Same shape of turn, but here the operator deliberately priced the model at 0
203-
// (e.g. a local/self-hosted free model). That intentional $0 must be respected.
202+
// Same shape of turn, with a configured all-zero cost block. After config defaults,
203+
// omitted cost and explicit all-zero cost are indistinguishable, so a zero-rate
204+
// token-burning turn is still safer to report as missing than as complete $0 spend.
204205
const entry = {
205206
type: "message",
206207
timestamp: new Date().toISOString(),
@@ -225,8 +226,8 @@ describe("session cost usage", () => {
225226
"utf-8",
226227
);
227228

228-
// The operator explicitly configured this model's price as 0 — an intentional
229-
// "free" price that must be preserved as complete $0 cost data.
229+
// This mirrors normalized config where a model declaration without pricing has
230+
// already received default zero rates.
230231
const config = {
231232
models: {
232233
providers: {
@@ -247,8 +248,7 @@ describe("session cost usage", () => {
247248
const summary = await loadCostUsageSummary({ days: 30, config });
248249
expect(summary.totals.totalTokens).toBe(23287);
249250
expect(summary.totals.totalCost).toBe(0);
250-
// Operator-configured $0 is intentional and complete — not a missing entry.
251-
expect(summary.totals.missingCostEntries).toBe(0);
251+
expect(summary.totals.missingCostEntries).toBe(1);
252252
});
253253
});
254254

‎src/infra/session-cost-usage.ts‎

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import { normalizeOptionalString } from "../shared/string-coerce.js";
2424
import { countToolResults, extractToolCallNames } from "../utils/transcript-tools.js";
2525
import {
2626
estimateUsageCost,
27-
resolveConfiguredModelCost,
2827
resolveModelCostConfig,
2928
resolveModelCostConfigFingerprint,
3029
} from "../utils/usage-format.js";
@@ -1122,23 +1121,16 @@ async function scanTranscriptFile(params: {
11221121
entry.costBreakdown = undefined;
11231122
} else if (
11241123
!isModelPricingKnown(cost) &&
1125-
resolveConfiguredModelCost({
1126-
provider: entry.provider,
1127-
model: entry.model,
1128-
config: params.config,
1129-
}) === undefined &&
11301124
(entry.costTotal === undefined || entry.costTotal === 0) &&
11311125
computeUsageTokenTotals(entry.usage).totalTokens > 0
11321126
) {
1133-
// Pricing for this model is unknown: it has no positive per-token rate, the
1134-
// operator did not explicitly configure its price (so an all-zero value is a
1135-
// catalog default, not an intentional "free" price), and there is no trustworthy
1136-
// recorded cost — the transport either recorded nothing or a fabricated $0
1137-
// derived from an all-zero catalog entry. Surface this token-burning turn as a
1138-
// missing-cost entry instead of recording a confident $0, so budget and spike
1139-
// safeguards that read totalCost are not left blind to it. A turn with a real
1140-
// positive recorded cost, or a model the operator deliberately priced at 0, is
1141-
// preserved by the guards above.
1127+
// Pricing for this model is unknown: it has no positive per-token rate and no
1128+
// trustworthy recorded cost. The transport either recorded nothing or a
1129+
// fabricated $0 derived from an all-zero/default catalog entry. Surface this
1130+
// token-burning turn as a missing-cost entry instead of recording a confident
1131+
// $0, so budget and spike safeguards that read totalCost are not left blind to
1132+
// it. A turn carrying a real positive recorded cost is preserved by the guard
1133+
// above.
11421134
entry.costTotal = undefined;
11431135
entry.costBreakdown = undefined;
11441136
} else if (entry.costTotal === undefined) {

‎src/utils/usage-format.ts‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -298,22 +298,6 @@ export function resolveModelCostConfigFingerprint(config?: OpenClawConfig): stri
298298
});
299299
}
300300

301-
// Returns model pricing ONLY when the operator explicitly configured it under
302-
// `models.providers` in their OpenClaw config. Unlike resolveModelCostConfig this
303-
// ignores the generated model catalog (models.json) and the gateway pricing cache, so
304-
// callers can tell an intentional operator-set price (e.g. a deliberately free local
305-
// model priced at 0) apart from a price the catalog merely defaulted to zero.
306-
export function resolveConfiguredModelCost(params: {
307-
provider?: string;
308-
model?: string;
309-
config?: OpenClawConfig;
310-
}): ModelCostConfig | undefined {
311-
return (
312-
findConfiguredProviderCost({ ...params, allowPluginNormalization: false }) ??
313-
findConfiguredProviderCost(params)
314-
);
315-
}
316-
317301
export function resolveModelCostConfig(params: {
318302
provider?: string;
319303
model?: string;

0 commit comments

Comments
 (0)