Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(llm): cost calculation doesn't take all steps into consideration
Elian Doran8920e0c6be79
diff --git a/apps/server/src/services/llm/stream.spec.ts b/apps/server/src/services/llm/stream.spec.tsindex fe15712a07..319ce43b13 100644--- a/apps/server/src/services/llm/stream.spec.ts+++ b/apps/server/src/services/llm/stream.spec.ts@@ -8,14 +8,14 @@ import type { StreamResult } from "./types.js";type ErrorChunk = Extract<LlmStreamChunk, { type: "error" }>;/** Build a minimal fake StreamResult exposing just what streamToChunks consumes. */-function fakeResult(parts: unknown[], usage: Promise<unknown>): StreamResult {+function fakeResult(parts: unknown[], totalUsage: Promise<unknown>): StreamResult {return {fullStream: (async function* () {for (const part of parts) {yield part;}})(),- usage+ totalUsage} as unknown as StreamResult;}@@ -40,8 +40,8 @@ function apiCallError(): APICallError {}/**- * The AI SDK rejects `result.usage` with this generic message whenever the stream- * produced no steps — which is exactly what happens after a connection-level error.+ * The AI SDK rejects `result.totalUsage` with this generic message whenever the+ * stream produced no steps — which is exactly what happens after a connection-level error.*/function noOutputUsage(): Promise<never> {return Promise.reject(new Error("No output generated. Check the stream for errors."));@@ -72,7 +72,7 @@ describe("streamToChunks", () => {});it("does not mask a real stream error with the generic 'No output generated' error", async () => {- // A genuine error part arrives, then `usage` rejects because no steps ran.+ // A genuine error part arrives, then `totalUsage` rejects because no steps ran.const chunks = await collect(fakeResult([{ type: "error", error: apiCallError() }],noOutputUsage()diff --git a/apps/server/src/services/llm/stream.ts b/apps/server/src/services/llm/stream.tsindex 1b871de9fb..82b4da168f 100644--- a/apps/server/src/services/llm/stream.ts+++ b/apps/server/src/services/llm/stream.ts@@ -136,14 +136,16 @@ export async function* streamToChunks(result: StreamResult, options: StreamOptio}}- // Get usage information after the stream completes. When the stream produced- // no steps, the AI SDK rejects `usage` with a generic NoOutputGeneratedError- // ("No output generated. Check the stream for errors."). If a real error was- // already emitted above, that earlier error is the actual cause — don't mask- // it with the generic one.+ // Get usage information after the stream completes. Use `totalUsage`, which+ // aggregates token usage across every step of the agentic loop — `usage` only+ // reports the final step, undercounting cost on turns that involve tool calls.+ // When the stream produced no steps, the AI SDK rejects `totalUsage` with a+ // generic NoOutputGeneratedError ("No output generated. Check the stream for+ // errors."). If a real error was already emitted above, that earlier error is+ // the actual cause — don't mask it with the generic one.let usage: LanguageModelUsage;try {- usage = await result.usage;+ usage = await result.totalUsage;} catch (error) {if (!errorEmitted) {yield { type: "error", error: describeStreamError(error) };