第 16 课说过「成本 = 用量 × 单价」,但留了个悬念:单价从哪来?一个叫法五花八门的模型名,又怎么对上它的价目? 这一课补上这块拼图——Langfuse 的定价数据模型。重点有三:matchPattern 用一条正则把同一个模型在各家平台千奇百怪的命名统一认出来;pricingTiers 用「默认档 + 条件档」支持按用量/上下文窗口分级定价;而 prices 是一张「每种用量、每个 token」的细价目(输入、输出、缓存读……各有各的价)。
它直接喂养第 16 课的成本计算:拿一条调用的模型名去匹配出价目、按用量选中合适的档、再用「单价 × 用量」算出这次花了多少钱。
gpt-4o、openai/gpt-4o、us.anthropic.claude-...-v1:0。手册用一条「识别规则」(matchPattern 正则)把这些花名全认成同一辆车,对上同一份价目。
而车费不是一口价:市区短途一个价,但要是你这趟特别长(比如上下文窗口超过 20 万 token),就跳到「长途档」按另一套价算(pricingTiers 的条件档)。最后结账时,手册按「跑了多少 × 每单位多少钱」逐项相加——而且分项计费:起步价、里程费、等待费各算各的(输入 token、输出 token、缓存命中各有单价)。一本手册,把「认车 + 分档 + 分项计费」三件事一次办妥。
同一个模型,在 OpenAI、Anthropic、AWS Bedrock、Google Vertex、Azure 上的命名规则各不相同:有的带 openai/ 前缀、有的带 us. 区域前缀、有的带 -v1:0 版本后缀、有的用 @日期。如果用「精确名」去匹配价目,就得为每种写法各存一条,维护噩梦。Langfuse 的解法是给每个模型配一条大小写不敏感、全串匹配的正则 matchPattern,一网打尽所有合法写法。
default-model-prices.json 的每个条目都带一条 matchPattern,如 gpt-4o 的 (?i)^(openai/)?(gpt-4o)$。它用 (?i) 不分大小写、^…$ 全串匹配、可选的 provider/区域前缀与版本后缀,把同一模型在六大平台的所有写法一网打尽。源码:worker/src/constants/default-model-prices.json。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 课摄取算成本就用它)。// 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) |
这几个组件像一套「可拆装的乐高」:哪家平台多了种命名花样,往正则里加一段可选组就行,无需重排价目表。一条规则,把六大平台的命名差异吸收在了模式层。
定价往往不是一口价。同一个模型,上下文窗口越大、单价可能越高;有的还按用量阶梯计费。Langfuse 用 pricingTiers(价目档)支持这种分级:每个模型有恰好一个默认档(isDefault=true、priority=0、conditions=[]),外加任意个条件档(带 conditions,如「输入 token > 20 万」)。选档时,matchPricingTier 把条件档按 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 表;之后每条调用都走这条计价线:
从一条 observation(generation)取出它报告的模型名,如 us.anthropic.claude-...-v1:0。
用模型名去试各条 matchPattern 正则,命中的就是这次该用的价目条目(含 tokenizerId 与 pricingTiers)。
若 SDK 没报用量,用条目的 tokenizerId/tokenizerConfig 把文本分词、数出 token 数(第 16 课)。
matchPricingTier 按用量选中合适的 tier(默认或条件档),拿到这次该用的 prices 价目。
对每种用量(输入/输出/缓存读…)用 prices[该项] × 用量 逐项相加,得这次调用的总成本。
还有一层灵活:这 158 条只是 Langfuse 内置的默认价(projectId 为空的全局 Model)。如果你用的是自研模型、私有部署、或谈了特殊折扣,可以在自己项目里新增/覆盖 Model 价目(带 projectId 的项目级条目优先)——于是平台既开箱即用(主流模型价已备好),又能精确贴合你的真实账单。add-model-price 这个仓库技能,管的就是怎么安全地往这份种子价里增改条目(生成 UUID、写对 matchPattern、校验 JSON)。
claude-3-5-sonnet、Bedrock 上叫 us.anthropic.claude-3-5-sonnet-v1:0、Vertex 上叫 claude-3-5-sonnet@20240620,再加上大小写、provider/ 前缀的排列组合,一个模型轻松有十几种合法写法。若用精确匹配,你得为每种写法各存一条价目,加个新区域、改个版本号就得全表翻新——维护成本爆炸,还极易漏。一条精心写的正则把这些同义写法收敛成一个意图:「无论你怎么称呼它,我都认得这是 gpt-4o」。正则在这里不是炫技,而是对抗命名碎片化的必需品——它把「世界的混乱」挡在了价目表之外。default-model-prices.json 158 条种子价被 upsert 进 Model/Price 表,每条调用据此算「用量 × 单价」。matchPricingTier 按 priority 升序评条件(AND 逻辑、operator gt/gte/lt/lte/eq/neq),第一个全满足者胜,否则回落默认。projectId 的 Model 覆盖(自研/私有/折扣价)。把分级策略放进数据而非代码——呼应第 16/28 课「规则即数据」。
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.
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.
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.
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.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).// 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 part | purpose | spellings it recognizes |
|---|---|---|
(?i) | case-insensitive | recognizes both gpt-4o and GPT-4O |
^…$ | full-string match | avoids false hits (won't treat gpt-4o-mini as gpt-4o) |
(provider/)? | optional provider prefix | openai/gpt-4o, anthropic/claude-… |
(us\.|eu\.|apac\.)? | optional AWS region prefix | us.anthropic.claude-… (Bedrock) |
(:0)? / @date | optional 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.
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.
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 type | isDefault / priority / conditions | role |
|---|---|---|
| default (exactly one per model) | true / 0 / [] | backstop price—any usage gets computed, never missed |
| conditional (any number) | false / ≥1 / has conditions | tier 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 | ||
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:
From an observation (generation), take its reported model name, e.g. us.anthropic.claude-...-v1:0.
Test the name against each matchPattern regex; the hit is the price entry to use (with tokenizerId and pricingTiers).
If the SDK didn't report usage, use the entry's tokenizerId/tokenizerConfig to tokenize the text and count tokens (Lesson 16).
matchPricingTier picks the right tier by usage (default or conditional), yielding the prices list to use this time.
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).
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.default-model-prices.json's 158 seed prices are upserted into the Model/Price tables, and every call computes "usage × price" from them.matchPricingTier evaluates conditions in ascending priority (AND logic, operators gt/gte/lt/lte/eq/neq), first all-holding wins, else default.projectId-scoped Models to override (self-built/private/discount prices). Policy in data not code—echoing Lessons 16/28's "rules as data".