🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(pricing): match tiers on observation attributes (#16204)
Hassieb Pakzad9a837216913c
diff --git a/fern/apis/server/definition/commons.yml b/fern/apis/server/definition/commons.ymlindex e6fa97c84d..266c0d4faa 100644--- a/fern/apis/server/definition/commons.yml+++ b/fern/apis/server/definition/commons.yml@@ -770,7 +770,7 @@ types:Models can have either simple flat pricing or tiered pricing:- Flat pricing: Single price per usage type (legacy, but still supported)- - Tiered pricing: Multiple pricing tiers with conditional matching based on usage patterns+ - Tiered pricing: Multiple pricing tiers with conditional matching based on usage patterns or observation attributesThe pricing tiers approach is recommended for models with usage-based pricing variations.When using tiered pricing, the flat price fields (inputPrice, outputPrice, prices) are populated@@ -818,10 +818,10 @@ types:type: map<string, ModelPrice>pricingTiers:docs: |- Array of pricing tiers with conditional pricing based on usage thresholds.+ Array of pricing tiers with conditional pricing based on usage thresholds or observation attributes.Pricing tiers enable accurate cost tracking for models that charge different rates based on usage patterns- (e.g., different rates for high-volume usage, large context windows, or cached tokens).+ or request attributes (e.g., high-volume usage, large context windows, cached tokens, or service tiers).Each model must have exactly one default tier (isDefault=true, priority=0) that serves as a fallback.Additional conditional tiers can be defined with specific matching criteria.@@ -835,54 +835,55 @@ types:PricingTierCondition:docs: |- Condition for matching a pricing tier based on usage details. Used to implement tiered pricing models where costs vary based on usage thresholds.+ Condition for matching a pricing tier against usage details or observation attributes.- How it works:- 1. The regex pattern matches against usage detail keys (e.g., "input_tokens", "input_cached")- 2. Values of all matching keys are summed together- 3. The sum is compared against the threshold value using the specified operator- 4. All conditions in a tier must be met (AND logic) for the tier to match+ Usage-detail conditions treat usageDetailPattern as a regex, sum all matching usage values, and compare the sum to the numeric value. Model-parameter and metadata conditions match an exact top-level key against one or more string values.+ discriminated: false+ union:+ - PricingTierUsageCondition+ - PricingTierAttributeCondition- Common use cases:- - Threshold-based pricing: Match when accumulated usage exceeds a certain amount- - Usage-type-specific pricing: Different rates for cached vs non-cached tokens, or input vs output- - Volume-based pricing: Different rates based on total request or token count+ PricingTierUsageCondition:+ docs: Condition that sums usage details whose keys match a regex.properties:usageDetailPattern:- docs: |- Regex pattern to match against usage detail keys. All matching keys' values are summed for threshold comparison.-- Examples:- - "^input" matches "input", "input_tokens", "input_cached", etc.- - "^(input|prompt)" matches both "input_tokens" and "prompt_tokens"- - "_cache$" matches "input_cache", "output_cache", etc.-- The pattern is case-insensitive by default. If no keys match, the sum is treated as zero.+ docs: Regex pattern matched against usage detail keys.type: string- operator:- docs: |- Comparison operator to apply between the summed value and the threshold.-- - gt: greater than (sum > threshold)- - gte: greater than or equal (sum >= threshold)- - lt: less than (sum < threshold)- - lte: less than or equal (sum <= threshold)- - eq: equal (sum == threshold)- - neq: not equal (sum != threshold)- type: PricingTierOperator+ operator: PricingTierOperatorvalue:- docs: Threshold value for comparison. For token-based pricing, this is typically the token count threshold (e.g., 200000 for a 200K token threshold).+ docs: Numeric threshold for the summed matching usage values.type: doublecaseSensitive:- docs: Whether the regex pattern matching is case-sensitive. Default is false (case-insensitive matching).+ docs: Whether the usage-detail regex is case-sensitive.type: boolean+ PricingTierAttributeCondition:+ docs: Condition that matches any configured value for a top-level observation attribute.+ properties:+ source:+ docs: Observation attribute object evaluated by this condition.+ type: PricingTierAttributeSource+ key:+ docs: Exact top-level attribute key.+ type: string+ operator:+ docs: Membership operator.+ type: literal<"in">+ values:+ docs: Accepted string attribute values. At least one value is required.+ type: list<string>++ PricingTierAttributeSource:+ enum:+ - model_parameters+ - metadata+PricingTier:docs: |- Pricing tier definition with conditional pricing based on usage thresholds.+ Pricing tier definition with conditional pricing based on usage thresholds or observation attributes.- Pricing tiers enable accurate cost tracking for LLM providers that charge different rates based on usage patterns.- For example, some providers charge higher rates when context size exceeds certain thresholds.+ Pricing tiers enable accurate cost tracking for LLM providers that charge different rates based on usage patterns or request attributes.+ For example, some providers charge higher rates when context size exceeds certain thresholds or when an accelerated service tier is selected.How tier matching works:1. Tiers are evaluated in ascending priority order (priority 1 before priority 2, etc.)@@ -988,7 +989,7 @@ types:The default tier must have an empty array (conditions=[]).Conditional tiers should define one or more conditions that specify when this tier's pricing applies.- Each condition specifies a regex pattern, operator, and threshold value for matching against usage details.+ Conditions can compare summed matching usage details to a numeric threshold, or exactly match a top-level model parameter or metadata value.type: list<PricingTierCondition>prices:docs: |diff --git a/packages/shared/scripts/seeder/scenarios/custom-models.ts b/packages/shared/scripts/seeder/scenarios/custom-models.tsindex 2771f75b8a..06ee84a0d0 100644--- a/packages/shared/scripts/seeder/scenarios/custom-models.ts+++ b/packages/shared/scripts/seeder/scenarios/custom-models.ts@@ -312,8 +312,11 @@ const run = async (const startTime =traceTimestamp + index * 1200 + jitter(ctx.seed, index, 200);const usage = model ? model.usage : UNPRICED_USAGE;- const defaultPrices = model?.tiers.find((tier) => tier.isDefault)?.prices;- const costs = defaultPrices ? costFor(usage, defaultPrices) : null;+ const defaultTier = model?.tiers.find((tier) => tier.isDefault);+ const costs = defaultTier ? costFor(usage, defaultTier.prices) : null;+ const internalModelId = model+ ? (writtenModelIds.get(model.key) ?? null)+ : null;return createObservation({id: `${traceId}-o${suffix}`,@@ -331,9 +334,12 @@ const run = async (input: JSON.stringify({ prompt: "Summarise the call." }),output: "The caller asked about pricing.",provided_model_name: model ? model.modelName : UNPRICED_MODEL_NAME,- internal_model_id: model- ? (writtenModelIds.get(model.key) ?? null)- : null,+ internal_model_id: internalModelId,+ usage_pricing_tier_id:+ internalModelId && defaultTier+ ? `tier-${internalModelId}-${defaultTier.priority}`+ : null,+ usage_pricing_tier_name: defaultTier?.name ?? null,provided_usage_details: usage,usage_details: usage,provided_cost_details: {},diff --git a/packages/shared/src/features/model-pricing/validation.ts b/packages/shared/src/features/model-pricing/validation.tsindex f41c01e6ce..763af9fdb4 100644--- a/packages/shared/src/features/model-pricing/validation.ts+++ b/packages/shared/src/features/model-pricing/validation.ts@@ -38,33 +38,48 @@ export function validateRegexPattern(pattern: string): void {}}-/**- * Pricing tier condition schema (shared across API and tRPC)- */-export const PricingTierConditionSchema = z.object({- usageDetailPattern: z- .string()- .min(1, "Pattern cannot be empty")- .max(200, "Pattern exceeds maximum length of 200 characters")- .refine(- (pattern) => {- try {- validateRegexPattern(pattern);- return true;- } catch {- return false;- }- },- {- message:- "Invalid regex pattern: must be valid regex and not cause catastrophic backtracking",- },- ),+const UsageDetailPatternSchema = z+ .string()+ .min(1, "Pattern cannot be empty")+ .max(200, "Pattern exceeds maximum length of 200 characters")+ .refine(+ (pattern) => {+ try {+ validateRegexPattern(pattern);+ return true;+ } catch {+ return false;+ }+ },+ {+ message:+ "Invalid regex pattern: must be valid regex and not cause catastrophic backtracking",+ },+ );++export const PricingTierUsageConditionSchema = z.object({+ usageDetailPattern: UsageDetailPatternSchema,operator: z.enum(["gt", "gte", "lt", "lte", "eq", "neq"]),value: z.number().nonnegative(),caseSensitive: z.boolean().default(false),});+export const PricingTierAttributeConditionSchema = z.object({+ source: z.enum(["model_parameters", "metadata"]),+ key: z.string().min(1, "Key cannot be empty").max(200),+ operator: z.literal("in"),+ values: z.array(z.string().min(1, "Value cannot be empty")).min(1),+});++/**+ * Pricing-specific condition union. Usage conditions preserve their established+ * persisted shape; observation attributes use an explicit exact-match shape.+ */+export const PricingTierConditionSchema = z.union([+ PricingTierUsageConditionSchema,+ PricingTierAttributeConditionSchema,+]);+export type PricingTierCondition = z.infer<typeof PricingTierConditionSchema>;/**@@ -194,11 +209,15 @@ export function validatePricingTiers(return { valid: false, error: "Pricing tier names must be unique" };}- // Validate all conditions have valid regex patterns+ // Validate all usage-detail conditions have valid regex patternsfor (const tier of tiers) {for (const condition of tier.conditions) {+ const pattern =+ "usageDetailPattern" in condition ? condition.usageDetailPattern : null;+ if (pattern === null) continue;+try {- validateRegexPattern(condition.usageDetailPattern);+ validateRegexPattern(pattern);} catch (error) {return {valid: false,diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.modelParameters.test.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.modelParameters.test.tsnew file mode 100644index 0000000000..a81903e074--- /dev/null+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.modelParameters.test.ts@@ -0,0 +1,170 @@+import { describe, expect, it } from "vitest";++import {+ OtelIngestionProcessor,+ type ResourceSpan,+} from "./OtelIngestionProcessor";++describe("OtelIngestionProcessor model parameters", () => {+ it("retains the service tier from Vercel AI SDK spans", () => {+ const batch: ResourceSpan[] = [+ {+ scopeSpans: [+ {+ scope: { name: "ai", version: "7.0.0" },+ spans: [+ {+ traceId: Buffer.from("0123456789abcdef0123456789abcdef", "hex"),+ spanId: Buffer.from("0123456789abcdef", "hex"),+ name: "chat gpt-5.5",+ kind: 3,+ startTimeUnixNano: "1752384000000000000",+ endTimeUnixNano: "1752384001000000000",+ attributes: [+ {+ key: "gen_ai.operation.name",+ value: { stringValue: "chat" },+ },+ {+ key: "gen_ai.request.model",+ value: { stringValue: "gpt-5.5" },+ },+ {+ key: "gen_ai.request.service_tier",+ value: { stringValue: "priority" },+ },+ ],+ status: {},+ },+ ],+ },+ ],+ },+ ];++ const events = new OtelIngestionProcessor({+ projectId: "project-1",+ publicKey: "pk-test",+ sdkName: "ai",+ sdkVersion: "7.0.0",+ }).processToEvent(batch);++ expect(events).toHaveLength(1);+ expect(events[0].modelParameters).toMatchObject({+ service_tier: "priority",+ });+ });++ it("falls back to the OpenAI service tier in Vercel provider metadata", () => {+ const batch: ResourceSpan[] = [+ {+ scopeSpans: [+ {+ scope: { name: "ai", version: "7.0.0" },+ spans: [+ {+ traceId: Buffer.from("0123456789abcdef0123456789abcdef", "hex"),+ spanId: Buffer.from("0123456789abcdef", "hex"),+ name: "chat gpt-5.5",+ kind: 3,+ startTimeUnixNano: "1752384000000000000",+ endTimeUnixNano: "1752384001000000000",+ attributes: [+ {+ key: "gen_ai.operation.name",+ value: { stringValue: "chat" },+ },+ {+ key: "gen_ai.request.model",+ value: { stringValue: "gpt-5.5" },+ },+ {+ key: "ai.response.providerMetadata",+ value: {+ stringValue: JSON.stringify({+ openai: { serviceTier: "priority" },+ }),+ },+ },+ ],+ status: {},+ },+ ],+ },+ ],+ },+ ];++ const events = new OtelIngestionProcessor({+ projectId: "project-1",+ publicKey: "pk-test",+ sdkName: "ai",+ sdkVersion: "7.0.0",+ }).processToEvent(batch);++ expect(events).toHaveLength(1);+ expect(events[0].modelParameters).toMatchObject({+ service_tier: "priority",+ });+ });++ it("merges the Vercel provider service tier into explicit model parameters", () => {+ const batch: ResourceSpan[] = [+ {+ scopeSpans: [+ {+ scope: { name: "ai", version: "7.0.0" },+ spans: [+ {+ traceId: Buffer.from("0123456789abcdef0123456789abcdef", "hex"),+ spanId: Buffer.from("0123456789abcdef", "hex"),+ name: "chat gpt-5.5",+ kind: 3,+ startTimeUnixNano: "1752384000000000000",+ endTimeUnixNano: "1752384001000000000",+ attributes: [+ {+ key: "gen_ai.operation.name",+ value: { stringValue: "chat" },+ },+ {+ key: "gen_ai.request.model",+ value: { stringValue: "gpt-5.5" },+ },+ {+ key: "langfuse.observation.model.parameters",+ value: {+ stringValue: JSON.stringify({ temperature: 0.5 }),+ },+ },+ {+ key: "ai.response.providerMetadata",+ value: {+ stringValue: JSON.stringify({+ openai: { serviceTier: "priority" },+ }),+ },+ },+ ],+ status: {},+ },+ ],+ },+ ],+ },+ ];++ const events = new OtelIngestionProcessor({+ projectId: "project-1",+ publicKey: "pk-test",+ sdkName: "ai",+ sdkVersion: "7.0.0",+ }).processToEvent(batch);++ expect(events).toHaveLength(1);+ expect(events[0].modelParameters).toMatchObject({+ temperature: 0.5,+ service_tier: "priority",+ });+ });+});diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.tsindex d97a0d6b83..5d90322488 100644--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts@@ -2436,20 +2436,25 @@ export class OtelIngestionProcessor {attributes: Record<string, unknown>,instrumentationScopeName: string,): Record<string, unknown> {+ let explicitModelParameters: Record<string, unknown> | undefined;if (attributes[LangfuseOtelSpanAttributes.OBSERVATION_MODEL_PARAMETERS]) {try {- return this.sanitizeModelParams(+ explicitModelParameters = this.sanitizeModelParams(JSON.parse(attributes[LangfuseOtelSpanAttributes.OBSERVATION_MODEL_PARAMETERS] as string,),- );+ ) as Record<string, unknown>;} catch {// Fallthrough}}+ if (explicitModelParameters && instrumentationScopeName !== "ai") {+ return explicitModelParameters;+ }+// Genkitif (instrumentationScopeName === "genkit-tracer") {const genkitConfig =@@ -2463,6 +2468,11 @@ export class OtelIngestionProcessor {// Vercel AI SDKif (instrumentationScopeName === "ai") {+ const providerMetadata = this.parseJsonPayload(+ attributes["ai.response.providerMetadata"],+ );+ const responseServiceTier = providerMetadata?.openai?.serviceTier;+return {maxSteps:"ai.settings.maxSteps" in attributes@@ -2500,9 +2510,18 @@ export class OtelIngestionProcessor {"gen_ai.request.temperature" in attributes? (attributes["gen_ai.request.temperature"]?.toString() ?? null): null,+ service_tier:+ typeof responseServiceTier === "string"+ ? responseServiceTier+ : "gen_ai.request.service_tier" in attributes+ ? (attributes["gen_ai.request.service_tier"]?.toString() ?? null)+ : null,+ ...explicitModelParameters,};}+ if (explicitModelParameters) return explicitModelParameters;+if (attributes["llm.invocation_parameters"]) {try {return this.sanitizeModelParams(diff --git a/packages/shared/src/server/pricing-tiers/index.ts b/packages/shared/src/server/pricing-tiers/index.tsindex 31ae589b33..f59c14a44c 100644--- a/packages/shared/src/server/pricing-tiers/index.ts+++ b/packages/shared/src/server/pricing-tiers/index.ts@@ -3,6 +3,10 @@* Export validation, types, and other utilities for use across the codebase*/-export type { PricingTierMatchResult, PricingTierWithPrices } from "./types";+export type {+ PricingTierMatchAttributes,+ PricingTierMatchResult,+ PricingTierWithPrices,+} from "./types";-export { matchPricingTier } from "./matcher";+export { hasPricingTierUsageDetails, matchPricingTier } from "./matcher";diff --git a/packages/shared/src/server/pricing-tiers/matcher.ts b/packages/shared/src/server/pricing-tiers/matcher.tsindex d4987bc978..50bb795037 100644--- a/packages/shared/src/server/pricing-tiers/matcher.ts+++ b/packages/shared/src/server/pricing-tiers/matcher.ts@@ -1,5 +1,9 @@import { PricingTierCondition } from "../../features/model-pricing";-import type { PricingTierMatchResult, PricingTierWithPrices } from "./types";+import type {+ PricingTierMatchAttributes,+ PricingTierMatchResult,+ PricingTierWithPrices,+} from "./types";/*** Pricing tier matching algorithm@@ -7,14 +11,30 @@ import type { PricingTierMatchResult, PricingTierWithPrices } from "./types";*//**- * Evaluates a single condition against usage details- * Sums all usage_details keys matching the pattern and compares to threshold+ * Evaluates a single condition against usage details or observation attributes.+ * Usage conditions sum keys matching the regex; attribute conditions compare an+ * exact top-level key against the configured values.*/function evaluateCondition(condition: PricingTierCondition,usageDetails: Record<string, number>,+ attributes: PricingTierMatchAttributes,): boolean {try {+ if ("source" in condition) {+ const values =+ condition.source === "model_parameters"+ ? attributes.modelParameters+ : attributes.metadata;++ if (!Object.prototype.hasOwnProperty.call(values ?? {}, condition.key)) {+ return false;+ }++ const attributeValue = values?.[condition.key];+ return condition.values.includes(attributeValue ?? "");+ }+// Build regex with case sensitivity flagconst flags = condition.caseSensitive ? "" : "i";const regex = new RegExp(condition.usageDetailPattern, flags);@@ -59,6 +79,7 @@ function evaluateCondition(function evaluateConditions(conditions: PricingTierCondition[],usageDetails: Record<string, number>,+ attributes: PricingTierMatchAttributes,): boolean {// Empty conditions should never match (except for default tiers)if (conditions.length === 0) {@@ -67,10 +88,16 @@ function evaluateConditions(// All conditions must pass (AND logic)return conditions.every((condition) =>- evaluateCondition(condition, usageDetails),+ evaluateCondition(condition, usageDetails, attributes),);}+export function hasPricingTierUsageDetails(+ usageDetails: Record<string, number> | undefined,+): boolean {+ return Object.keys(usageDetails ?? {}).length > 0;+}+/*** Matches usage details against pricing tiers and returns applicable tier with prices*@@ -83,11 +110,13 @@ function evaluateConditions(** @param tiers - Array of pricing tiers with prices* @param usageDetails - Usage details from the observation (e.g., { input_tokens: 250000, output_tokens: 2000 })+ * @param attributes - Top-level model parameters and metadata from the observation* @returns Matched tier with prices, or null if no match and no default*/export function matchPricingTier(tiers: PricingTierWithPrices[],usageDetails: Record<string, number>,+ attributes: PricingTierMatchAttributes = {},): PricingTierMatchResult | null {// 1. Filter and sort non-default tiers by priority (ascending)const sortedTiers = tiers@@ -96,7 +125,7 @@ export function matchPricingTier(// 2. Try to match each tier in priority orderfor (const tier of sortedTiers) {- if (evaluateConditions(tier.conditions, usageDetails)) {+ if (evaluateConditions(tier.conditions, usageDetails, attributes)) {return {pricingTierId: tier.id,pricingTierName: tier.name,diff --git a/packages/shared/src/server/pricing-tiers/types.ts b/packages/shared/src/server/pricing-tiers/types.tsindex 2b58f03a03..bc6f1c46ab 100644--- a/packages/shared/src/server/pricing-tiers/types.ts+++ b/packages/shared/src/server/pricing-tiers/types.ts@@ -15,6 +15,11 @@ export type PricingTierMatchResult = {prices: Record<string, Decimal>; // usageType -> price};+export type PricingTierMatchAttributes = {+ modelParameters?: Record<string, string>;+ metadata?: Record<string, string>;+};+/*** Pricing tier with prices included (from database)*/diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.ymlindex 2046357aec..74649eda70 100644--- a/web/public/generated/api/openapi.yml+++ b/web/public/generated/api/openapi.yml@@ -10579,7 +10579,7 @@ components:supported)- Tiered pricing: Multiple pricing tiers with conditional matching based- on usage patterns+ on usage patterns or observation attributesThe pricing tiers approach is recommended for models with usage-based@@ -10667,14 +10667,14 @@ components:$ref: '#/components/schemas/PricingTier'description: >-Array of pricing tiers with conditional pricing based on usage- thresholds.+ thresholds or observation attributes.Pricing tiers enable accurate cost tracking for models that chargedifferent rates based on usage patterns- (e.g., different rates for high-volume usage, large context windows,- or cached tokens).+ or request attributes (e.g., high-volume usage, large context+ windows, cached tokens, or service tiers).Each model must have exactly one default tier (isDefault=true,@@ -10711,104 +10711,87 @@ components:- pricePricingTierCondition:title: PricingTierCondition- type: object+ oneOf:+ - $ref: '#/components/schemas/PricingTierUsageCondition'+ - $ref: '#/components/schemas/PricingTierAttributeCondition'description: >-- Condition for matching a pricing tier based on usage details. Used to- implement tiered pricing models where costs vary based on usage- thresholds.--- How it works:-- 1. The regex pattern matches against usage detail keys (e.g.,- "input_tokens", "input_cached")-- 2. Values of all matching keys are summed together+ Condition for matching a pricing tier against usage details or+ observation attributes.- 3. The sum is compared against the threshold value using the specified- operator- 4. All conditions in a tier must be met (AND logic) for the tier to- match--- Common use cases:-- - Threshold-based pricing: Match when accumulated usage exceeds a- certain amount-- - Usage-type-specific pricing: Different rates for cached vs non-cached- tokens, or input vs output-- - Volume-based pricing: Different rates based on total request or token- count+ Usage-detail conditions treat usageDetailPattern as a regex, sum all+ matching usage values, and compare the sum to the numeric value.+ Model-parameter and metadata conditions match an exact top-level key+ against one or more string values.+ PricingTierUsageCondition:+ title: PricingTierUsageCondition+ type: object+ description: Condition that sums usage details whose keys match a regex.properties:usageDetailPattern:type: string- description: >-- Regex pattern to match against usage detail keys. All matching keys'- values are summed for threshold comparison.--- Examples:-- - "^input" matches "input", "input_tokens", "input_cached", etc.-- - "^(input|prompt)" matches both "input_tokens" and "prompt_tokens"-- - "_cache$" matches "input_cache", "output_cache", etc.--- The pattern is case-insensitive by default. If no keys match, the- sum is treated as zero.+ description: Regex pattern matched against usage detail keys.operator:$ref: '#/components/schemas/PricingTierOperator'- description: >-- Comparison operator to apply between the summed value and the- threshold.--- - gt: greater than (sum > threshold)-- - gte: greater than or equal (sum >= threshold)-- - lt: less than (sum < threshold)-- - lte: less than or equal (sum <= threshold)-- - eq: equal (sum == threshold)-- - neq: not equal (sum != threshold)value:type: numberformat: double- description: >-- Threshold value for comparison. For token-based pricing, this is- typically the token count threshold (e.g., 200000 for a 200K token- threshold).+ description: Numeric threshold for the summed matching usage values.caseSensitive:type: boolean- description: >-- Whether the regex pattern matching is case-sensitive. Default is- false (case-insensitive matching).+ description: Whether the usage-detail regex is case-sensitive.required:- usageDetailPattern- operator- value- caseSensitive+ PricingTierAttributeCondition:+ title: PricingTierAttributeCondition+ type: object+ description: >-+ Condition that matches any configured value for a top-level observation+ attribute.+ properties:+ source:+ $ref: '#/components/schemas/PricingTierAttributeSource'+ description: Observation attribute object evaluated by this condition.+ key:+ type: string+ description: Exact top-level attribute key.+ operator:+ type: string+ const: in+ description: Membership operator.+ values:+ type: array+ items:+ type: string+ description: Accepted string attribute values. At least one value is required.+ required:+ - source+ - key+ - operator+ - values+ PricingTierAttributeSource:+ title: PricingTierAttributeSource+ type: string+ enum:+ - model_parameters+ - metadataPricingTier:title: PricingTiertype: objectdescription: >-Pricing tier definition with conditional pricing based on usage- thresholds.+ thresholds or observation attributes.Pricing tiers enable accurate cost tracking for LLM providers that- charge different rates based on usage patterns.+ charge different rates based on usage patterns or request attributes.For example, some providers charge higher rates when context size- exceeds certain thresholds.+ exceeds certain thresholds or when an accelerated service tier is+ selected.How tier matching works:@@ -10999,8 +10982,9 @@ components:when this tier's pricing applies.- Each condition specifies a regex pattern, operator, and threshold- value for matching against usage details.+ Conditions can compare summed matching usage details to a numeric+ threshold, or exactly match a top-level model parameter or metadata+ value.prices:type: objectadditionalProperties:diff --git a/web/src/__tests__/server/model-pricing-tiers.servertest.ts b/web/src/__tests__/server/model-pricing-tiers.servertest.tsindex da96075ae8..612ed35684 100644--- a/web/src/__tests__/server/model-pricing-tiers.servertest.ts+++ b/web/src/__tests__/server/model-pricing-tiers.servertest.ts@@ -105,6 +105,64 @@ describe("validation methods", () => {expect(result.valid).toBe(true);});+ it("accepts attribute membership conditions", () => {+ const result = PricingTierInputSchema.safeParse({+ name: "Fast mode",+ isDefault: false,+ priority: 1,+ conditions: [+ {+ source: "model_parameters",+ key: "service_tier",+ operator: "in",+ values: ["fast", "priority"],+ },+ ],+ prices: { input: 6.0, output: 30.0 },+ });++ expect(result.success).toBe(true);+ });++ it("rejects empty attribute membership conditions", () => {+ const result = PricingTierInputSchema.safeParse({+ name: "Fast mode",+ isDefault: false,+ priority: 1,+ conditions: [+ {+ source: "model_parameters",+ key: "service_tier",+ operator: "in",+ values: [],+ },+ ],+ prices: { input: 6.0, output: 30.0 },+ });++ expect(result.success).toBe(false);+ });++ it("rejects generic filter-shaped pricing conditions", () => {+ const result = PricingTierInputSchema.safeParse({+ name: "Priority",+ isDefault: false,+ priority: 1,+ conditions: [+ {+ column: "model_parameters",+ type: "stringObject",+ key: "service_tier",+ operator: "=",+ value: "priority",+ },+ ],+ prices: { input: 6.0, output: 30.0 },+ });++ expect(result.success).toBe(false);+ });+it("should reject empty tier array", () => {const result = validatePricingTiers([]);expect(result.valid).toBe(false);diff --git a/web/src/features/models/components/PricingSection/components/TierConditionsEditor.tsx b/web/src/features/models/components/PricingSection/components/TierConditionsEditor.tsxindex a4abf1933b..2ec91b216e 100644--- a/web/src/features/models/components/PricingSection/components/TierConditionsEditor.tsx+++ b/web/src/features/models/components/PricingSection/components/TierConditionsEditor.tsx@@ -20,6 +20,7 @@ import {} from "@/src/components/ui/select";import type { UseFormReturn } from "react-hook-form";import type { FormUpsertModel } from "@/src/features/models/validation";+import type { PricingTierCondition } from "@langfuse/shared";type TierConditionsEditorProps = {tierIndex: number;@@ -32,11 +33,18 @@ export function TierConditionsEditor({tierIndex,form,}: TierConditionsEditorProps) {- const { fields, append, remove } = useFieldArray({+ const { fields, append, remove, update } = useFieldArray({control: form.control,name: `pricingTiers.${tierIndex}.conditions`,});+ const replaceCondition = (+ conditionIndex: number,+ condition: PricingTierCondition,+ ) => {+ update(conditionIndex, condition);+ };+return (<div className="space-y-3"><div className="flex items-center justify-between">@@ -66,107 +74,199 @@ export function TierConditionsEditor({</div>)}- {fields.map((condition, conditionIndex) => (- <div key={condition.id} className="space-y-3 rounded-lg border p-3">- <div className="flex items-center justify-between">- <span className="text-sm font-bold">- Condition {conditionIndex + 1}- </span>- <Button- type="button"- variant="ghost"- size="sm"- onClick={() => remove(conditionIndex)}- >- <Trash2 className="h-4 w-4" />- </Button>- </div>+ {fields.map((condition, conditionIndex) => {+ const isUsageCondition = "usageDetailPattern" in condition;+ const source = isUsageCondition ? "usage_details" : condition.source;- {/* Pattern */}- <FormField- control={form.control}- name={`pricingTiers.${tierIndex}.conditions.${conditionIndex}.usageDetailPattern`}- render={({ field }) => (- <FormItem>- <FormLabel>Usage Detail Pattern (Regex)</FormLabel>- <FormControl>- <Input {...field} placeholder="^input" />- </FormControl>- <FormDescription>- Match usage type keys (e.g., ^input, .*cache.*, output_tokens)- </FormDescription>- <FormMessage />- </FormItem>- )}- />+ return (+ <div key={condition.id} className="space-y-3 rounded-lg border p-3">+ <div className="flex items-center justify-between">+ <span className="text-sm font-bold">+ Condition {conditionIndex + 1}+ </span>+ <Button+ type="button"+ variant="ghost"+ size="sm"+ onClick={() => remove(conditionIndex)}+ >+ <Trash2 className="h-4 w-4" />+ </Button>+ </div>- {/* Operator + Value */}- <div className="grid grid-cols-2 gap-2">- <FormField- control={form.control}- name={`pricingTiers.${tierIndex}.conditions.${conditionIndex}.operator`}- render={({ field }) => (- <FormItem>- <FormLabel>Operator</FormLabel>- <Select value={field.value} onValueChange={field.onChange}>- <SelectTrigger>- <SelectValue />- </SelectTrigger>- <SelectContent>- <SelectItem value="gt">> (greater than)</SelectItem>- <SelectItem value="gte">- >= (greater or equal)- </SelectItem>- <SelectItem value="lt">< (less than)</SelectItem>- <SelectItem value="lte"><= (less or equal)</SelectItem>- <SelectItem value="eq">= (equals)</SelectItem>- <SelectItem value="neq">!= (not equals)</SelectItem>- </SelectContent>- </Select>- <FormMessage />- </FormItem>- )}- />+ <FormItem>+ <FormLabel>Source</FormLabel>+ <Select+ value={source}+ onValueChange={(nextSource) => {+ if (nextSource === "usage_details") {+ replaceCondition(conditionIndex, {+ usageDetailPattern: "",+ operator: "gt",+ value: 0,+ caseSensitive: false,+ });+ } else {+ replaceCondition(conditionIndex, {+ source: nextSource as "model_parameters" | "metadata",+ key: "",+ operator: "in",+ values: [""],+ });+ }+ }}+ >+ <SelectTrigger>+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ <SelectItem value="usage_details">Usage details</SelectItem>+ <SelectItem value="model_parameters">+ Model parameters+ </SelectItem>+ <SelectItem value="metadata">Metadata</SelectItem>+ </SelectContent>+ </Select>+ </FormItem><FormFieldcontrol={form.control}- name={`pricingTiers.${tierIndex}.conditions.${conditionIndex}.value`}+ name={`pricingTiers.${tierIndex}.conditions.${conditionIndex}.${isUsageCondition ? "usageDetailPattern" : "key"}`}render={({ field }) => (<FormItem>- <FormLabel>Value</FormLabel>+ <FormLabel>+ {isUsageCondition+ ? "Usage detail key pattern (Regex)"+ : "Top-level key"}+ </FormLabel><FormControl><Input- type="number"{...field}- onChange={(e) =>- field.onChange(parseFloat(e.target.value))+ value={field.value as string}+ placeholder={+ isUsageCondition+ ? "^input"+ : source === "model_parameters"+ ? "service_tier"+ : "model_provider"}/></FormControl>+ <FormDescription>+ {isUsageCondition+ ? "Match and sum usage keys such as input or cached tokens."+ : "Match this key exactly; nested paths are not supported."}+ </FormDescription><FormMessage /></FormItem>)}/>- </div>- {/* Case Sensitive */}- <FormField- control={form.control}- name={`pricingTiers.${tierIndex}.conditions.${conditionIndex}.caseSensitive`}- render={({ field }) => (- <FormItem className="flex items-center gap-2">- <FormControl>- <Checkbox- checked={field.value}- onCheckedChange={field.onChange}- />- </FormControl>- <FormLabel className="mt-0!">Case sensitive</FormLabel>- </FormItem>+ {/* Operator + Value */}+ <div className="grid grid-cols-2 gap-2">+ {isUsageCondition ? (+ <FormField+ control={form.control}+ name={`pricingTiers.${tierIndex}.conditions.${conditionIndex}.operator`}+ render={({ field }) => (+ <FormItem>+ <FormLabel>Operator</FormLabel>+ <Select+ value={field.value}+ onValueChange={field.onChange}+ >+ <SelectTrigger>+ <SelectValue />+ </SelectTrigger>+ <SelectContent>+ <SelectItem value="gt">+ > (greater than)+ </SelectItem>+ <SelectItem value="gte">+ >= (greater or equal)+ </SelectItem>+ <SelectItem value="lt">< (less than)</SelectItem>+ <SelectItem value="lte">+ <= (less or equal)+ </SelectItem>+ <SelectItem value="eq">= (equals)</SelectItem>+ <SelectItem value="neq">!= (not equals)</SelectItem>+ </SelectContent>+ </Select>+ <FormMessage />+ </FormItem>+ )}+ />+ ) : (+ <FormItem>+ <FormLabel>Operator</FormLabel>+ <Input disabled value="in (any of)" />+ </FormItem>+ )}++ <FormField+ control={form.control}+ name={`pricingTiers.${tierIndex}.conditions.${conditionIndex}.${isUsageCondition ? "value" : "values"}`}+ render={({ field }) => (+ <FormItem>+ <FormLabel>+ {isUsageCondition ? "Value" : "Values"}+ </FormLabel>+ <FormControl>+ <Input+ type={isUsageCondition ? "number" : "text"}+ {...field}+ value={+ !isUsageCondition+ ? ((field.value as string[]) ?? []).join(", ")+ : (field.value as string | number)+ }+ placeholder={+ !isUsageCondition ? "fast, priority" : undefined+ }+ onChange={(e) =>+ field.onChange(+ isUsageCondition+ ? parseFloat(e.target.value)+ : e.target.value+ .split(",")+ .map((value) => value.trim()),+ )+ }+ />+ </FormControl>+ <FormMessage />+ {!isUsageCondition && (+ <FormDescription>+ Comma-separated exact values. All other tier conditions+ still apply.+ </FormDescription>+ )}+ </FormItem>+ )}+ />+ </div>++ {isUsageCondition && (+ <FormField+ control={form.control}+ name={`pricingTiers.${tierIndex}.conditions.${conditionIndex}.caseSensitive`}+ render={({ field }) => (+ <FormItem className="flex items-center gap-2">+ <FormControl>+ <Checkbox+ checked={field.value ?? false}+ onCheckedChange={field.onChange}+ />+ </FormControl>+ <FormLabel className="mt-0!">Case sensitive</FormLabel>+ </FormItem>+ )}+ />)}- />- </div>- ))}+ </div>+ );+ })}</div>);}diff --git a/web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialog.clienttest.tsx b/web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialog.clienttest.tsxindex 3b61e8d4cd..878f1657eb 100644--- a/web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialog.clienttest.tsx+++ b/web/src/features/models/components/UpsertModelFormDialog/UpsertModelFormDialog.clienttest.tsx@@ -71,6 +71,22 @@ const longContextTier = {prices: { text_input: 0.000008, text_output: 0.000032, audio: 0.00006 },};+const fastModeTier = {+ id: "tier-priority",+ name: "Fast mode",+ isDefault: false,+ priority: 1,+ conditions: [+ {+ source: "model_parameters" as const,+ key: "service_tier",+ operator: "in" as const,+ values: ["fast", "priority"],+ },+ ],+ prices: { text_input: 0.0000125, text_output: 0.000075, audio: 0.00009 },+};+const modelData = (pricingTiers: GetModelResult["pricingTiers"],): GetModelResult => ({@@ -188,7 +204,14 @@ describe("UpsertModelFormDialog price editor", () => {name: "Long context",isDefault: false,priority: 1,- conditions: longContextTier.conditions,+ conditions: [+ {+ usageDetailPattern: "^text_input",+ operator: "gt",+ value: 128000,+ caseSensitive: false,+ },+ ],// The rename must not zero this tier's price for the renamed key.prices: {text_input_cached: 0.000008,@@ -199,6 +222,16 @@ describe("UpsertModelFormDialog price editor", () => {]);});+ it("preserves model parameter membership conditions when saving", async () => {+ openEditDialog([defaultTier, fastModeTier]);+ submit();++ await waitFor(() => expect(upsertMutateAsync).toHaveBeenCalledTimes(1));+ expect(+ upsertMutateAsync.mock.calls[0][0].pricingTiers[1].conditions,+ ).toEqual(fastModeTier.conditions);+ });+it("renaming a clone keeps a custom match pattern but follows a generated one", () => {const custom = String.raw`(?i)^(gpt-realtime-2\.\d+)$`;openCloneDialog(custom);diff --git a/web/src/features/models/components/test-match/StringMapEditor.clienttest.tsx b/web/src/features/models/components/test-match/StringMapEditor.clienttest.tsxnew file mode 100644index 0000000000..49aec70c7c--- /dev/null+++ b/web/src/features/models/components/test-match/StringMapEditor.clienttest.tsx@@ -0,0 +1,55 @@+import { fireEvent, render, screen } from "@testing-library/react";+import { useState } from "react";++import { StringMapEditor } from "./StringMapEditor";++function TestEditor({+ initialEntries = {},+}: {+ initialEntries?: Record<string, string>;+}) {+ const [entries, setEntries] = useState<Array<[string, string]>>(() =>+ Object.entries(initialEntries),+ );++ return (+ <StringMapEditor+ title="Model Parameters"+ description="Pricing attributes"+ entries={entries}+ onChange={setEntries}+ />+ );+}++describe("StringMapEditor", () => {+ it("edits exact string keys and values", () => {+ render(<TestEditor />);++ fireEvent.click(screen.getByRole("button", { name: "Add Attribute" }));++ fireEvent.change(screen.getByPlaceholderText("e.g. service_tier"), {+ target: { value: "service_tier" },+ });+ fireEvent.change(screen.getByPlaceholderText("e.g. priority"), {+ target: { value: "priority" },+ });++ expect(screen.getByDisplayValue("service_tier")).toBeInTheDocument();+ expect(screen.getByDisplayValue("priority")).toBeInTheDocument();+ });++ it("preserves the row while replacing its key", () => {+ render(<TestEditor initialEntries={{ service_tier: "priority" }} />);++ const keyInput = screen.getByDisplayValue("service_tier");+ fireEvent.change(keyInput, { target: { value: "" } });++ expect(screen.getByDisplayValue("priority")).toBeInTheDocument();++ fireEvent.change(keyInput, { target: { value: "service_class" } });++ expect(screen.getByDisplayValue("service_class")).toBeInTheDocument();+ expect(screen.getByDisplayValue("priority")).toBeInTheDocument();+ });+});diff --git a/web/src/features/models/components/test-match/StringMapEditor.tsx b/web/src/features/models/components/test-match/StringMapEditor.tsxnew file mode 100644index 0000000000..218e7e397c--- /dev/null+++ b/web/src/features/models/components/test-match/StringMapEditor.tsx@@ -0,0 +1,82 @@+import { PlusCircle, Trash2 } from "lucide-react";+import { Button } from "@/src/components/ui/button";+import { Input } from "@/src/components/ui/input";++type StringMapEditorProps = {+ title: string;+ description: string;+ entries: Array<[string, string]>;+ onChange: (entries: Array<[string, string]>) => void;+};++export function StringMapEditor({+ title,+ description,+ entries,+ onChange,+}: StringMapEditorProps) {+ const updateRows = (newRows: Array<[string, string]>) => {+ onChange(newRows);+ };++ return (+ <div className="space-y-4">+ <div>+ <div className="pb-2 text-sm font-bold">{title} (optional)</div>+ <div className="text-muted-foreground text-sm">{description}</div>+ </div>++ {entries.length > 0 && (+ <div className="space-y-2 rounded-lg border p-3">+ <div className="grid grid-cols-[1fr_1fr_auto] gap-2 text-sm font-bold">+ <div>Key</div>+ <div>Value</div>+ <div className="w-10" />+ </div>+ {entries.map(([key, value], index) => (+ <div key={index} className="grid grid-cols-[1fr_1fr_auto] gap-2">+ <Input+ placeholder="e.g. service_tier"+ value={key}+ onChange={(event) => {+ const newRows = [...entries];+ newRows[index] = [event.target.value, value];+ updateRows(newRows);+ }}+ />+ <Input+ placeholder="e.g. priority"+ value={value}+ onChange={(event) => {+ const newRows = [...entries];+ newRows[index] = [key, event.target.value];+ updateRows(newRows);+ }}+ />+ <Button+ type="button"+ variant="ghost"+ size="sm"+ onClick={() =>+ updateRows(entries.filter((_, i) => i !== index))+ }+ >+ <Trash2 className="h-4 w-4" />+ </Button>+ </div>+ ))}+ </div>+ )}++ <Button+ type="button"+ variant="ghost"+ onClick={() => updateRows([...entries, ["new_key", ""]])}+ className="w-full"+ >+ <PlusCircle className="mr-2 h-4 w-4" />+ Add Attribute+ </Button>+ </div>+ );+}diff --git a/web/src/features/models/components/test-match/TestModelMatchDialog.tsx b/web/src/features/models/components/test-match/TestModelMatchDialog.tsxindex 0e301829b4..d496275be9 100644--- a/web/src/features/models/components/test-match/TestModelMatchDialog.tsx+++ b/web/src/features/models/components/test-match/TestModelMatchDialog.tsx@@ -12,6 +12,7 @@ import { Input } from "@/src/components/ui/input";import { Button } from "@/src/components/ui/button";import { api } from "@/src/utils/api";import { UsageDetailsEditor } from "./UsageDetailsEditor";+import { StringMapEditor } from "./StringMapEditor";import { MatchedModelCard } from "./MatchedModelCard";import { MatchedTierCard } from "./MatchedTierCard";import { NoMatchDisplay } from "./NoMatchDisplay";@@ -26,6 +27,9 @@ type TestModelMatchDialogProps = {export type { TestModelMatchDialogProps };+const validStringMap = (entries: Array<[string, string]>) =>+ Object.fromEntries(entries.filter(([key]) => key.trim().length > 0));+export function TestModelMatchDialog({projectId,open,@@ -33,7 +37,15 @@ export function TestModelMatchDialog({}: TestModelMatchDialogProps) {const [modelName, setModelName] = useState("");const [usageDetails, setUsageDetails] = useState<Record<string, number>>({});+ const [modelParameterEntries, setModelParameterEntries] = useState<+ Array<[string, string]>+ >([]);+ const [metadataEntries, setMetadataEntries] = useState<+ Array<[string, string]>+ >([]);const [hasSubmitted, setHasSubmitted] = useState(false);+ const modelParameters = validStringMap(modelParameterEntries);+ const metadata = validStringMap(metadataEntries);// Query for match result - only enabled after submitconst { data, isLoading, error, refetch } = api.models.testMatch.useQuery(@@ -41,6 +53,8 @@ export function TestModelMatchDialog({projectId,modelName,usageDetails,+ modelParameters,+ metadata,},{enabled: false, // Manual trigger only@@ -61,6 +75,8 @@ export function TestModelMatchDialog({if (!open) {setModelName("");setUsageDetails({});+ setModelParameterEntries([]);+ setMetadataEntries([]);setHasSubmitted(false);}}, [open]);@@ -101,6 +117,20 @@ export function TestModelMatchDialog({usageDetails={usageDetails}onChange={setUsageDetails}/>++ <StringMapEditor+ title="Model Parameters"+ description="Add top-level model parameters used by pricing tier conditions."+ entries={modelParameterEntries}+ onChange={setModelParameterEntries}+ />++ <StringMapEditor+ title="Metadata"+ description="Add top-level metadata used by pricing tier conditions."+ entries={metadataEntries}+ onChange={setMetadataEntries}+ /></div>{/* Buttons at bottom of left column */}diff --git a/web/src/features/models/fns/toPricingTierInputs.ts b/web/src/features/models/fns/toPricingTierInputs.tsindex 1e1a63d2c7..4dc3e4c622 100644--- a/web/src/features/models/fns/toPricingTierInputs.ts+++ b/web/src/features/models/fns/toPricingTierInputs.ts@@ -14,10 +14,11 @@ export const toPricingTierInputs = (name: tier.name,isDefault: tier.isDefault,priority: priorities[tierIndex],- conditions: tier.conditions.map((condition) => ({- ...condition,- caseSensitive: condition.caseSensitive ?? false,- })),+ conditions: tier.conditions.map((condition) =>+ "usageDetailPattern" in condition+ ? { ...condition, caseSensitive: condition.caseSensitive ?? false }+ : condition,+ ),prices: Object.fromEntries(values.usageTypes.flatMap((row) => {const price = parsePriceInput(tier.prices[row.key]);diff --git a/web/src/features/traces/components/BreakdownTooltip.stories.tsx b/web/src/features/traces/components/BreakdownTooltip.stories.tsxnew file mode 100644index 0000000000..181aa56198--- /dev/null+++ b/web/src/features/traces/components/BreakdownTooltip.stories.tsx@@ -0,0 +1,100 @@+import { expect, waitFor, within } from "storybook/test";++import preview from "../../../../.storybook/preview";+import { BreakdownTooltip } from "./BreakdownTooltip";++const usageDetails = {+ input: 120,+ input_cached_tokens: 20,+ output: 35,+ output_reasoning_tokens: 10,+ total: 185,+};++const costDetails = {+ input: 0.00015,+ input_cached_tokens: 0,+ output: 0.001575,+ output_reasoning_tokens: 0,+ total: 0.001725,+};++const priceSource = {+ projectId: "project-1",+ modelId: "gpt-5.6/priority",+ modelName: "gpt-5.6",+ pricingTierId: "tier-priority",+ pricingTierName: "Priority",+};++const meta = preview.meta({+ component: BreakdownTooltip,+});++export const Usage = meta.story({+ args: {+ details: usageDetails,+ children: <span>185 tokens</span>,+ },+});++export const Cost = meta.story({+ args: {+ details: costDetails,+ children: <span>$0.001725</span>,+ isCost: true,+ },+});++export const CostWithMatchedPricingTier = meta.story({+ args: {+ details: costDetails,+ children: <span>$0.001725</span>,+ isCost: true,+ priceSource,+ },+});++export const AggregatedCost = meta.story({+ args: {+ details: [costDetails, costDetails],+ children: <span>$0.003450</span>,+ isCost: true,+ },+});++export const TestLinksMatchedPricingTier = meta.story({+ name: "(Test) Opens matched pricing tier breakdown",+ args: {+ details: costDetails,+ children: <span>$0.001725</span>,+ isCost: true,+ priceSource,+ },+ play: async ({ canvasElement }) => {+ const canvas = within(canvasElement);+ const trigger = canvas.getByRole("button", { name: "$0.001725" });+ trigger.click();+ await waitFor(() =>+ expect(trigger.getAttribute("data-state")).toMatch(/-open$/),+ );+ await waitFor(() => expect(trigger).toHaveAttribute("aria-describedby"));++ const tooltipId = trigger.getAttribute("aria-describedby");+ const tooltip = tooltipId+ ? canvasElement.ownerDocument.getElementById(tooltipId)+ : null;+ if (!tooltip) throw new Error("Tooltip content was not rendered");++ const content = within(tooltip);+ await expect(content.getByText("Cost breakdown")).toBeInTheDocument();+ await expect(content.getByText("Total cost")).toBeInTheDocument();+ const link = content.getByRole("link");+ await expect(link).toHaveTextContent("Priority");+ await expect(link).toHaveAttribute(+ "href",+ "/project/project-1/settings/models/gpt-5.6%2Fpriority?pricingTier=tier-priority",+ );+ await expect(link).toHaveClass("text-xs", "italic");+ },+});diff --git a/web/src/features/traces/components/BreakdownTooltip.tsx b/web/src/features/traces/components/BreakdownTooltip.tsxindex b746ce6542..58f833e416 100644--- a/web/src/features/traces/components/BreakdownTooltip.tsx+++ b/web/src/features/traces/components/BreakdownTooltip.tsx@@ -6,14 +6,25 @@ import {} from "@/src/components/ui/tooltip";import { useState } from "react";import Decimal from "decimal.js";+import Link from "next/link";import { getMaxDecimals } from "@/src/features/models/fns/getMaxDecimals";import { type Details } from "@/src/features/traces/fns/calculateAggregatedUsage";+import { ExternalLink } from "lucide-react";++export interface PriceSource {+ projectId: string;+ modelId: string;+ modelName: string;+ pricingTierId: string;+ pricingTierName: string;+}interface BreakdownTooltipProps {details: Details | Details[];children: React.ReactNode;isCost?: boolean;pricingTierName?: string;+ priceSource?: PriceSource;}export const BreakdownTooltip = ({@@ -21,6 +32,7 @@ export const BreakdownTooltip = ({children,isCost = false,pricingTierName,+ priceSource,}: BreakdownTooltipProps) => {const [isOpen, setIsOpen] = useState(false);@@ -65,6 +77,18 @@ export const BreakdownTooltip = ({<span className="font-bold">{isCost ? "Cost breakdown" : "Usage breakdown"}</span>++ {isCost && priceSource && (+ <Link+ href={`/project/${encodeURIComponent(priceSource.projectId)}/settings/models/${encodeURIComponent(priceSource.modelId)}?pricingTier=${encodeURIComponent(priceSource.pricingTierId)}`}+ className="text-muted-foreground flex flex-row gap-1 text-xs italic underline-offset-4 hover:underline"+ target="_blank"+ rel="noopener noreferrer"+ >+ {priceSource.pricingTierName} Tier Pricing+ <ExternalLink className="h-3 w-3" />+ </Link>+ )}{Array.isArray(details) && details.length > 0 && (<span className="text-muted-foreground text-xs italic">Aggregate across {details.length}{" "}diff --git a/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader.tsx b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader.tsxindex 8fdbca423b..921365aa8b 100644--- a/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader.tsx+++ b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader.tsx@@ -410,6 +410,24 @@ export const ObservationDetailViewHeader = memo(costDetails={subtreeMetrics?.costDetails ?? observation.costDetails}+ priceSource={+ isGenerationLike(observation.type) &&+ observation.internalModelId &&+ observation.model &&+ observation.usagePricingTierId &&+ observation.usagePricingTierName &&+ Object.keys(observation.providedCostDetails).length === 0 &&+ (!subtreeMetrics ||+ treeNodeTotalCost?.eq(totalCost ?? 0) === true)+ ? {+ projectId,+ modelId: observation.internalModelId,+ modelName: observation.model,+ pricingTierId: observation.usagePricingTierId,+ pricingTierName: observation.usagePricingTierName,+ }+ : undefined+ }/>{subtreeMetrics ? (subtreeMetrics.hasGenerationLike &&diff --git a/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsx b/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsxindex 2cfb8a9eb3..ef6b70261e 100644--- a/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsx+++ b/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsx@@ -5,22 +5,31 @@import { type ObservationType, isGenerationLike } from "@langfuse/shared";import { Badge } from "@/src/components/ui/badge";-import { BreakdownTooltip } from "@/src/features/traces/components/BreakdownTooltip";+import {+ BreakdownTooltip,+ type PriceSource,+} from "@/src/features/traces/components/BreakdownTooltip";import { usdFormatter, formatTokenCounts } from "@/src/utils/numbers";import { InfoIcon } from "lucide-react";export function CostBadge({totalCost,costDetails,+ priceSource,}: {totalCost: number | null;costDetails: Record<string, number> | undefined;+ priceSource?: PriceSource;}) {// Don't show if no cost data or cost is 0if (totalCost == null || totalCost === 0 || !costDetails) return null;return (- <BreakdownTooltip details={costDetails} isCost={true}>+ <BreakdownTooltip+ details={costDetails}+ isCost={true}+ priceSource={priceSource}+ ><Badge variant="tertiary" className="flex items-center gap-1"><span>{usdFormatter(totalCost)}</span><InfoIcon className="h-3 w-3" />diff --git a/web/src/pages/project/[projectId]/settings/models/[modelId].tsx b/web/src/pages/project/[projectId]/settings/models/[modelId].tsxindex d4a5b1925b..5caed359d7 100644--- a/web/src/pages/project/[projectId]/settings/models/[modelId].tsx+++ b/web/src/pages/project/[projectId]/settings/models/[modelId].tsx@@ -35,7 +35,17 @@ import {HoverCardTrigger,} from "@/src/components/ui/hover-card";import { CodeMirrorEditor } from "@/src/components/editor";-import { useEffect } from "react";++const resolvePricingTier = <T extends { id: string }>(+ tiers: T[],+ selectedTierId: string | null,+ defaultTier: T | null,+): T | null => {+ if (selectedTierId) {+ return tiers.find((tier) => tier.id === selectedTierId) ?? defaultTier;+ }+ return defaultTier;+};export default function ModelDetailPage() {const router = useRouter();@@ -59,31 +69,18 @@ export default function ModelDetailPage() {return model.pricingTiers.find((t) => t.isDefault) || model.pricingTiers[0];}, [model?.pricingTiers]);- // State for selected pricing tier - initialize from URL param- const [selectedTierId, setSelectedTierId] = useState<string | null>(- pricingTierParam ?? null,- );-- // Sync with URL parameter when it changes- useEffect(() => {- if (pricingTierParam && model?.pricingTiers) {- const tierExists = model.pricingTiers.some(- (t) => t.id === pricingTierParam,- );- if (tierExists) {- setSelectedTierId(pricingTierParam);- }- }- }, [pricingTierParam, model?.pricingTiers]);+ // Keep user selection local and derive the initial linked tier during render.+ const [selectedTierId, setSelectedTierId] = useState<string | null>(null);// Get the active tier (selected or default)const activeTier = useMemo(() => {if (!model?.pricingTiers) return null;- if (selectedTierId) {- return model.pricingTiers.find((t) => t.id === selectedTierId) || null;- }- return defaultTier;- }, [model?.pricingTiers, selectedTierId, defaultTier]);+ return resolvePricingTier(+ model.pricingTiers,+ selectedTierId ?? pricingTierParam ?? null,+ defaultTier,+ );+ }, [model?.pricingTiers, selectedTierId, pricingTierParam, defaultTier]);const maxDecimals = useMemo(() =>@@ -300,7 +297,7 @@ export default function ModelDetailPage() {</CardContent></Card>- <Card className="col-span-2">+ <Card className="col-span-2 min-w-0"><CardHeader><CardTitle className="flex items-center justify-between"><span>Model observations</span>@@ -315,8 +312,8 @@ export default function ModelDetailPage() {</Button></CardTitle></CardHeader>- <CardContent>- <div className="flex max-h-[calc(100vh-20rem)] flex-col">+ <CardContent className="min-w-0">+ <div className="flex h-[calc(100vh-20rem)] min-h-0 min-w-0 flex-col overflow-hidden"><GenerationsprojectId={projectId}omittedFilter={["model"]}diff --git a/web/src/server/api/routers/models.ts b/web/src/server/api/routers/models.tsindex 0f9c152c7b..23ac04c8f6 100644--- a/web/src/server/api/routers/models.ts+++ b/web/src/server/api/routers/models.ts@@ -18,6 +18,7 @@ import {clearModelCacheForProject,queryClickhouse,findModel,+ hasPricingTierUsageDetails,matchPricingTier,} from "@langfuse/shared/src/server";import { TRPCError } from "@trpc/server";@@ -415,10 +416,13 @@ export const modelRouter = createTRPCRouter({projectId: z.string(),modelName: z.string().min(1),usageDetails: z.record(z.string(), z.number()).optional(),+ modelParameters: z.record(z.string(), z.string()).optional(),+ metadata: z.record(z.string(), z.string()).optional(),}),).query(async ({ input, ctx }) => {- const { projectId, modelName, usageDetails } = input;+ const { projectId, modelName, usageDetails, modelParameters, metadata } =+ input;throwIfNoProjectAccess({session: ctx.session,@@ -436,41 +440,22 @@ export const modelRouter = createTRPCRouter({return { matched: false as const };}- // Step 2: If no usage details provided, return default tier- if (!usageDetails || Object.keys(usageDetails).length === 0) {- const defaultTier = pricingTiers.find((t) => t.isDefault);- if (!defaultTier) {- return { matched: false as const };- }-- return {- matched: true as const,- model: {- id: model.id,- modelName: model.modelName,- matchPattern: model.matchPattern,- projectId: model.projectId,- },- matchedTier: {- id: defaultTier.id,- name: defaultTier.name,- priority: defaultTier.priority,- isDefault: true,- prices: Object.fromEntries(- defaultTier.prices.map((p) => [p.usageType, p.price.toNumber()]),- ),- },- };+ // Step 2: Mirror ingestion: without usage there is no priced observation+ // and therefore no pricing tier match, even for attribute-only tiers.+ if (!hasPricingTierUsageDetails(usageDetails)) {+ return { matched: false as const };}- // Step 3: Use matchPricingTier from shared- const matchResult = matchPricingTier(pricingTiers, usageDetails);+ const matchResult = matchPricingTier(pricingTiers, usageDetails ?? {}, {+ modelParameters,+ metadata,+ });if (!matchResult) {return { matched: false as const };}- // Step 4: Find the full tier details+ // Step 3: Find the full tier detailsconst matchedTier = pricingTiers.find((t) => t.id === matchResult.pricingTierId,);diff --git a/worker/src/__tests__/pricing-tier-matcher.test.ts b/worker/src/__tests__/pricing-tier-matcher.test.tsindex 45585029ef..b18ef7f541 100644--- a/worker/src/__tests__/pricing-tier-matcher.test.ts+++ b/worker/src/__tests__/pricing-tier-matcher.test.ts@@ -3,12 +3,21 @@ import { Decimal } from "decimal.js";import { z } from "zod";import { validateRegexPattern } from "@langfuse/shared";import {+ hasPricingTierUsageDetails,matchPricingTier,type PricingTierWithPrices,} from "@langfuse/shared/src/server";import { DefaultModelPriceSchema } from "../scripts/upsertDefaultModelPrices";import defaultModelPrices from "../constants/default-model-prices.json";+describe("hasPricingTierUsageDetails", () => {+ it("requires at least one usage detail, including zero-valued details", () => {+ expect(hasPricingTierUsageDetails(undefined)).toBe(false);+ expect(hasPricingTierUsageDetails({})).toBe(false);+ expect(hasPricingTierUsageDetails({ input: 0 })).toBe(true);+ });+});+describe("default-model-prices.json", () => {it("should parse successfully with Zod schema (same validation as upsertDefaultModelPrices)", () => {expect(() =>@@ -174,26 +183,29 @@ describe("default-model-prices.json", () => {expect(tier.conditions.length).toBeGreaterThan(0);for (const condition of tier.conditions) {- expect(condition).toHaveProperty("usageDetailPattern");expect(condition).toHaveProperty("operator");- expect(condition).toHaveProperty("value");- expect(condition).toHaveProperty("caseSensitive");-- // Validate operator- expect(["gt", "gte", "lt", "lte", "eq", "neq"]).toContain(- condition.operator,- );-- // Validate value is a number- expect(typeof condition.value).toBe("number");-- // Validate caseSensitive is boolean- expect(typeof condition.caseSensitive).toBe("boolean");-- // Validate pattern is a string- expect(typeof condition.usageDetailPattern).toBe("string");- expect(condition.usageDetailPattern.length).toBeGreaterThan(0);- expect(condition.usageDetailPattern.length).toBeLessThanOrEqual(200);+ if ("usageDetailPattern" in condition) {+ expect(condition).toHaveProperty("value");+ expect(["gt", "gte", "lt", "lte", "eq", "neq"]).toContain(+ condition.operator,+ );+ expect(typeof condition.value).toBe("number");+ expect(typeof condition.caseSensitive).toBe("boolean");+ expect(typeof condition.usageDetailPattern).toBe("string");+ expect(condition.usageDetailPattern.length).toBeGreaterThan(0);+ expect(condition.usageDetailPattern.length).toBeLessThanOrEqual(+ 200,+ );+ } else {+ expect(["model_parameters", "metadata"]).toContain(+ condition.source,+ );+ expect(condition).toHaveProperty("key");+ expect(condition.operator).toBe("in");+ expect(condition.values).toEqual(+ expect.arrayContaining([expect.any(String)]),+ );+ }}}}@@ -203,9 +215,13 @@ describe("default-model-prices.json", () => {for (const model of defaultModelPrices) {for (const tier of model.pricingTiers) {for (const condition of tier.conditions) {- expect(() =>- validateRegexPattern(condition.usageDetailPattern),- ).not.toThrow();+ const pattern =+ "usageDetailPattern" in condition+ ? condition.usageDetailPattern+ : null;+ if (pattern !== null) {+ expect(() => validateRegexPattern(pattern)).not.toThrow();+ }}}}@@ -295,7 +311,7 @@ describe("default-model-prices.json", () => {}});- it("should price explicit GPT-5.6 usage aliases without implicit tiers", () => {+ it("should price GPT-5.6 usage aliases across context and service tiers", () => {const modelNames = ["gpt-5.6-sol", "gpt-5.6-terra", "gpt-5.6-luna"];for (const modelName of modelNames) {@@ -305,6 +321,8 @@ describe("default-model-prices.json", () => {expect(model, modelName).toBeDefined();expect(model!.pricingTiers.map((tier) => tier.name)).toEqual(["Standard",+ "Fast mode · Large context (>272K)",+ "Fast mode","Large Context (>272K)",]);@@ -356,7 +374,71 @@ describe("default-model-prices.json", () => {expect(matchPricingTier(tiers, { cache_write_tokens: 272001 })?.pricingTierName,).toBe("Large Context (>272K)");++ expect(+ matchPricingTier(+ tiers,+ { cache_write_tokens: 272001 },+ { modelParameters: { service_tier: "priority" } },+ )?.pricingTierName,+ ).toBe("Fast mode · Large context (>272K)");++ expect(+ matchPricingTier(+ tiers,+ { cache_write_tokens: 272001 },+ { modelParameters: { service_tier: "fast" } },+ )?.pricingTierName,+ ).toBe("Fast mode · Large context (>272K)");++ expect(+ matchPricingTier(+ tiers,+ { input: 1000 },+ { modelParameters: { service_tier: "priority" } },+ )?.pricingTierName,+ ).toBe("Fast mode");++ expect(+ matchPricingTier(+ tiers,+ { input: 1000 },+ { modelParameters: { service_tier: "fast" } },+ )?.pricingTierName,+ ).toBe("Fast mode");});++ it.each(["fast", "priority"])(+ "should match GPT-5.5 Fast mode for the %s service tier value",+ (serviceTier) => {+ const model = defaultModelPrices.find(+ (candidate) => candidate.modelName === "gpt-5.5-2026-04-23",+ );+ expect(model).toBeDefined();++ const tiers: PricingTierWithPrices[] = model!.pricingTiers.map(+ (tier) => ({+ id: tier.id,+ name: tier.name,+ isDefault: tier.isDefault,+ priority: tier.priority,+ conditions: tier.conditions,+ prices: Object.entries(tier.prices).map(([usageType, price]) => ({+ usageType,+ price: new Decimal(price),+ })),+ }),+ );++ expect(+ matchPricingTier(+ tiers,+ { input: 1000 },+ { modelParameters: { service_tier: serviceTier } },+ )?.pricingTierName,+ ).toBe("Fast mode");+ },+ );});describe("validateRegexPattern", () => {@@ -404,6 +486,134 @@ describe("validateRegexPattern", () => {});describe("matchPricingTier", () => {+ describe("attribute conditions", () => {+ const tiers: PricingTierWithPrices[] = [+ {+ id: "tier-default",+ name: "Standard",+ isDefault: true,+ priority: 0,+ conditions: [],+ prices: [{ usageType: "input", price: new Decimal("0.000005") }],+ },+ {+ id: "tier-priority",+ name: "Priority",+ isDefault: false,+ priority: 1,+ conditions: [+ {+ source: "model_parameters",+ key: "service_tier",+ operator: "in",+ values: ["priority"],+ },+ ],+ prices: [{ usageType: "input", price: new Decimal("0.0000125") }],+ },+ ];++ it("matches exact top-level model parameters", () => {+ const result = matchPricingTier(+ tiers,+ { input: 12 },+ {+ modelParameters: { service_tier: "priority" },+ },+ );++ expect(result?.pricingTierId).toBe("tier-priority");+ });++ it("matches exact top-level metadata", () => {+ const metadataTiers: PricingTierWithPrices[] = [+ tiers[0]!,+ {+ ...tiers[1]!,+ conditions: [+ {+ source: "metadata",+ key: "inference_geo",+ operator: "in",+ values: ["us"],+ },+ ],+ },+ ];++ const result = matchPricingTier(+ metadataTiers,+ { input: 12 },+ {+ metadata: { inference_geo: "us" },+ },+ );++ expect(result?.pricingTierId).toBe("tier-priority");+ });++ it("falls back when the exact key is absent", () => {+ const result = matchPricingTier(+ tiers,+ { input: 12 },+ {+ modelParameters: { different_key: "priority" },+ },+ );++ expect(result?.pricingTierId).toBe("tier-default");+ });++ it.each(["fast", "priority"])(+ "matches any configured attribute value for %s",+ (serviceTier) => {+ const result = matchPricingTier(+ [+ tiers[0]!,+ {+ ...tiers[1]!,+ conditions: [+ {+ source: "model_parameters",+ key: "service_tier",+ operator: "in",+ values: ["fast", "priority"],+ },+ ],+ },+ ],+ { input: 12 },+ { modelParameters: { service_tier: serviceTier } },+ );++ expect(result?.pricingTierId).toBe("tier-priority");+ },+ );++ it("falls back when an attribute value is outside the configured set", () => {+ const result = matchPricingTier(+ [+ tiers[0]!,+ {+ ...tiers[1]!,+ conditions: [+ {+ source: "model_parameters",+ key: "service_tier",+ operator: "in",+ values: ["fast", "priority"],+ },+ ],+ },+ ],+ { input: 12 },+ { modelParameters: { service_tier: "standard" } },+ );++ expect(result?.pricingTierId).toBe("tier-default");+ });+ });+describe("Basic tier matching", () => {it("should return default tier when no conditions match", () => {const tiers: PricingTierWithPrices[] = [diff --git a/worker/src/constants/default-model-prices.json b/worker/src/constants/default-model-prices.jsonindex d443f9557a..eae50924bc 100644--- a/worker/src/constants/default-model-prices.json+++ b/worker/src/constants/default-model-prices.json@@ -4202,7 +4202,7 @@"modelName": "gpt-5.5-2026-04-23","matchPattern": "(?i)^(openai\/)?(gpt-5.5(-2026-04-23)?)$","createdAt": "2026-04-25T00:00:00.000Z",- "updatedAt": "2026-08-04T00:00:00.000Z",+ "updatedAt": "2026-08-18T00:00:00.000Z","tokenizerConfig": {"tokensPerName": 1,"tokenizerModel": "gpt-4",@@ -4227,11 +4227,35 @@"reasoning_tokens": 30e-6}},+ {+ "id": "af5ffdbd-411b-4215-ae86-bfc0d9c9817e",+ "name": "Fast mode",+ "isDefault": false,+ "priority": 1,+ "conditions": [+ {+ "source": "model_parameters",+ "key": "service_tier",+ "operator": "in",+ "values": ["fast", "priority"]+ }+ ],+ "prices": {+ "input": 12.5e-6,+ "input_cached_tokens": 1.25e-6,+ "input_cache_read": 1.25e-6,+ "cache_read_input_tokens": 1.25e-6,+ "output": 75e-6,+ "output_reasoning_tokens": 75e-6,+ "output_reasoning": 75e-6,+ "reasoning_tokens": 75e-6+ }+ },{"id": "a3ad657c-fff1-44dd-a51b-20d9845a112a","name": "Large Context (>272K)","isDefault": false,- "priority": 1,+ "priority": 2,"conditions": [{"usageDetailPattern": "(input)",@@ -4308,7 +4332,7 @@"modelName": "gpt-5.6-sol","matchPattern": "(?i)^(openai/)?(gpt-5.6-sol)$","createdAt": "2026-07-09T00:00:00.000Z",- "updatedAt": "2026-08-03T00:00:00.000Z",+ "updatedAt": "2026-08-18T00:00:00.000Z","tokenizerConfig": {"tokensPerName": 1,"tokenizerModel": "gpt-4",@@ -4335,11 +4359,69 @@"reasoning_tokens": 30e-6}},+ {+ "id": "54bb424a-ac97-4316-93b2-1b3a46a79f71",+ "name": "Fast mode · Large context (>272K)",+ "isDefault": false,+ "priority": 1,+ "conditions": [+ {+ "source": "model_parameters",+ "key": "service_tier",+ "operator": "in",+ "values": ["fast", "priority"]+ },+ {+ "usageDetailPattern": "(input|cache_write)",+ "operator": "gt",+ "value": 272000,+ "caseSensitive": false+ }+ ],+ "prices": {+ "input": 20e-6,+ "input_cached_tokens": 2e-6,+ "input_cache_read": 2e-6,+ "cache_read_input_tokens": 2e-6,+ "input_cache_creation": 25e-6,+ "cache_write_tokens": 25e-6,+ "output": 90e-6,+ "output_reasoning_tokens": 90e-6,+ "output_reasoning": 90e-6,+ "reasoning_tokens": 90e-6+ }+ },+ {+ "id": "26ef382b-df27-4a55-ad8c-dedea29d76ee",+ "name": "Fast mode",+ "isDefault": false,+ "priority": 2,+ "conditions": [+ {+ "source": "model_parameters",+ "key": "service_tier",+ "operator": "in",+ "values": ["fast", "priority"]+ }+ ],+ "prices": {+ "input": 10e-6,+ "input_cached_tokens": 1e-6,+ "input_cache_read": 1e-6,+ "cache_read_input_tokens": 1e-6,+ "input_cache_creation": 12.5e-6,+ "cache_write_tokens": 12.5e-6,+ "output": 60e-6,+ "output_reasoning_tokens": 60e-6,+ "output_reasoning": 60e-6,+ "reasoning_tokens": 60e-6+ }+ },{"id": "1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d","name": "Large Context (>272K)","isDefault": false,- "priority": 1,+ "priority": 3,"conditions": [{"usageDetailPattern": "(input|cache_write)",@@ -4368,7 +4450,7 @@"modelName": "gpt-5.6-terra","matchPattern": "(?i)^(openai/)?(gpt-5.6-terra)$","createdAt": "2026-07-09T00:00:00.000Z",- "updatedAt": "2026-08-03T00:00:00.000Z",+ "updatedAt": "2026-08-18T00:00:00.000Z","tokenizerConfig": {"tokensPerName": 1,"tokenizerModel": "gpt-4",@@ -4395,11 +4477,69 @@"reasoning_tokens": 12e-6}},+ {+ "id": "9ca35f10-4a56-4cb0-9ca6-0ae9c57deeee",+ "name": "Fast mode · Large context (>272K)",+ "isDefault": false,+ "priority": 1,+ "conditions": [+ {+ "source": "model_parameters",+ "key": "service_tier",+ "operator": "in",+ "values": ["fast", "priority"]+ },+ {+ "usageDetailPattern": "(input|cache_write)",+ "operator": "gt",+ "value": 272000,+ "caseSensitive": false+ }+ ],+ "prices": {+ "input": 8e-6,+ "input_cached_tokens": 0.8e-6,+ "input_cache_read": 0.8e-6,+ "cache_read_input_tokens": 0.8e-6,+ "input_cache_creation": 10e-6,+ "cache_write_tokens": 10e-6,+ "output": 36e-6,+ "output_reasoning_tokens": 36e-6,+ "output_reasoning": 36e-6,+ "reasoning_tokens": 36e-6+ }+ },+ {+ "id": "9ff3bb71-f328-4cee-81b8-6b767871d820",+ "name": "Fast mode",+ "isDefault": false,+ "priority": 2,+ "conditions": [+ {+ "source": "model_parameters",+ "key": "service_tier",+ "operator": "in",+ "values": ["fast", "priority"]+ }+ ],+ "prices": {+ "input": 4e-6,+ "input_cached_tokens": 0.4e-6,+ "input_cache_read": 0.4e-6,+ "cache_read_input_tokens": 0.4e-6,+ "input_cache_creation": 5e-6,+ "cache_write_tokens": 5e-6,+ "output": 24e-6,+ "output_reasoning_tokens": 24e-6,+ "output_reasoning": 24e-6,+ "reasoning_tokens": 24e-6+ }+ },{"id": "2b3c4d5e-6f7a-8b9c-0d1e-2f3a4b5c6d7e","name": "Large Context (>272K)","isDefault": false,- "priority": 1,+ "priority": 3,"conditions": [{"usageDetailPattern": "(input|cache_write)",@@ -4428,7 +4568,7 @@"modelName": "gpt-5.6-luna","matchPattern": "(?i)^(openai/)?(gpt-5.6-luna)$","createdAt": "2026-07-09T00:00:00.000Z",- "updatedAt": "2026-08-03T00:00:00.000Z",+ "updatedAt": "2026-08-18T00:00:00.000Z","tokenizerConfig": {"tokensPerName": 1,"tokenizerModel": "gpt-4",@@ -4455,11 +4595,69 @@"reasoning_tokens": 1.2e-6}},+ {+ "id": "5ab18b8b-86aa-4077-9f3c-a4ea6f8a37c8",+ "name": "Fast mode · Large context (>272K)",+ "isDefault": false,+ "priority": 1,+ "conditions": [+ {+ "source": "model_parameters",+ "key": "service_tier",+ "operator": "in",+ "values": ["fast", "priority"]+ },+ {+ "usageDetailPattern": "(input|cache_write)",+ "operator": "gt",+ "value": 272000,+ "caseSensitive": false+ }+ ],+ "prices": {+ "input": 0.8e-6,+ "input_cached_tokens": 0.08e-6,+ "input_cache_read": 0.08e-6,+ "cache_read_input_tokens": 0.08e-6,+ "input_cache_creation": 1e-6,+ "cache_write_tokens": 1e-6,+ "output": 3.6e-6,+ "output_reasoning_tokens": 3.6e-6,+ "output_reasoning": 3.6e-6,+ "reasoning_tokens": 3.6e-6+ }+ },+ {+ "id": "079cdd36-4c31-4df3-9e4b-38074becb0b4",+ "name": "Fast mode",+ "isDefault": false,+ "priority": 2,+ "conditions": [+ {+ "source": "model_parameters",+ "key": "service_tier",+ "operator": "in",+ "values": ["fast", "priority"]+ }+ ],+ "prices": {+ "input": 0.4e-6,+ "input_cached_tokens": 0.04e-6,+ "input_cache_read": 0.04e-6,+ "cache_read_input_tokens": 0.04e-6,+ "input_cache_creation": 0.5e-6,+ "cache_write_tokens": 0.5e-6,+ "output": 2.4e-6,+ "output_reasoning_tokens": 2.4e-6,+ "output_reasoning": 2.4e-6,+ "reasoning_tokens": 2.4e-6+ }+ },{"id": "3c4d5e6f-7a8b-9c0d-1e2f-3a4b5c6d7e8f","name": "Large Context (>272K)","isDefault": false,- "priority": 1,+ "priority": 3,"conditions": [{"usageDetailPattern": "(input|cache_write)",diff --git a/worker/src/scripts/upsertDefaultModelPrices.test.ts b/worker/src/scripts/upsertDefaultModelPrices.test.tsnew file mode 100644index 0000000000..c8d2a0505b--- /dev/null+++ b/worker/src/scripts/upsertDefaultModelPrices.test.ts@@ -0,0 +1,110 @@+import { beforeEach, describe, expect, it, vi } from "vitest";++const transaction = vi.hoisted(() => vi.fn());++vi.mock("@langfuse/shared/src/db", () => ({+ prisma: { $transaction: transaction },+}));++import {+ type DefaultModelPrice,+ upsertModelWithTiers,+} from "./upsertDefaultModelPrices";++describe("upsertModelWithTiers", () => {+ beforeEach(() => {+ vi.clearAllMocks();+ });++ it("vacates existing priorities before applying a reordered tier layout", async () => {+ const calls: string[] = [];+ const tx = {+ model: {+ upsert: vi.fn().mockImplementation(() => {+ calls.push("model.upsert");+ }),+ },+ pricingTier: {+ deleteMany: vi.fn(),+ update: vi.fn().mockImplementation(({ data }) => {+ calls.push(`tier.update:${data.priority}`);+ }),+ upsert: vi.fn().mockImplementation(({ create }) => {+ calls.push(`tier.upsert:${create.priority}`);+ }),+ },+ price: {+ findMany: vi.fn().mockResolvedValue([]),+ deleteMany: vi.fn(),+ upsert: vi.fn(),+ },+ };++ transaction.mockImplementation(+ async (callback: (client: typeof tx) => Promise<void>) => callback(tx),+ );++ const model = {+ id: "model-id",+ modelName: "Model",+ matchPattern: "(?i)^model$",+ createdAt: new Date("2026-01-01"),+ updatedAt: new Date("2026-02-01"),+ tokenizerConfig: null,+ tokenizerId: null,+ pricingTiers: [+ {+ id: "standard",+ name: "Standard",+ isDefault: true,+ priority: 0,+ conditions: [],+ prices: {},+ },+ {+ id: "priority",+ name: "Priority",+ isDefault: false,+ priority: 1,+ conditions: [],+ prices: {},+ },+ {+ id: "large-context",+ name: "Large Context",+ isDefault: false,+ priority: 2,+ conditions: [],+ prices: {},+ },+ ],+ } satisfies DefaultModelPrice;++ await upsertModelWithTiers(model, {+ updatedAt: new Date("2026-01-01"),+ tiers: [+ {+ id: "standard",+ name: "Standard",+ priority: 0,+ isDefault: true,+ },+ {+ id: "large-context",+ name: "Large Context",+ priority: 1,+ isDefault: false,+ },+ ],+ });++ expect(calls).toEqual([+ "model.upsert",+ "tier.update:3",+ "tier.update:4",+ "tier.upsert:0",+ "tier.upsert:1",+ "tier.upsert:2",+ ]);+ });+});diff --git a/worker/src/scripts/upsertDefaultModelPrices.ts b/worker/src/scripts/upsertDefaultModelPrices.tsindex 3db5e76177..5e44cd8bd1 100644--- a/worker/src/scripts/upsertDefaultModelPrices.ts+++ b/worker/src/scripts/upsertDefaultModelPrices.ts@@ -218,7 +218,7 @@ export const upsertDefaultModelPrices = async (force = false) => {/*** Upserts a model with its pricing tiers in a transaction*/-async function upsertModelWithTiers(+export async function upsertModelWithTiers(defaultModelPrice: DefaultModelPrice,existingModel:| {@@ -280,6 +280,26 @@ async function upsertModelWithTiers(`Deleted ${tiersToDelete.length} obsolete tiers for model ${defaultModelPrice.modelName}`,);}++ // Vacate all persisted priorities before applying the desired layout.+ // This avoids unique constraint conflicts when an existing tier moves to+ // a priority that a newly inserted tier should take over.+ const temporaryPriorityStart =+ Math.max(+ ...existingModel.tiers.map((tier) => tier.priority),+ ...defaultModelPrice.pricingTiers.map((tier) => tier.priority),+ ) + 1;++ const retainedTiers = existingModel.tiers.filter((tier) =>+ jsonTierIds.has(tier.id),+ );++ for (const [index, tier] of retainedTiers.entries()) {+ await tx.pricingTier.update({+ where: { id: tier.id },+ data: { priority: temporaryPriorityStart + index },+ });+ }}// 4. Upsert each tier and its pricesdiff --git a/worker/src/services/IngestionService/index.ts b/worker/src/services/IngestionService/index.tsindex 577a1d9734..283563c8a8 100644--- a/worker/src/services/IngestionService/index.ts+++ b/worker/src/services/IngestionService/index.ts@@ -8,6 +8,7 @@ import {ObservationLevel,PrismaClient,Prompt,+ safeJsonParse,type JsonNested,} from "@langfuse/shared";import {@@ -41,6 +42,7 @@ import {TraceUpsertQueue,UsageCostType,findModel,+ hasPricingTierUsageDetails,matchPricingTier,validateAndInflateScore,DatasetRunItemRecordInsertType,@@ -53,6 +55,7 @@ import {hasNoEvalConfigsCache,buildClickHouseLogComment,type IngestionAttribution,+ type PricingTierMatchAttributes,} from "@langfuse/shared/src/server";import { tokenCountAsync } from "../../features/tokenisation/async-usage";@@ -79,7 +82,43 @@ function parseUInt16(value: string | null | undefined): number | undefined {return num;}+function toPricingAttributeRecord(value: unknown): Record<string, string> {+ const parsedValue = typeof value === "string" ? safeJsonParse(value) : value;++ if (+ !parsedValue ||+ typeof parsedValue !== "object" ||+ Array.isArray(parsedValue)+ ) {+ return {};+ }++ return Object.fromEntries(+ Object.entries(parsedValue).flatMap(([key, attributeValue]) =>+ typeof attributeValue === "string" ||+ typeof attributeValue === "number" ||+ typeof attributeValue === "boolean"+ ? [[key, String(attributeValue)]]+ : [],+ ),+ );+}+export type EventInput = InternalTraceEventInput;++function parseEventModelParameters(+ value: EventInput["modelParameters"],+): string | object | number | boolean | null {+ if (!value) return {};+ if (typeof value !== "string") return value;++ try {+ return JSON.parse(value) as string | object | number | boolean | null;+ } catch {+ return value;+ }+}+type InsertRecord =| TraceRecordInsertType| ScoreRecordInsertType@@ -95,6 +134,11 @@ type MergeAndWriteParams = {attribution: IngestionAttribution;};+type PricingTierMatchAttributeValues = {+ modelParameters?: unknown;+ metadata?: unknown;+};+/*** Returns a new event record whose `event_bytes` value is the UTF-8 size of* the final JSONEachRow payload, excluding the self-referential accounting@@ -256,6 +300,9 @@ export class IngestionService {// fields as strings, so stringify at this schema boundary.const input = this.stringify(eventData.input);const output = this.stringify(eventData.output);+ const modelParameters = parseEventModelParameters(+ eventData.modelParameters,+ );// Runs outside the modelName gate below so model-less events with provided// usage are still checked.@@ -288,6 +335,10 @@ export class IngestionService {shouldEnrichUsageAndCost? this.getGenerationUsage({projectId: eventData.projectId,+ pricingMatchAttributeValues: {+ modelParameters,+ metadata: eventData.metadata,+ },observationRecord: {id: eventData.spanId,project_id: eventData.projectId,@@ -359,11 +410,11 @@ export class IngestionService {// Modelmodel_id: generationUsage?.internal_model_id || "",provided_model_name: eventData.modelName,- model_parameters: eventData.modelParameters- ? typeof eventData.modelParameters === "string"- ? JSON.parse(eventData.modelParameters)- : eventData.modelParameters- : {},+ // Evaluation scheduling consumes this enriched record before it is+ // written and expects parsed model parameters. The established runtime+ // contract is therefore wider than the ClickHouse insert type declares.+ model_parameters:+ modelParameters as EventRecordInsertType["model_parameters"],// Usage & Costprovided_usage_details: eventData.providedUsageDetails ?? {},@@ -937,6 +988,21 @@ export class IngestionService {const rawOutput =reversedRawRecords.find((record) => record?.body?.output)?.body?.output ??clickhouseObservationRecord?.output;+ const pricingEventIndex = observationRecords.findLastIndex(+ (record) => Object.keys(record.provided_usage_details ?? {}).length > 0,+ );+ const pricingEvent = timeSortedEvents[pricingEventIndex];+ const pricingMatchAttributeValues:+ | PricingTierMatchAttributeValues+ | undefined = pricingEvent+ ? {+ modelParameters:+ "modelParameters" in pricingEvent.body+ ? pricingEvent.body.modelParameters+ : undefined,+ metadata: pricingEvent.body.metadata,+ }+ : undefined;const normalizedTools = normalizeToolsForObservation(rawInput,rawOutput,@@ -981,6 +1047,7 @@ export class IngestionService {const generationUsage = await this.getGenerationUsage({projectId,+ pricingMatchAttributeValues,observationRecord: mergedObservationRecord,});const finalObservationRecord = {@@ -1197,6 +1264,7 @@ export class IngestionService {private async getGenerationUsage(params: {projectId: string;+ pricingMatchAttributeValues?: PricingTierMatchAttributeValues;observationRecord: Pick<ObservationRecordInsertType,| "project_id"@@ -1220,7 +1288,8 @@ export class IngestionService {| "usage_pricing_tier_name">> {- const { projectId, observationRecord } = params;+ const { projectId, observationRecord, pricingMatchAttributeValues } =+ params;const { model: internalModel, pricingTiers } =observationRecord.provided_model_name? await findModel({@@ -1241,14 +1310,38 @@ export class IngestionService {let modelPrices: Array<{ usageType: string; price: Decimal }> = [];let usage_pricing_tier_id: string | null = null;let usage_pricing_tier_name: string | null = null;-if (pricingTiers.length > 0 &&- Object.keys(final_usage_details.usage_details ?? {}).length > 0+ hasPricingTierUsageDetails(final_usage_details.usage_details)) {+ const attributeSources = new Set(+ pricingTiers.flatMap((tier) =>+ tier.isDefault+ ? []+ : tier.conditions.flatMap((condition) =>+ "source" in condition ? [condition.source] : [],+ ),+ ),+ );+ const pricingMatchAttributes: PricingTierMatchAttributes | undefined =+ attributeSources.size > 0+ ? {+ modelParameters: attributeSources.has("model_parameters")+ ? toPricingAttributeRecord(+ pricingMatchAttributeValues?.modelParameters,+ )+ : {},+ metadata: attributeSources.has("metadata")+ ? toPricingAttributeRecord(+ pricingMatchAttributeValues?.metadata,+ )+ : {},+ }+ : undefined;const matchedTier = matchPricingTier(pricingTiers,final_usage_details.usage_details,+ pricingMatchAttributes,);if (matchedTier) {diff --git a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts b/worker/src/services/IngestionService/tests/IngestionService.integration.test.tsindex b069bb7a79..c11d261405 100644--- a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts+++ b/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts@@ -2573,6 +2573,188 @@ describe("Ingestion end-to-end tests", () => {});describe("Tiered Pricing", () => {+ it("applies an exact model parameter tier to direct event writes", async () => {+ const traceId = randomUUID();+ const generationId = randomUUID();+ const modelId = randomUUID();+ const modelName = `priority-tier-${randomUUID()}`;++ await prisma.model.create({+ data: {+ id: modelId,+ projectId,+ modelName,+ matchPattern: `(?i)^(${modelName})$`,+ startDate: new Date("2021-01-01T00:00:00.000Z"),+ unit: ModelUsageUnit.Tokens,+ pricingTiers: {+ create: [+ {+ name: "Standard",+ isDefault: true,+ priority: 0,+ conditions: [],+ prices: {+ create: [+ { usageType: "input", price: 0.000005, modelId },+ { usageType: "output", price: 0.00003, modelId },+ ],+ },+ },+ {+ name: "Priority",+ isDefault: false,+ priority: 1,+ conditions: [+ {+ source: "model_parameters",+ key: "service_tier",+ operator: "in",+ values: ["priority"],+ },+ ],+ prices: {+ create: [+ { usageType: "input", price: 0.0000125, modelId },+ { usageType: "output", price: 0.000075, modelId },+ ],+ },+ },+ ],+ },+ },+ });++ const eventRecord = await ingestionService.createEventRecord(+ {+ projectId,+ traceId,+ spanId: generationId,+ name: "priority-generation",+ type: "GENERATION",+ environment,+ startTimeISO: new Date().toISOString(),+ modelName,+ modelParameters: { service_tier: "priority" },+ metadata: {},+ providedUsageDetails: { input: 12, output: 21 },+ source: "otel",+ },+ `otel/${projectId}/priority-generation.json`,+ );++ expect(eventRecord.usage_pricing_tier_name).toBe("Priority");+ expect(eventRecord.cost_details.total).toBeCloseTo(0.001725, 9);+ });++ it("applies exact attribute tiers from a legacy usage event", async () => {+ const traceId = randomUUID();+ const generationId = randomUUID();+ const modelId = randomUUID();+ const modelName = `legacy-priority-tier-${randomUUID()}`;++ await prisma.model.create({+ data: {+ id: modelId,+ projectId,+ modelName,+ matchPattern: `(?i)^(${modelName})$`,+ startDate: new Date("2021-01-01T00:00:00.000Z"),+ unit: ModelUsageUnit.Tokens,+ pricingTiers: {+ create: [+ {+ name: "Standard",+ isDefault: true,+ priority: 0,+ conditions: [],+ prices: {+ create: [+ { usageType: "input", price: 0.000005, modelId },+ { usageType: "output", price: 0.00003, modelId },+ ],+ },+ },+ {+ name: "Priority US",+ isDefault: false,+ priority: 1,+ conditions: [+ {+ source: "model_parameters",+ key: "service_tier",+ operator: "in",+ values: ["priority"],+ },+ {+ source: "metadata",+ key: "region",+ operator: "in",+ values: ["us"],+ },+ ],+ prices: {+ create: [+ { usageType: "input", price: 0.0000125, modelId },+ { usageType: "output", price: 0.000075, modelId },+ ],+ },+ },+ ],+ },+ },+ });++ const observationEventList: ObservationEvent[] = [+ {+ id: randomUUID(),+ timestamp: new Date().toISOString(),+ type: "generation-create",+ body: {+ id: generationId,+ traceId,+ name: "legacy-priority-generation",+ startTime: new Date().toISOString(),+ model: modelName,+ environment,+ },+ },+ {+ id: randomUUID(),+ timestamp: new Date().toISOString(),+ type: "generation-update",+ body: {+ id: generationId,+ traceId,+ usage: {+ input: 12,+ output: 21,+ unit: ModelUsageUnit.Tokens,+ },+ modelParameters: { service_tier: "priority" },+ metadata: { region: "us" },+ environment,+ },+ },+ ];++ await ingestionService.processObservationEventList({+ projectId,+ entityId: generationId,+ createdAtTimestamp: new Date(),+ observationEventList,+ });+ await clickhouseWriter.flushAll(true);++ const generation = await getClickhouseRecord(+ TableName.Observations,+ generationId,+ );++ expect(generation.usage_pricing_tier_name).toBe("Priority US");+ expect(generation.cost_details.total).toBeCloseTo(0.001725, 9);+ });+it("should apply default tier for usage below threshold (Anthropic Claude example)", async () => {const traceId = randomUUID();const generationId = randomUUID();diff --git a/worker/src/services/IngestionService/tests/IngestionService.unit.test.ts b/worker/src/services/IngestionService/tests/IngestionService.unit.test.tsindex c0d1c3cc6d..97127c504b 100644--- a/worker/src/services/IngestionService/tests/IngestionService.unit.test.ts+++ b/worker/src/services/IngestionService/tests/IngestionService.unit.test.ts@@ -157,7 +157,110 @@ describe("IngestionService unit tests", () => {expect(eventRecord.cost_details).toEqual({ total: 0.03 });});- it("does not overflow legacy observation or dual-write staging records", async () => {+ it("preserves non-JSON model parameter strings on direct events", async () => {+ const ingestionService = new IngestionService(+ {} as any,+ {} as any,+ {} as any,+ {} as any,+ );++ const eventRecord = await ingestionService.createEventRecord(+ {+ projectId: "project-id",+ traceId: "trace-id",+ spanId: "observation-id",+ name: "invalid-model-parameters",+ type: "SPAN",+ environment: "default",+ startTimeISO: "2026-08-17T00:00:00.000Z",+ endTimeISO: "2026-08-17T00:00:01.000Z",+ modelParameters: "not-json",+ metadata: {},+ source: "otel",+ },+ "otel/project-id/raw-event.json",+ );++ expect(eventRecord.model_parameters).toBe("not-json");+ });++ it("passes direct-event attribute values to pricing", async () => {+ const ingestionService = new IngestionService(+ {} as any,+ {} as any,+ {} as any,+ {} as any,+ );+ const getGenerationUsage = vi+ .spyOn(ingestionService as any, "getGenerationUsage")+ .mockResolvedValue({});++ const eventRecord = await ingestionService.createEventRecord(+ {+ projectId: "project-id",+ traceId: "trace-id",+ spanId: "observation-id",+ name: "primitive-pricing-attributes",+ type: "GENERATION",+ environment: "default",+ startTimeISO: "2026-08-18T00:00:00.000Z",+ endTimeISO: "2026-08-18T00:00:01.000Z",+ modelName: "model-name",+ modelParameters: {+ service_tier: "priority",+ temperature: 0.5,+ stream: true,+ nested: { ignored: "value" },+ list: ["ignored"],+ nil: null,+ },+ metadata: {+ region: "us",+ attempts: 2,+ cached: false,+ nested: { ignored: "value" },+ list: ["ignored"],+ nil: null,+ },+ source: "otel",+ },+ "otel/project-id/raw-event.json",+ );++ expect(eventRecord.model_parameters).toEqual({+ service_tier: "priority",+ temperature: 0.5,+ stream: true,+ nested: { ignored: "value" },+ list: ["ignored"],+ nil: null,+ });+ expect(getGenerationUsage).toHaveBeenCalledWith(+ expect.objectContaining({+ pricingMatchAttributeValues: {+ modelParameters: {+ service_tier: "priority",+ temperature: 0.5,+ stream: true,+ nested: { ignored: "value" },+ list: ["ignored"],+ nil: null,+ },+ metadata: {+ region: "us",+ attempts: 2,+ cached: false,+ nested: { ignored: "value" },+ list: ["ignored"],+ nil: null,+ },+ },+ }),+ );+ });++ it("uses only pricing attributes from the legacy event that supplies usage", async () => {const addToQueue = vi.fn();const ingestionService = new IngestionService({} as any,@@ -180,10 +283,39 @@ describe("IngestionService unit tests", () => {startTime: timestamp,input,output,- metadata: { large: metadataValue },+ metadata: {+ large: metadataValue,+ count: 2,+ enabled: false,+ nested: { ignored: "value" },+ list: ["ignored"],+ nil: null,+ },+ modelParameters: {+ service_tier: "priority",+ temperature: 0.5,+ stream: true,+ nested: { ignored: "value" },+ list: ["ignored"],+ nil: null,+ },environment: "default",},},+ {+ id: "update-event-id",+ timestamp: "2026-07-22T00:00:01.000Z",+ type: "generation-update",+ body: {+ id: "observation-id",+ usage: {+ input: 12,+ output: 21,+ },+ modelParameters: { service_tier: "fast" },+ metadata: { region: "eu" },+ },+ },];vi.spyOn(ingestionService as any, "getClickhouseRecord").mockResolvedValue(@@ -208,6 +340,15 @@ describe("IngestionService unit tests", () => {});expect(mocks.applyObservationFieldOverflow).not.toHaveBeenCalled();+ const getGenerationUsage = vi.mocked(+ (ingestionService as any).getGenerationUsage,+ );+ expect(+ getGenerationUsage.mock.calls[0]?.[0].pricingMatchAttributeValues,+ ).toEqual({+ modelParameters: { service_tier: "fast" },+ metadata: { region: "eu" },+ });for (const table of [TableName.Observations,TableName.ObservationsBatchStaging,