feat(ai): Add cost tracking to LLM implementations

- Track input/output token costs for all providers
- Calculate costs based on Model pricing information
- Include cost information in AssistantMessage responses
- Add Usage interface with detailed cost breakdown
- Implement calculateCost utility function for cost calculations
This commit is contained in:
Mario Zechner 2025-08-30 00:45:08 +02:00
parent f9d688d577
commit 550da5e47c
6 changed files with 61 additions and 14 deletions

View file

@ -5,6 +5,7 @@ import {
type GenerateContentParameters,
GoogleGenAI,
} from "@google/genai";
import { calculateCost } from "../models.js";
import type {
AssistantMessage,
Context,
@ -13,9 +14,9 @@ import type {
Message,
Model,
StopReason,
TokenUsage,
Tool,
ToolCall,
Usage,
} from "../types.js";
export interface GoogleLLMOptions extends LLMOptions {
@ -97,11 +98,12 @@ export class GoogleLLM implements LLM<GoogleLLMOptions> {
let thinking = "";
let thoughtSignature: string | undefined;
const toolCalls: ToolCall[] = [];
let usage: TokenUsage = {
let usage: Usage = {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
};
let stopReason: StopReason = "stop";
let inTextBlock = false;
@ -179,6 +181,13 @@ export class GoogleLLM implements LLM<GoogleLLMOptions> {
(chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),
cacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,
cacheWrite: 0,
cost: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
total: 0,
},
};
}
}
@ -203,6 +212,9 @@ export class GoogleLLM implements LLM<GoogleLLMOptions> {
thoughtSignature = Buffer.from(signature).toString("base64");
}
// Calculate cost
calculateCost(this.model, usage);
// Usage metadata is in the last chunk
// Already captured during streaming
@ -227,6 +239,7 @@ export class GoogleLLM implements LLM<GoogleLLMOptions> {
output: 0,
cacheRead: 0,
cacheWrite: 0,
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
},
stopReason: "error",
error: error instanceof Error ? error.message : String(error),