From 5c91d1dc71477e96329a623ea2d6e673c7526a6b Mon Sep 17 00:00:00 2001 From: Amolith Date: Sat, 7 Feb 2026 23:50:23 +0000 Subject: [PATCH] Fix NaN cost display and show request count in usage summary When model cost config is zero (local/free models), the cost math produced NaN. Now cost is only shown when it's a valid positive number. Usage summary now includes the number of LLM requests (assistant messages) for better observability: 'usage: N tokens across M requests'. Co-authored-by: Shelley --- src/agent/runner.ts | 4 ++++ src/cli/commands/repo.ts | 2 +- src/cli/commands/web.ts | 2 +- src/cli/output.ts | 18 +++++++++++++++--- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/agent/runner.ts b/src/agent/runner.ts index c740e7345794ddd20e0731fe6978450857ac4439..2445e26681e29728e6a849ccf03e178d8c4418af 100644 --- a/src/agent/runner.ts +++ b/src/agent/runner.ts @@ -16,6 +16,7 @@ export interface AgentRunOptions { export interface AgentRunResult { message: string; usage?: unknown; + requestCount: number; } /** @@ -82,8 +83,11 @@ export async function runAgent(query: string, options: AgentRunOptions): Promise throw new AgentError("Agent returned no text response"); } + const requestCount = agent.state.messages.filter((msg) => msg.role === "assistant").length; + return { message: text, usage: last?.usage, + requestCount, }; } diff --git a/src/cli/commands/repo.ts b/src/cli/commands/repo.ts index 9cc13ea7ea09d5b708767c5c4a893268c7cf2919..e5d1fd7f0ab4802ddc9552cbcb0da0d8309225fa 100644 --- a/src/cli/commands/repo.ts +++ b/src/cli/commands/repo.ts @@ -90,7 +90,7 @@ export async function runRepoCommand(options: RepoCommandOptions): Promise }); process.stdout.write(result.message + "\n"); - printUsageSummary(result.usage as any); + printUsageSummary(result.usage as any, result.requestCount); } finally { await workspace.cleanup(); } diff --git a/src/cli/commands/web.ts b/src/cli/commands/web.ts index ed0b60fd6738a64e52fd4ae31f2ff2f1043ef5f7..1e86b935e953d51553ac144073c35b91e3485ef8 100644 --- a/src/cli/commands/web.ts +++ b/src/cli/commands/web.ts @@ -97,7 +97,7 @@ export async function runWebCommand(options: WebCommandOptions): Promise { }); process.stdout.write(result.message + "\n"); - printUsageSummary(result.usage as any); + printUsageSummary(result.usage as any, result.requestCount); } finally { await workspace.cleanup(); } diff --git a/src/cli/output.ts b/src/cli/output.ts index 93315e3dce0167e140542dfffe324db1f5275e91..bc78b8ad0b45c449ace2d4688923ce1f625547b3 100644 --- a/src/cli/output.ts +++ b/src/cli/output.ts @@ -44,10 +44,22 @@ export function createEventLogger(options: OutputOptions) { }; } -export function printUsageSummary(usage: { cost?: { total?: number }; totalTokens?: number; output?: number; input?: number } | undefined) { +export function printUsageSummary( + usage: { cost?: { total?: number }; totalTokens?: number; output?: number; input?: number } | undefined, + requestCount?: number, +) { if (!usage) return; - const cost = usage.cost?.total ?? 0; const tokens = usage.totalTokens ?? (usage.output ?? 0) + (usage.input ?? 0); - console.error(`\nusage: ${tokens} tokens, cost $${cost.toFixed(4)}`); + const rawCost = usage.cost?.total; + const cost = typeof rawCost === "number" && !isNaN(rawCost) && rawCost > 0 ? rawCost : undefined; + + let line = `\nusage: ${tokens} tokens`; + if (requestCount !== undefined && requestCount > 0) { + line += ` across ${requestCount} ${requestCount === 1 ? "request" : "requests"}`; + } + if (cost !== undefined) { + line += `, cost $${cost.toFixed(4)}`; + } + console.error(line); }