|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { enrichCreatableEvents, setLlmPricingRegistry } from "./enrichCreatableEvents.server"; |
| 3 | +import type { CreateEventInput } from "../eventRepository/eventRepository.types"; |
| 4 | + |
| 5 | +type Registry = Parameters<typeof setLlmPricingRegistry>[0]; |
| 6 | +type RegistryCost = NonNullable<ReturnType<Registry["calculateCost"]>>; |
| 7 | + |
| 8 | +function registryReturning(cost: RegistryCost | null, isLoaded = true): Registry { |
| 9 | + return { |
| 10 | + isLoaded, |
| 11 | + calculateCost: () => cost, |
| 12 | + }; |
| 13 | +} |
| 14 | + |
| 15 | +// A catalog cost result the registry would produce. The numbers here stand in for a catalog |
| 16 | +// price that disagrees with what the provider actually billed. |
| 17 | +function catalogCost(overrides: Partial<RegistryCost> = {}): RegistryCost { |
| 18 | + return { |
| 19 | + matchedModelId: "llm_model_test", |
| 20 | + matchedModelName: "test-model", |
| 21 | + pricingTierId: "tier_standard", |
| 22 | + pricingTierName: "Standard", |
| 23 | + inputCost: 0, |
| 24 | + outputCost: 0, |
| 25 | + totalCost: 0, |
| 26 | + costDetails: {}, |
| 27 | + ...overrides, |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +function makeEvent(properties: Record<string, unknown>): CreateEventInput { |
| 32 | + return { |
| 33 | + message: "ai.generateText.doGenerate", |
| 34 | + kind: "INTERNAL", |
| 35 | + isPartial: false, |
| 36 | + properties: properties as CreateEventInput["properties"], |
| 37 | + } as unknown as CreateEventInput; |
| 38 | +} |
| 39 | + |
| 40 | +function enrichOne(event: CreateEventInput): CreateEventInput { |
| 41 | + const [out] = enrichCreatableEvents([event]); |
| 42 | + return out; |
| 43 | +} |
| 44 | + |
| 45 | +function costPillText(event: CreateEventInput): string | undefined { |
| 46 | + const accessory = (event.style as any)?.accessory; |
| 47 | + const items: Array<{ text: string; icon: string }> = accessory?.items ?? []; |
| 48 | + return items.find((i) => i.icon === "tabler-currency-dollar")?.text; |
| 49 | +} |
| 50 | + |
| 51 | +function modelPillText(event: CreateEventInput): string | undefined { |
| 52 | + const accessory = (event.style as any)?.accessory; |
| 53 | + const items: Array<{ text: string; icon: string }> = accessory?.items ?? []; |
| 54 | + return items.find((i) => i.icon === "tabler-cube")?.text; |
| 55 | +} |
| 56 | + |
| 57 | +describe("enrichLlmMetrics — provider-reported cost", () => { |
| 58 | + it("prefers the provider-reported cost over catalog pricing when a cache discount applies", () => { |
| 59 | + // OpenRouter served mimo with 25,280 of 34,374 prompt tokens as cache reads. Cache counts |
| 60 | + // only reach us via providerMetadata, so the catalog bills the full prompt at the input |
| 61 | + // rate while OpenRouter's exact bill reflects the cache discount. |
| 62 | + setLlmPricingRegistry(registryReturning(catalogCost({ totalCost: 0.01603584 }))); |
| 63 | + |
| 64 | + const out = enrichOne( |
| 65 | + makeEvent({ |
| 66 | + "gen_ai.system": "openrouter", |
| 67 | + "gen_ai.request.model": "xiaomi/mimo-v2.5-pro", |
| 68 | + "gen_ai.response.model": "xiaomi/mimo-v2.5-pro-20260422", |
| 69 | + "gen_ai.usage.input_tokens": 34374, |
| 70 | + "gen_ai.usage.output_tokens": 1245, |
| 71 | + "ai.response.providerMetadata": JSON.stringify({ |
| 72 | + openrouter: { |
| 73 | + provider: "Xiaomi", |
| 74 | + usage: { cost: 0.005130048, promptTokensDetails: { cachedTokens: 25280 } }, |
| 75 | + }, |
| 76 | + }), |
| 77 | + }) |
| 78 | + ); |
| 79 | + |
| 80 | + expect(out.properties["trigger.llm.total_cost"]).toBe(0.005130048); |
| 81 | + expect(out.properties["trigger.llm.cost_source"]).toBe("openrouter"); |
| 82 | + // The catalog breakdown must not be written — it priced the wrong (full-rate) total. |
| 83 | + expect(out.properties["trigger.llm.input_cost"]).toBeUndefined(); |
| 84 | + expect(out.properties["trigger.llm.matched_model"]).toBeUndefined(); |
| 85 | + expect(costPillText(out)).toBe("$0.005130"); |
| 86 | + |
| 87 | + expect(out._llmMetrics?.totalCost).toBe(0.005130048); |
| 88 | + expect(out._llmMetrics?.costSource).toBe("openrouter"); |
| 89 | + expect(out._llmMetrics?.providerCost).toBe(0.005130048); |
| 90 | + expect(out._llmMetrics?.inputCost).toBe(0); |
| 91 | + }); |
| 92 | + |
| 93 | + it("prices the served fallback model's provider cost, not the requested model", () => { |
| 94 | + // Requested mimo, OpenRouter routed to a gemini fallback: gen_ai.response.model carries the |
| 95 | + // SERVED model, and the provider cost is authoritative. |
| 96 | + setLlmPricingRegistry(registryReturning(catalogCost({ totalCost: 0.02 }))); |
| 97 | + |
| 98 | + const out = enrichOne( |
| 99 | + makeEvent({ |
| 100 | + "gen_ai.system": "openrouter", |
| 101 | + "gen_ai.request.model": "xiaomi/mimo-v2.5-pro", |
| 102 | + "gen_ai.response.model": "google/gemini-3.5-flash-20260519", |
| 103 | + "gen_ai.usage.input_tokens": 5000, |
| 104 | + "gen_ai.usage.output_tokens": 800, |
| 105 | + "ai.response.providerMetadata": JSON.stringify({ |
| 106 | + openrouter: { provider: "Google", usage: { cost: 0.011058 } }, |
| 107 | + }), |
| 108 | + }) |
| 109 | + ); |
| 110 | + |
| 111 | + expect(out.properties["trigger.llm.total_cost"]).toBe(0.011058); |
| 112 | + expect(out.properties["trigger.llm.cost_source"]).toBe("openrouter"); |
| 113 | + // The pill (and stored response model) reflect the served fallback model. |
| 114 | + expect(modelPillText(out)).toBe("google/gemini-3.5-flash-20260519"); |
| 115 | + expect(out._llmMetrics?.requestModel).toBe("xiaomi/mimo-v2.5-pro"); |
| 116 | + expect(out._llmMetrics?.responseModel).toBe("google/gemini-3.5-flash-20260519"); |
| 117 | + expect(out._llmMetrics?.totalCost).toBe(0.011058); |
| 118 | + }); |
| 119 | + |
| 120 | + it("prefers the gateway-reported cost over catalog pricing", () => { |
| 121 | + setLlmPricingRegistry(registryReturning(catalogCost({ totalCost: 0.02 }))); |
| 122 | + |
| 123 | + const out = enrichOne( |
| 124 | + makeEvent({ |
| 125 | + "gen_ai.system": "gateway", |
| 126 | + "gen_ai.response.model": "openai/gpt-4o", |
| 127 | + "gen_ai.usage.input_tokens": 1000, |
| 128 | + "gen_ai.usage.output_tokens": 500, |
| 129 | + "ai.response.providerMetadata": JSON.stringify({ |
| 130 | + gateway: { cost: "0.0006615" }, |
| 131 | + }), |
| 132 | + }) |
| 133 | + ); |
| 134 | + |
| 135 | + expect(out.properties["trigger.llm.total_cost"]).toBe(0.0006615); |
| 136 | + expect(out.properties["trigger.llm.cost_source"]).toBe("gateway"); |
| 137 | + expect(out._llmMetrics?.costSource).toBe("gateway"); |
| 138 | + }); |
| 139 | + |
| 140 | + it("uses provider cost even when the catalog does not match the model", () => { |
| 141 | + setLlmPricingRegistry(registryReturning(null)); |
| 142 | + |
| 143 | + const out = enrichOne( |
| 144 | + makeEvent({ |
| 145 | + "gen_ai.system": "openrouter", |
| 146 | + "gen_ai.response.model": "some/unlisted-model", |
| 147 | + "gen_ai.usage.input_tokens": 2000, |
| 148 | + "gen_ai.usage.output_tokens": 300, |
| 149 | + "ai.response.providerMetadata": JSON.stringify({ |
| 150 | + openrouter: { provider: "SomeProvider", usage: { cost: 0.00042 } }, |
| 151 | + }), |
| 152 | + }) |
| 153 | + ); |
| 154 | + |
| 155 | + expect(out.properties["trigger.llm.total_cost"]).toBe(0.00042); |
| 156 | + expect(out.properties["trigger.llm.cost_source"]).toBe("openrouter"); |
| 157 | + }); |
| 158 | + |
| 159 | + it("falls back to catalog pricing when no provider cost is reported", () => { |
| 160 | + setLlmPricingRegistry( |
| 161 | + registryReturning( |
| 162 | + catalogCost({ |
| 163 | + matchedModelId: "llm_model_gpt4o", |
| 164 | + matchedModelName: "gpt-4o", |
| 165 | + inputCost: 0.04, |
| 166 | + outputCost: 0.01, |
| 167 | + totalCost: 0.05, |
| 168 | + costDetails: { input: 0.04, output: 0.01 }, |
| 169 | + }) |
| 170 | + ) |
| 171 | + ); |
| 172 | + |
| 173 | + const out = enrichOne( |
| 174 | + makeEvent({ |
| 175 | + "gen_ai.system": "openai", |
| 176 | + "gen_ai.response.model": "gpt-4o", |
| 177 | + "gen_ai.usage.input_tokens": 1000, |
| 178 | + "gen_ai.usage.output_tokens": 500, |
| 179 | + }) |
| 180 | + ); |
| 181 | + |
| 182 | + expect(out.properties["trigger.llm.total_cost"]).toBe(0.05); |
| 183 | + expect(out.properties["trigger.llm.input_cost"]).toBe(0.04); |
| 184 | + expect(out.properties["trigger.llm.output_cost"]).toBe(0.01); |
| 185 | + expect(out.properties["trigger.llm.matched_model"]).toBe("gpt-4o"); |
| 186 | + // Registry path does not set a cost_source attribute. |
| 187 | + expect(out.properties["trigger.llm.cost_source"]).toBeUndefined(); |
| 188 | + expect(out._llmMetrics?.costSource).toBe("registry"); |
| 189 | + expect(out._llmMetrics?.matchedModelId).toBe("llm_model_gpt4o"); |
| 190 | + }); |
| 191 | + |
| 192 | + it("falls back to catalog pricing when providerMetadata carries no cost field", () => { |
| 193 | + // The cheap `"cost"` guard should skip parsing and let the registry price this span. |
| 194 | + setLlmPricingRegistry( |
| 195 | + registryReturning(catalogCost({ totalCost: 0.03, matchedModelName: "claude" })) |
| 196 | + ); |
| 197 | + |
| 198 | + const out = enrichOne( |
| 199 | + makeEvent({ |
| 200 | + "gen_ai.system": "anthropic", |
| 201 | + "gen_ai.response.model": "claude-sonnet-4-0", |
| 202 | + "gen_ai.usage.input_tokens": 1000, |
| 203 | + "gen_ai.usage.output_tokens": 500, |
| 204 | + "ai.response.providerMetadata": JSON.stringify({ |
| 205 | + anthropic: { usage: { service_tier: "standard" } }, |
| 206 | + }), |
| 207 | + }) |
| 208 | + ); |
| 209 | + |
| 210 | + expect(out.properties["trigger.llm.total_cost"]).toBe(0.03); |
| 211 | + expect(out.properties["trigger.llm.cost_source"]).toBeUndefined(); |
| 212 | + expect(out._llmMetrics?.costSource).toBe("registry"); |
| 213 | + }); |
| 214 | +}); |
0 commit comments