🪢 Langfuse 图解教程Langfuse Visual Guide 第八部分 · 仪表盘·指标·成本Part 8 · Dashboards, Metrics & Cost 42 / 55
第八部分 · 仪表盘·指标·成本Part 8 · Dashboards, Metrics & Cost

模型与定价Models & pricing

🧭 读前Before you start
先读 第16课 的 token 与成本,本课讲模型与定价表如何配置。
Read Lesson 16 on tokens and cost; this lesson covers how models and the pricing table are configured.

第 16 课说过「成本 = 用量 × 单价」,但留了个悬念:单价从哪来?一个叫法五花八门的模型名,又怎么对上它的价目? 这一课补上这块拼图——Langfuse 的定价数据模型。重点有三:matchPattern 用一条正则把同一个模型在各家平台千奇百怪的命名统一认出来;pricingTiers 用「默认档 + 条件档」支持按用量/上下文窗口分级定价;而 prices 是一张「每种用量、每个 token」的细价目(输入、输出、缓存读……各有各的价)。 它直接喂养第 16 课的成本计算:拿一条调用的模型名去匹配出价目、按用量选中合适的档、再用「单价 × 用量」算出这次花了多少钱。

📋 生活类比
模型定价像一本「跨国出租车计价手册」。同一款车,在不同城市、不同叫车平台有一堆不同的名字——gpt-4o、openai/gpt-4o、us.anthropic.claude-...-v1:0。手册用一条「识别规则」(matchPattern 正则)把这些花名全认成同一辆车,对上同一份价目。 而车费不是一口价:市区短途一个价,但要是你这趟特别长(比如上下文窗口超过 20 万 token),就跳到「长途档」按另一套价算(pricingTiers 的条件档)。最后结账时,手册按「跑了多少 × 每单位多少钱」逐项相加——而且分项计费:起步价、里程费、等待费各算各的(输入 token、输出 token、缓存命中各有单价)。一本手册,把「认车 + 分档 + 分项计费」三件事一次办妥。

matchPattern:一条正则,认尽各家花名

同一个模型,在 OpenAI、Anthropic、AWS Bedrock、Google Vertex、Azure 上的命名规则各不相同:有的带 openai/ 前缀、有的带 us. 区域前缀、有的带 -v1:0 版本后缀、有的用 @日期。如果用「精确名」去匹配价目,就得为每种写法各存一条,维护噩梦。Langfuse 的解法是给每个模型配一条大小写不敏感、全串匹配的正则 matchPattern,一网打尽所有合法写法。

各家花名 → 一条正则 → 同一份价目 gpt-4o(裸名) openai/gpt-4o(带 provider 前缀) us.anthropic.claude-...-v1:0(Bedrock) claude-...@20240620(Vertex 日期) matchPattern(?i)^(provider/)?(...)$大小写不敏感 · 全串匹配 同一个 Model 条目tokenizerId + pricingTiers认成同一辆车 → 同一价目 正则组件:(?i) 不分大小写 · ^…$ 全串 · (provider/)? 可选前缀 · (us.|eu.|apac.)? 区域 · (:0)? / @date 版本
一条正则,统一碎片化命名:default-model-prices.json 的每个条目都带一条 matchPattern,如 gpt-4o 的 (?i)^(openai/)?(gpt-4o)$。它用 (?i) 不分大小写、^…$ 全串匹配、可选的 provider/区域前缀与版本后缀,把同一模型在六大平台的所有写法一网打尽。源码:worker/src/constants/default-model-prices.json。
示例:模型定价表(每 1M token) modelName 输入 $/1M 输出 $/1M 输出价对比 gpt-4o $2.50 $10.00 $10.00 gpt-4o-mini $0.15 $0.60 $0.60 claude-3-5-sonnet-20240620 $3.00 $15.00 $15.00 claude-3-haiku-20240307 $0.25 $1.25 $1.25 gpt-4-turbo $10.00 $30.00 $30.00 模型名怎么对上价格?matchPattern 正则 usage.model = "openai/gpt-4o" ──match──▶ (?i)^(openai/)?(gpt-4o)$ ✓ → gpt-4o 价格按 token 存在 pricingTiers[].prices.{input,output}(图中已 ×1e6 换成每百万);摄取时用正则把模型名匹配到价格行
定价是「正则 → 每 token 价」的查找表(数据取自 worker/src/constants/default-model-prices.json,真实价):每个模型在 pricingTiers[].prices 里按 token 存 input/output 价(表中已 ×1e6 换成「每百万 token」更好读)。同是输出价,gpt-4-turbo($30)是 gpt-4o-mini($0.60)的 50 倍——选型即成本。模型名怎么对上价格?靠 matchPattern 正则:"openai/gpt-4o" 命中 (?i)^(openai/)?(gpt-4o)$ 就用这行价,于是带不带 openai/ 前缀都能正确计费(第 12 课摄取算成本就用它)。
worker/src/constants/default-model-prices.json一个模型条目(节选)
// 158 条种子价之一;matchPattern 认尽各家花名
{
  "modelName": "gpt-4o",
  "matchPattern": "(?i)^(openai/)?(gpt-4o)$",   // gpt-4o / openai/gpt-4o / GPT-4O 都匹配
  "tokenizerId": "openai",                        // SDK 没报用量时,用这个分词器数 token
  "pricingTiers": [ { "name": "Standard", "isDefault": true, "conditions": [],
    "prices": { "input": 2.5e-6, "output": 1e-5,             // 每 token 单价
                "input_cached_tokens": 1.25e-6, … } } ]      // 分项:输入/输出/缓存读各有价
}
// Anthropic 的 matchPattern 还会含 us./eu./apac. 区域前缀与 -v1:0、@date 版本写法
正则组件作用能认出的写法
(?i)大小写不敏感gpt-4o 和 GPT-4O 都认
^…$全串匹配避免误伤(不会把 gpt-4o-mini 当成 gpt-4o)
(provider/)?可选 provider 前缀openai/gpt-4o、anthropic/claude-…
(us\.|eu\.|apac\.)?可选 AWS 区域前缀us.anthropic.claude-…(Bedrock)
(:0)? / @date可选版本后缀…-v1:0(Bedrock)、…@20240620(Vertex)

这几个组件像一套「可拆装的乐高」:哪家平台多了种命名花样,往正则里加一段可选组就行,无需重排价目表。一条规则,把六大平台的命名差异吸收在了模式层。

pricingTiers:默认档 + 条件档

定价往往不是一口价。同一个模型,上下文窗口越大、单价可能越高;有的还按用量阶梯计费。Langfuse 用 pricingTiers(价目档)支持这种分级:每个模型有恰好一个默认档(isDefault=true、priority=0、conditions=[]),外加任意个条件档(带 conditions,如「输入 token > 20 万」)。选档时,matchPricingTier 把条件档按 priority 升序逐个试,第一个所有条件都满足的胜出;都不满足就回落到默认档。

按用量选档:条件档优先,都不中则回落默认 这次调用的用量input=250k, output=8k 条件档(priority 1):input > 200000 ?满足 ✓ → 选它(长上下文价) 条件档(priority 2):…(前一个已中,不再试) 默认档(priority 0, conditions=[]):都不中时的兜底 选中档的 prices→ 第16课算成本 conditions 用 AND 逻辑(全满足才算中);operator 支持 gt/gte/lt/lte/eq/neq;按 priority 升序「先到先得」 默认档永远存在、永远兜底——保证任何用量都能算出一个价,绝不漏算
分级定价,默认兜底:matchPricingTier(pricing-tiers/matcher.ts:88)滤掉默认档、按 priority 升序评估条件档,第一个全条件满足(AND 逻辑)的胜出,否则回落默认档。条件 = usageDetailPattern(正则选用量项)+ operator(gt/gte/lt/lte/eq/neq)+ value。
档类型isDefault / priority / conditions作用
默认档(每模型恰一个)true / 0 / []兜底价——任何用量都能算出,绝不漏
条件档(任意个)false / ≥1 / 有条件按上下文窗口/用量分级(如「>20万 token 走长上下文价」)
选档规则:条件档按 priority 升序逐个评,第一个全条件满足者胜;都不中→默认档

合起来:一次调用的成本是怎么算出来的

把 matchPattern 和 pricingTiers 串起来,就还原了第 16 课「成本 = 用量 × 单价」的完整链条。default-model-prices.json 这 158 条种子价会被 upsertDefaultModelPrices 灌进数据库的 Model/Price 表;之后每条调用都走这条计价线:

① 拿模型名 us.anthropic .claude-… ② 匹配 Model matchPattern 正则 → tiers + tokenizer ③ 数 token (SDK 没报时) tokenizerId 分词 ④ 选档 matchPricingTier → prices 价目 ⑤ 分项算钱 prices[项] × 用量 逐项相加 总成本 = Σ(各项单价 × 各项用量) 还原第 16 课「成本 = 用量 × 单价」的完整链条
1

拿模型名

从一条 observation(generation)取出它报告的模型名,如 us.anthropic.claude-...-v1:0。

2

匹配 Model

用模型名去试各条 matchPattern 正则,命中的就是这次该用的价目条目(含 tokenizerId 与 pricingTiers)。

3

(必要时)数 token

若 SDK 没报用量,用条目的 tokenizerId/tokenizerConfig 把文本分词、数出 token 数(第 16 课)。

4

选档

matchPricingTier 按用量选中合适的 tier(默认或条件档),拿到这次该用的 prices 价目。

5

分项算钱

对每种用量(输入/输出/缓存读…)用 prices[该项] × 用量 逐项相加,得这次调用的总成本。

还有一层灵活:这 158 条只是 Langfuse 内置的默认价(projectId 为空的全局 Model)。如果你用的是自研模型、私有部署、或谈了特殊折扣,可以在自己项目里新增/覆盖 Model 价目(带 projectId 的项目级条目优先)——于是平台既开箱即用(主流模型价已备好),又能精确贴合你的真实账单。add-model-price 这个仓库技能,管的就是怎么安全地往这份种子价里增改条目(生成 UUID、写对 matchPattern、校验 JSON)。

🎯 设计取舍
为什么用正则匹配模型名,而不是精确字符串相等? 因为模型命名在生态里碎得令人发指。同一个 Claude,OpenAI 风格叫 claude-3-5-sonnet、Bedrock 上叫 us.anthropic.claude-3-5-sonnet-v1:0、Vertex 上叫 claude-3-5-sonnet@20240620,再加上大小写、provider/ 前缀的排列组合,一个模型轻松有十几种合法写法。若用精确匹配,你得为每种写法各存一条价目,加个新区域、改个版本号就得全表翻新——维护成本爆炸,还极易漏。一条精心写的正则把这些同义写法收敛成一个意图:「无论你怎么称呼它,我都认得这是 gpt-4o」。正则在这里不是炫技,而是对抗命名碎片化的必需品——它把「世界的混乱」挡在了价目表之外。

为什么定价要分「默认档 + 条件档」,而不是一口价? 因为真实定价本就分级:长上下文更贵、缓存命中更便宜、某些用量超阈值跳档。如果只存一口价,这些差异要么算错、要么得在计费代码里写满 if-else——把定价策略硬编码进逻辑,每变一次价就得改代码、发版本。把分级表达成数据(一组带 conditions 的 tier),计费逻辑就只剩一件事:按用量选中合适的档,而「档怎么分、价怎么定」全在数据里、可随时调。这又是一次「把策略从代码挪进数据」的胜利——和第 28 课 score config、第 16 课定价 schema 一脉相承:能用数据表达的规则,就别写进代码。而那个永远存在的默认档,则是一道优雅的兜底:保证任何用量都落得进某个价,绝不会因为没匹配上条件就算不出钱。
🎯 本课要点
  • 定价数据模型喂养第 16 课成本计算:default-model-prices.json 158 条种子价被 upsert 进 Model/Price 表,每条调用据此算「用量 × 单价」。
  • matchPattern 统一命名:每模型一条大小写不敏感、全串匹配的正则,认尽 OpenAI/Anthropic/Bedrock/Vertex/Azure/Gemini 的各种写法(provider 前缀、区域前缀、版本后缀、@日期)。
  • pricingTiers 分级定价:恰一个默认档(兜底)+ 任意条件档;matchPricingTier 按 priority 升序评条件(AND 逻辑、operator gt/gte/lt/lte/eq/neq),第一个全满足者胜,否则回落默认。
  • prices 分项计费:每种用量(输入/输出/缓存读…)各有每-token 单价;成本 = Σ(各项单价 × 各项用量)。
  • 默认价 + 项目覆盖:158 条全局默认开箱即用,项目可新增带 projectId 的 Model 覆盖(自研/私有/折扣价)。把分级策略放进数据而非代码——呼应第 16/28 课「规则即数据」。

🧪 自测 · 想一想为什么这么设计

1. Langfuse 给每个模型配一条 matchPattern 正则(如 gpt-4o 的 (?i)^(openai/)?(gpt-4o)$),而不是用精确字符串匹配模型名。为什么?
  1. 模型命名极度碎片化:同一模型在 OpenAI/Bedrock/Vertex 有 provider 前缀、us./eu. 区域前缀、-v1:0/@date 版本等十几种合法写法;一条正则把这些同义写法收敛成一个意图,精确匹配则要为每种写法各存一条、维护爆炸
  2. 正则匹配更快
  3. 正则能加密模型名
  4. 为了支持模糊搜索
看答案与解析 点击展开
答案:A。同一个 Claude 在 OpenAI 风格叫 claude-3-5-sonnet、Bedrock 叫 us.anthropic.claude-3-5-sonnet-v1:0、Vertex 叫 claude-...@20240620,加大小写和 provider/ 前缀排列,轻松十几种写法。精确匹配要为每种各存一条价目,加区域/改版本就全表翻新、极易漏。一条 (?i)^…$ 全串正则把同义写法收敛成「无论你怎么称呼,我都认得这是 gpt-4o」——对抗命名碎片化的必需品。
2. Langfuse 的 pricingTiers 把定价分成「恰一个默认档 + 任意条件档」。matchPricingTier 是怎么选出该用哪个档的?
  1. 随机选一个档
  2. 总是用最便宜的档
  3. 用 priority 最高的档
  4. 滤掉默认档,把条件档按 priority 升序逐个评估(每档 conditions 是 AND 逻辑、全满足才中),第一个全满足的胜出;都不满足就回落默认档——保证任何用量都能算出价
看答案与解析 点击展开
答案:D。matchPricingTier(matcher.ts:88):① 滤掉 isDefault 档、按 priority 升序排序非默认档;② 逐个评估其 conditions(evaluateConditions 用 AND——全部 operator(gt/gte/lt/lte/eq/neq) 比较通过才算中);③ 返回第一个全满足的档;④ 都不中则返回默认档。默认档永远存在、永远兜底,保证任何用量都落进某个价、绝不漏算。这把「分级定价策略」表达成数据而非 if-else。
3. default-model-prices.json 里有 158 条模型价目,但一个项目用了自研模型、谈了折扣价。Langfuse 怎么兼顾「开箱即用」和「贴合真实账单」?
  1. 必须改 JSON 文件重新部署
  2. 只能用内置的 158 个模型
  3. 折扣价不支持
  4. 158 条是 projectId 为空的全局默认价(upsert 进 Model/Price 表,开箱即用);项目可新增带 projectId 的 Model 条目覆盖(自研/私有/折扣价优先)——默认 + 项目覆盖两全
看答案与解析 点击展开
答案:D。default-model-prices.json 的 158 条是 Langfuse 内置默认价,由 upsertDefaultModelPrices 灌进 DB 的 Model/Price 表(projectId 为空=全局)。项目可在自己项目下新增带 projectId 的 Model 条目,优先于全局默认——于是主流模型价开箱即备好,自研/私有/折扣场景又能精确覆盖。add-model-price 仓库技能管的就是怎么安全往种子价里增改条目。
💭 发散思考(没有标准答案,动手或动脑想想)
  • 模型定价把「分级策略」表达成数据(带 conditions 的 tier)而非代码里的 if-else,这是第 16/28 课「规则即数据」的又一次回响。回想你做过的系统:哪些「业务规则」其实更适合放进数据/配置而非硬编码?「规则即数据」带来灵活,但也有代价(要设计一套表达规则的 schema、要写解释器、调试更绕)。你会用什么标准判断一条规则该进代码还是进数据?

Lesson 16 said "cost = usage × price", but left a cliffhanger: where does the price come from, and how does a wildly-named model name match its price list? This lesson fills that piece—Langfuse's pricing data model. Three focuses: matchPattern uses one regex to recognize the same model under every platform's quirky naming; pricingTiers uses "a default tier + conditional tiers" to support tiered pricing by usage / context window; and prices is a fine price list "per usage type, per token" (input, output, cache reads… each with its own price). It directly feeds Lesson 16's cost computation: take a call's model name, match its price list, select the right tier by usage, and compute "price × usage" to get how much this call cost.

📋 Analogy
Model pricing is like a "cross-country taxi fare manual". The same car has a pile of different names across cities and ride apps—gpt-4o, openai/gpt-4o, us.anthropic.claude-...-v1:0. The manual uses one "recognition rule" (the matchPattern regex) to recognize all these aliases as the same car, matching one fare table. And the fare isn't flat: a short city trip at one rate, but if your trip is especially long (say a context window over 200K tokens), it jumps to a "long-haul tier" at another rate (pricingTiers' conditional tiers). At checkout, the manual sums "how much × rate per unit" item by item—and bills line-by-line: base fare, mileage, waiting each separately (input tokens, output tokens, cache hits each have their own rate). One manual handles "identify the car + pick the tier + itemized billing" in one go.

matchPattern: one regex to recognize every alias

The same model is named differently on OpenAI, Anthropic, AWS Bedrock, Google Vertex, and Azure: some carry an openai/ prefix, some a us. region prefix, some a -v1:0 version suffix, some an @date. Matching the price list by "exact name" would mean storing one entry per spelling—a maintenance nightmare. Langfuse's solution gives each model a case-insensitive, full-string regex matchPattern that catches every valid spelling.

every alias → one regex → one price list gpt-4o (bare name) openai/gpt-4o (provider prefix) us.anthropic.claude-...-v1:0 (Bedrock) claude-...@20240620 (Vertex date) matchPattern(?i)^(provider/)?(...)$case-insensitive · full-string one Model entrytokenizerId + pricingTierssame car → same price list regex parts: (?i) case-insensitive · ^…$ full-string · (provider/)? optional prefix · (us.|eu.|apac.)? region · (:0)? / @date version
one regex unifies fragmented naming: each default-model-prices.json entry carries a matchPattern, e.g. gpt-4o's (?i)^(openai/)?(gpt-4o)$. With (?i) case-insensitive, ^…$ full-string, optional provider/region prefixes and version suffixes, it catches all spellings of one model across six platforms. Source: worker/src/constants/default-model-prices.json.
Example: model pricing (per 1M tokens) modelName input $/1M output $/1M output price gpt-4o $2.50 $10.00 $10.00 gpt-4o-mini $0.15 $0.60 $0.60 claude-3-5-sonnet-20240620 $3.00 $15.00 $15.00 claude-3-haiku-20240307 $0.25 $1.25 $1.25 gpt-4-turbo $10.00 $30.00 $30.00 how a model maps to a price? the matchPattern regex usage.model = "openai/gpt-4o" ──match──▶ (?i)^(openai/)?(gpt-4o)$ ✓ → gpt-4o prices stored per-token in pricingTiers[].prices.{input,output} (shown x1e6 as per-million); at ingestion the regex maps the model name to a price row
Pricing is a "regex → per-token price" lookup table (data from worker/src/constants/default-model-prices.json, real prices): each model stores input/output price per token under pricingTiers[].prices (shown x1e6 as "per million" for readability). On output price alone, gpt-4-turbo ($30) is 50x gpt-4o-mini ($0.60) — model choice is a cost choice. How does a model name map to a price? Via the matchPattern regex: "openai/gpt-4o" matches (?i)^(openai/)?(gpt-4o)$ and uses that row, so it bills correctly with or without the openai/ prefix (used by Lesson 12's ingestion cost step).
worker/src/constants/default-model-prices.jsonone model entry (excerpt)
// one of 158 seed prices; matchPattern catches every alias
{
  "modelName": "gpt-4o",
  "matchPattern": "(?i)^(openai/)?(gpt-4o)$",   // matches gpt-4o / openai/gpt-4o / GPT-4O
  "tokenizerId": "openai",                        // if the SDK didn't report usage, count tokens with this tokenizer
  "pricingTiers": [ { "name": "Standard", "isDefault": true, "conditions": [],
    "prices": { "input": 2.5e-6, "output": 1e-5,             // per-token prices
                "input_cached_tokens": 1.25e-6, … } } ]      // line items: input/output/cache-read each priced
}
// an Anthropic matchPattern also includes us./eu./apac. region prefixes and -v1:0, @date version forms
regex partpurposespellings it recognizes
(?i)case-insensitiverecognizes both gpt-4o and GPT-4O
^…$full-string matchavoids false hits (won't treat gpt-4o-mini as gpt-4o)
(provider/)?optional provider prefixopenai/gpt-4o, anthropic/claude-…
(us\.|eu\.|apac\.)?optional AWS region prefixus.anthropic.claude-… (Bedrock)
(:0)? / @dateoptional version suffix…-v1:0 (Bedrock), …@20240620 (Vertex)

These parts are like detachable Lego: when a platform adds a new naming quirk, just add an optional group to the regex—no need to rearrange the price table. One rule absorbs the naming differences of six platforms at the pattern layer.

pricingTiers: a default tier + conditional tiers

Pricing is often not flat. For the same model, a larger context window may cost more; some bill by usage steps. Langfuse uses pricingTiers for this tiering: each model has exactly one default tier (isDefault=true, priority=0, conditions=[]), plus any number of conditional tiers (with conditions, e.g. "input tokens > 200K"). To pick, matchPricingTier evaluates conditional tiers in ascending priority, the first whose conditions all hold wins; if none, it falls back to the default tier.

pick a tier by usage: conditional first, fall back to default if none this call's usageinput=250k, output=8k conditional (priority 1): input > 200000 ?holds ✓ → pick it (long-context price) conditional (priority 2): … (prior one won, not tried) default (priority 0, conditions=[]): fallback when none hold the tier's prices→ Lesson 16 cost conditions use AND logic (all must hold); operator supports gt/gte/lt/lte/eq/neq; first-come-first-served by ascending priority the default tier always exists and always backstops—guaranteeing any usage gets a price, never uncomputed
tiered pricing, default backstop: matchPricingTier (pricing-tiers/matcher.ts:88) filters out the default tier, evaluates conditional tiers in ascending priority, the first with all conditions holding (AND logic) wins, else falls back to default. A condition = usageDetailPattern (regex selecting a usage item) + operator (gt/gte/lt/lte/eq/neq) + value.
tier typeisDefault / priority / conditionsrole
default (exactly one per model)true / 0 / []backstop price—any usage gets computed, never missed
conditional (any number)false / ≥1 / has conditionstier by context window/usage (e.g. ">200K tokens → long-context price")
selection rule: evaluate conditional tiers in ascending priority, first with all conditions holding wins; none → default

Put together: how a call's cost is computed

Chaining matchPattern and pricingTiers restores Lesson 16's full "cost = usage × price" chain. The 158 seed prices in default-model-prices.json are loaded into the DB's Model/Price tables by upsertDefaultModelPrices; thereafter every call follows this pricing line:

1 model name us.anthropic .claude-… 2 match Model matchPattern regex → tiers + tokenizer 3 count tokens (if SDK omitted) tokenizerId 4 pick tier matchPricingTier → prices 5 sum per item prices[i] × usage added up total cost = Σ(price per item × usage per item) restoring Lesson 16's full "cost = usage × price" chain
1

get the model name

From an observation (generation), take its reported model name, e.g. us.anthropic.claude-...-v1:0.

2

match the Model

Test the name against each matchPattern regex; the hit is the price entry to use (with tokenizerId and pricingTiers).

3

(if needed) count tokens

If the SDK didn't report usage, use the entry's tokenizerId/tokenizerConfig to tokenize the text and count tokens (Lesson 16).

4

select the tier

matchPricingTier picks the right tier by usage (default or conditional), yielding the prices list to use this time.

5

itemized cost

For each usage type (input/output/cache-read…) sum prices[item] × usage, giving the call's total cost.

One more layer of flexibility: these 158 are just Langfuse's built-in defaults (global Models with empty projectId). If you use a self-built model, a private deployment, or negotiated special discounts, you can add/override Model prices in your own project (project-scoped entries with a projectId take priority)—so the platform is both ready out of the box (mainstream model prices preloaded) and precisely fitted to your real bill. The repo skill add-model-price governs exactly how to safely add/edit entries in this seed file (generate a UUID, write the right matchPattern, validate the JSON).

🎯 Design trade-off
Why match model names with regex rather than exact string equality? Because model naming in the ecosystem is maddeningly fragmented. The same Claude is claude-3-5-sonnet in OpenAI style, us.anthropic.claude-3-5-sonnet-v1:0 on Bedrock, claude-3-5-sonnet@20240620 on Vertex, plus permutations of casing and provider/ prefixes—a model easily has a dozen valid spellings. With exact matching, you'd store one price entry per spelling, and adding a region or bumping a version means overhauling the whole table—maintenance explodes and misses abound. One carefully written regex converges these synonymous spellings into one intent: "however you call it, I know this is gpt-4o". Regex here isn't showing off but a necessity against naming fragmentation—it keeps "the world's chaos" outside the price table.

Why split pricing into "default + conditional tiers" rather than a flat price? Because real pricing is tiered: long context costs more, cache hits cost less, some usage jumps tiers above a threshold. Storing only a flat price would either miscompute or fill the billing code with if-else—hardcoding pricing policy into logic, so every price change means a code change and a release. Expressing tiering as data (a set of tiers with conditions) leaves the billing logic just one job: pick the right tier by usage, while "how tiers split, how prices are set" lives in data, adjustable anytime. Another win for "move policy from code into data"—the same lineage as Lesson 28's score config and Lesson 16's pricing schema: a rule expressible as data shouldn't be written into code. And the ever-present default tier is an elegant backstop: it guarantees any usage lands in some price, never failing to compute just because no condition matched.
🎯 Key points
  • The pricing data model feeds Lesson 16's cost computation: default-model-prices.json's 158 seed prices are upserted into the Model/Price tables, and every call computes "usage × price" from them.
  • matchPattern unifies naming: one case-insensitive, full-string regex per model catches all the spellings of OpenAI/Anthropic/Bedrock/Vertex/Azure/Gemini (provider prefix, region prefix, version suffix, @date).
  • pricingTiers tier pricing: exactly one default tier (backstop) + any conditional tiers; matchPricingTier evaluates conditions in ascending priority (AND logic, operators gt/gte/lt/lte/eq/neq), first all-holding wins, else default.
  • prices bills line-by-line: each usage type (input/output/cache-read…) has its own per-token price; cost = Σ(per-item price × per-item usage).
  • defaults + project override: 158 global defaults ready out of the box, projects can add projectId-scoped Models to override (self-built/private/discount prices). Policy in data not code—echoing Lessons 16/28's "rules as data".

🧪 Self-test - think about the design

1. Langfuse gives each model a matchPattern regex (e.g. gpt-4o's (?i)^(openai/)?(gpt-4o)$) instead of exact string matching. Why?
  1. model naming is highly fragmented: the same model has provider prefixes, us./eu. region prefixes, -v1:0/@date versions—a dozen valid spellings across OpenAI/Bedrock/Vertex; one regex converges these synonyms into one intent, whereas exact matching needs one entry per spelling, exploding maintenance
  2. regex matching is faster
  3. regex can encrypt model names
  4. to support fuzzy search
Show answer & explanation click to expand
Answer: A. The same Claude is claude-3-5-sonnet in OpenAI style, us.anthropic.claude-3-5-sonnet-v1:0 on Bedrock, claude-...@20240620 on Vertex, plus casing and provider/ prefix permutations—easily a dozen spellings. Exact matching needs one price entry per spelling; adding a region or bumping a version overhauls the table and misses abound. One (?i)^…$ full-string regex converges synonyms into 'however you call it, I know this is gpt-4o'—a necessity against naming fragmentation.
2. Langfuse's pricingTiers splits pricing into 'exactly one default tier + any conditional tiers'. How does matchPricingTier pick which tier to use?
  1. picks a random tier
  2. always uses the cheapest tier
  3. uses the highest-priority tier
  4. filter out the default tier, evaluate conditional tiers in ascending priority (each tier's conditions are AND logic, all must hold), the first all-holding wins; if none hold, fall back to the default tier—guaranteeing any usage gets a price
Show answer & explanation click to expand
Answer: D. matchPricingTier (matcher.ts:88): ① filter out the isDefault tier, sort non-default tiers by ascending priority; ② evaluate each one's conditions (evaluateConditions uses AND—all operator(gt/gte/lt/lte/eq/neq) comparisons must pass to match); ③ return the first all-holding tier; ④ if none, return the default. The default always exists and backstops, guaranteeing any usage lands in a price. This expresses 'tiered pricing policy' as data, not if-else.
3. default-model-prices.json has 158 model prices, but a project uses a self-built model with a negotiated discount. How does Langfuse balance 'ready out of the box' with 'fits the real bill'?
  1. you must edit the JSON and redeploy
  2. only the built-in 158 models can be used
  3. discount prices aren't supported
  4. the 158 are global defaults with empty projectId (upserted into Model/Price tables, ready out of the box); a project can add projectId-scoped Model entries to override (self-built/private/discount prices take priority)—defaults + project override, both covered
Show answer & explanation click to expand
Answer: D. The 158 in default-model-prices.json are Langfuse's built-in defaults, loaded into the DB's Model/Price tables by upsertDefaultModelPrices (empty projectId = global). A project can add projectId-scoped Model entries that take priority over global defaults—so mainstream prices are preloaded while self-built/private/discount cases override precisely. The add-model-price repo skill governs how to safely add/edit entries in the seed file.
💭 Open questions (no single right answer - just think or try)
  • Model pricing expresses 'tiering policy' as data (tiers with conditions) rather than if-else in code—another echo of Lessons 16/28's 'rules as data'. Recall systems you've built: which 'business rules' are actually better as data/config than hardcoded? 'Rules as data' brings flexibility but also costs (designing a schema to express rules, writing an interpreter, harder debugging). By what criteria would you decide whether a rule goes into code or into data?