Skip to content

Build your personal knowledge base with Trilium Notes

refactor(llm): delegate pricings to provider

Elian Doran2db9e376d555
diff --git a/apps/server/src/routes/api/llm_chat.ts b/apps/server/src/routes/api/llm_chat.ts
index 07c79318dc..279c34186e 100644
--- a/apps/server/src/routes/api/llm_chat.ts
+++ b/apps/server/src/routes/api/llm_chat.ts
@@ -46,9 +46,10 @@ async function streamChat(req: Request, res: Response) {
const provider = getProvider(config.provider || "anthropic");
const result = provider.chat(messages, config);
- // Pass model for cost calculation
+ // Get pricing from provider for cost calculation
const model = config.model || "claude-sonnet-4-20250514";
- for await (const chunk of streamToChunks(result, { model })) {
+ const pricing = provider.getModelPricing(model);
+ for await (const chunk of streamToChunks(result, { pricing })) {
res.write(`data: ${JSON.stringify(chunk)}\n\n`);
// Flush immediately to ensure real-time streaming
if (typeof flushableRes.flush === "function") {
diff --git a/apps/server/src/services/llm/providers/anthropic.ts b/apps/server/src/services/llm/providers/anthropic.ts
index 03e0bc91b6..a05c1d45da 100644
--- a/apps/server/src/services/llm/providers/anthropic.ts
+++ b/apps/server/src/services/llm/providers/anthropic.ts
@@ -3,11 +3,27 @@ import { streamText, stepCountIs, type CoreMessage } from "ai";
import type { LlmMessage } from "@triliumnext/commons";
import { noteTools } from "../tools.js";
-import type { LlmProvider, LlmProviderConfig, StreamResult } from "../types.js";
+import type { LlmProvider, LlmProviderConfig, ModelPricing, StreamResult } from "../types.js";
const DEFAULT_MODEL = "claude-sonnet-4-20250514";
const DEFAULT_MAX_TOKENS = 8096;
+/**
+ * Pricing per million tokens for Anthropic models (USD).
+ */
+const MODEL_PRICING: Record<string, ModelPricing> = {
+ // Claude Sonnet 4
+ "claude-sonnet-4-20250514": { input: 3, output: 15 },
+ // Claude Opus 4
+ "claude-opus-4-20250514": { input: 15, output: 75 },
+ // Claude Haiku 3.5
+ "claude-3-5-haiku-20241022": { input: 0.8, output: 4 },
+ "claude-3-5-haiku-latest": { input: 0.8, output: 4 },
+ // Claude Sonnet 3.5
+ "claude-3-5-sonnet-20241022": { input: 3, output: 15 },
+ "claude-3-5-sonnet-latest": { input: 3, output: 15 },
+};
+
export class AnthropicProvider implements LlmProvider {
name = "anthropic";
@@ -81,4 +97,8 @@ export class AnthropicProvider implements LlmProvider {
return streamText(streamOptions);
}
+
+ getModelPricing(model: string): ModelPricing | undefined {
+ return MODEL_PRICING[model];
+ }
}
diff --git a/apps/server/src/services/llm/stream.ts b/apps/server/src/services/llm/stream.ts
index c7ebbd523b..e86fc4c61b 100644
--- a/apps/server/src/services/llm/stream.ts
+++ b/apps/server/src/services/llm/stream.ts
@@ -4,32 +4,12 @@
import type { LlmStreamChunk } from "@triliumnext/commons";
-import type { StreamResult } from "./types.js";
+import type { ModelPricing, StreamResult } from "./types.js";
/**
- * Pricing per million tokens for known models.
- * Prices in USD as of 2024.
+ * Calculate estimated cost in USD based on token usage and pricing.
*/
-const MODEL_PRICING: Record<string, { input: number; output: number }> = {
- // Claude Sonnet 4
- "claude-sonnet-4-20250514": { input: 3, output: 15 },
- // Claude Opus 4
- "claude-opus-4-20250514": { input: 15, output: 75 },
- // Claude Haiku 3.5
- "claude-3-5-haiku-20241022": { input: 0.8, output: 4 },
- "claude-3-5-haiku-latest": { input: 0.8, output: 4 },
- // Claude Sonnet 3.5
- "claude-3-5-sonnet-20241022": { input: 3, output: 15 },
- "claude-3-5-sonnet-latest": { input: 3, output: 15 },
-};
-
-/**
- * Calculate estimated cost in USD based on token usage and model.
- */
-function calculateCost(inputTokens: number, outputTokens: number, model?: string): number | undefined {
- if (!model) return undefined;
-
- const pricing = MODEL_PRICING[model];
+function calculateCost(inputTokens: number, outputTokens: number, pricing?: ModelPricing): number | undefined {
if (!pricing) return undefined;
const inputCost = (inputTokens / 1_000_000) * pricing.input;
@@ -39,8 +19,8 @@ function calculateCost(inputTokens: number, outputTokens: number, model?: string
}
export interface StreamOptions {
- /** Model identifier for cost calculation */
- model?: string;
+ /** Model pricing for cost calculation (from provider) */
+ pricing?: ModelPricing;
}
/**
@@ -99,7 +79,7 @@ export async function* streamToChunks(result: StreamResult, options: StreamOptio
// Get usage information after stream completes
const usage = await result.usage;
if (usage && typeof usage.inputTokens === "number" && typeof usage.outputTokens === "number") {
- const cost = calculateCost(usage.inputTokens, usage.outputTokens, options.model);
+ const cost = calculateCost(usage.inputTokens, usage.outputTokens, options.pricing);
yield {
type: "usage",
usage: {
diff --git a/apps/server/src/services/llm/types.ts b/apps/server/src/services/llm/types.ts
index a7f080fa86..4b8166399b 100644
--- a/apps/server/src/services/llm/types.ts
+++ b/apps/server/src/services/llm/types.ts
@@ -20,6 +20,16 @@ export interface LlmProviderConfig extends LlmChatConfig {
*/
export type StreamResult = ReturnType<typeof streamText>;
+/**
+ * Pricing per million tokens for a model.
+ */
+export interface ModelPricing {
+ /** Cost per million input tokens in USD */
+ input: number;
+ /** Cost per million output tokens in USD */
+ output: number;
+}
+
export interface LlmProvider {
name: string;
@@ -31,4 +41,9 @@ export interface LlmProvider {
messages: LlmMessage[],
config: LlmProviderConfig
): StreamResult;
+
+ /**
+ * Get pricing for a model. Returns undefined if pricing is not available.
+ */
+ getModelPricing(model: string): ModelPricing | undefined;
}