🪢 Langfuse 图解教程Langfuse Visual Guide 第三部分 · 摄取链路Part 3 · The Ingestion Path 16 / 55
第三部分 · 摄取链路Part 3 · The Ingestion Path

Token 计数与成本Token counting & cost

🧭 读前Before you start
先读 第13课 的事件字段,本课讲 token 计数与成本是怎么算出来的。
Read Lesson 13 on event fields; this lesson covers how token counts and cost are computed.

第 15 课合并到第 6 步时一句带过:「算 token 与成本」。这一课就把那一步摊开。核心是一条贯穿 Langfuse 的原则——provided 压倒 computed: 你(或 LLM 提供商)报上来的 usage / cost 一律优先采信;只有你什么都没报时,Langfuse 才亲自动手:用分词器数 token、 按价目表算钱。这正是第 3 课 provided_* 与 usage_details 两套字段的来历,也是第 8 课那些 provided_* Map 列存在的理由。

📞 生活类比
把它想成话费账单。如果运营商已经把账单逐项列好(你这月打了多少分钟、各花多少钱),系统直接照单归档——这就是 provided。 可如果你手里只有一堆通话记录、没有金额,系统就得自己来:先数通话时长(分词、数 token),再翻出这个套餐的资费表(模型价格) 算出应付多少——这就是 computed。注意:只要运营商给了哪怕一项金额,系统就全盘采信它的账单,绝不把「自己估的」和「运营商给的」掺着用—— 两套数字混在一起,账就乱了。

getGenerationUsage:四步流水线

合并出 observation 后,getGenerationUsage 接手,把「模型 → token → 价格 → 成本」串成一条流水线。每一步的输入都可能来自你上报的 provided_*, 也可能由系统补算:

getGenerationUsage:模型 → token → 价格 → 成本 ① findModelprovided_model_name~ match_pattern 正则 ② getUsageUnits有 provided 就用否则分词数 token ③ matchPricingTier按 usage 选价目层每种用量一个单价 ④ calculateUsageCosts有 provided cost 就用否则 price×units 产物:usage_details · cost_details · total_cost+ internal_model_id · 价目层 id/名
一条四步流水线:findModel 用正则把模型名匹配到价目 → getUsageUnits 取/算 token → matchPricingTier 选价目层 → calculateUsageCosts 算钱。产物写进 observation 的 usage_details/cost_details。源码:IngestionService/index.ts:1057-1143。
示例:token 用量与成本的拆解 usageDetails(token) input 512 tok output 88 = 600 tok costDetails(成本) input·单价 output·单价(通常更贵) total_cost ≈ $0.0041 cost = input_tok × in_price + output_tok × out_price 单价按 model 从定价表查(match-pattern);usage 没带就按 tokenizer 估,再算钱
token 决定钱(字段对齐 domain/observations.ts 的 usageDetails/costDetails,单价结构对齐 worker/src/constants/default-model-prices.json;值为示例):先把用量拆成 input/output token,再按该 model 的各自单价折算成 costDetails 求和得 total_cost。注意 output 单价通常比 input 贵,所以「话多」比「读得多」更费钱。若命中提示缓存,input 走更低的 input_cached_tokens 阶梯价(gpt-4o 约 $1.25/1M,为普通 input 的一半)。
worker/src/services/IngestionService/index.tsgetGenerationUsage
// ① 模型匹配:拿 provided_model_name 去 models 表正则匹配
const { model, pricingTiers } = provided_model_name
  ? await findModel({ projectId, model: provided_model_name })
  : { model: null, pricingTiers: [] };

// ② 取/算 token;③ 选价目层;④ 算成本
const usage = await getUsageUnits(observationRecord, model);
const tier  = matchPricingTier(pricingTiers, usage.usage_details);
const cost  = IngestionService.calculateUsageCosts(tier.prices, obs, usage.usage_details);

return { ...usage, ...cost, internal_model_id: model?.id, /* 价目层 */ };

provided 压倒 computed:两条岔路

token 和成本各有一条「provided 还是 computed」的岔路,规则一模一样:你给了,就用你的;你没给,系统才算。但「系统才算」是有前提的。

token / 成本:provided 优先,computed 兜底 你上报了吗?provided_usage / provided_cost ✅ 给了 → 直接采用(provided)usage:原样收下你的 token 数cost:只要给了任一项,全用你的绝不和系统估算掺用 🤖 没给 → 系统补算(computed)前提:匹配到模型 且 level≠ERRORtoken:用 model.tokenizerId 分词计数cost:Σ 价格[用量类型] × 数量 是 否 估算只是兜底,永远不会覆盖你上报的真实值——可信来源优先
估算是兜底,不是覆盖:provided 永远优先;computed 只在「匹配到模型 + 非 ERROR + 你没提供」三条同时满足时才发生。源码:getUsageUnits(index.ts:1145+,分词条件)与 calculateUsageCosts(「给了任一 cost 点就不再算」)。
维度provided(你上报)computed(系统补算,仅当你没给)
token 用量原样采用 provided_usage_details匹配到模型 + level≠ERROR 时,用 tokenizerId 对 input/output 分词计数
成本给了任一 cost 点 → 全用 provided_cost_details,不再算别的对每种用量:价格 × 数量,加总为 total_cost
为什么这样来源更可信(LLM API 常返回精确 token 数)无来源时的合理估计,不能与 provided 混用

落到真实埋点里,这两条岔路对应两种很常见的接入方式:

🅰️ 官方 SDK:走 provided

你用 OpenAI / Anthropic 官方 SDK,响应体里自带 usage(精确的 prompt/completion token 数)。SDK 把它原样上报,Langfuse 照单全收——既准确又省去分词开销。这是最理想的情况。

🅱️ 裸 HTTP / 自建:走 computed

你直接拼 HTTP 调一个模型、或用了不返回 usage 的网关,埋点里没有 token 数。只要模型名能匹配上、且不是 ERROR,Langfuse 就替你分词数 token、按价目算钱,让你照样有用量和成本可看。

举个具体的数:一次 gpt-4o 调用,computed 路径数出 input 1,000 token、output 500 token;价目表里这个模型 input 单价 $2.5/百万、output $10/百万, 于是 cost_details = { input: 0.0025, output: 0.005, total: 0.0075 }。如果你的 SDK 本就报了 usage = {input:1000, output:500}, 第②步就跳过分词、直接用这两个数;如果你连 cost 也报了,第④步连乘法都省了,整条成本直接采信你的。能少算就少算,能信你就信你。

第③步 matchPricingTier 是较新的精细化能力:一个模型可以有多个价目层,每层带一组条件——把 usage 里匹配某模式的用量求和、 和阈值比较(>、>=、< 等,多条件 AND)。于是「超过 128k token 走长上下文高价」「缓存 token 走折扣价」这类按量分层定价就能精确表达。 选中的层提供每种用量类型的单价,第④步再据此算钱。没有命中任何层,就退回模型的基础价格——分层只是在「一价到底」之上多了一层灵活度。

模型匹配:一条正则找到价目

computed 这条路的起点,是把你上报的模型名字符串(如 gpt-4o-2024-08-06)对应到一条价目记录。这事由 findModel 在 Postgres 的 models 表上完成,靠的是一句正则匹配:

上报模型名 gpt-4o-2024-08-06 findModel(...) Postgres models 表 · 每行一条正则 match_pattern (?i)^(openai/)?(gpt-4o-2024-08-06)$ ✓命中 (?i)^(openai/)?(gpt-4o)$ (?i)^(claude-3-5-sonnet-.*)$ WHERE project_id=? OR NULL ORDER BY project_id, start_date DESC · LIMIT 1 命中行 → 价格 tiers → tokenizer_id (定分词器) 项目模型盖过全局、新价盖过旧价:正则给灵活、project_id 给覆盖权、start_date 给时间维度
怎么匹配model ~ match_pattern
每条 model 记录带一个正则 match_pattern;用 Postgres 的 ~ 运算符拿你的模型名去套。正则允许大小写不敏感、可选的提供商前缀等灵活写法。
谁优先项目模型 > 全局模型
WHERE project_id = ? OR project_id IS NULL,再 ORDER BY project_id——你自定义的模型价格盖过内置的,方便覆盖默认定价或加自有模型。
取哪条start_date 最新
ORDER BY start_date DESC + LIMIT 1:价格会随时间调整,匹配时取生效日期最近的那一条,于是历史数据按当时价、新数据按新价。

匹配上的 model 记录里,除了价格,还有一个关键字段 tokenizer_id——它决定了 computed 时用哪种分词器数 token(不同模型家族分词规则不同)。 分词本身在 worker/src/features/tokenisation 里,甚至放进独立 worker 线程跑(tokenCountAsync),免得大文本分词阻塞主流程;失败再退回同步 tokenCount。 这又是第 8 课 provided_* 与计算列分离的写入侧来源:provided 进 provided_usage_details,算出来的进 usage_details,互不污染。

看一条真实的内置规则(Langfuse 自带约 150+ 个模型价格):gpt-4o 的 match_pattern 是 (?i)^(openai/)?(gpt-4o)$—— 大小写不敏感、可带可不带 openai/ 前缀,精确匹配 gpt-4o。而带日期的版本(如 gpt-4o-2024-05-13)有各自独立的价格行, 因为不同发布日的定价可能不同。这套「正则 + 内置价目表」让 Langfuse 开箱即认得主流模型,你也能在 models 表里加自己的行覆盖或扩展—— 正则给了灵活度,project_id 优先级给了覆盖权,start_date 给了时间维度。三者合起来,就是一套能跟着模型市场演进的活的定价系统。

🎯 设计取舍
为什么铁了心「provided 压倒 computed」,而不是两者求平均、或谁更大用谁? 因为这是一个关于数据可信度的立场。LLM 提供商在响应里返回的 token 数, 是计费的权威依据——它知道自己怎么切的 token,比 Langfuse 在外面用通用分词器估的准得多。系统的本分是如实记录,而不是用自己的估算去「纠正」权威来源。 于是规则被设计得毫不含糊:有 provided 就一字不改地用,连成本都「给了一项就全用你的」,绝不把估算掺进去——因为半真半估的账,比纯估算更危险(你会误以为它精确)。 computed 只在信息真空(你啥也没给)时登场,当一个合理的兜底。可信优先、估算兜底、两者隔离——这条原则贯穿 usage 与 cost 的每一个角落。
🎯 本课要点
  • getGenerationUsage 四步:findModel(匹配模型/价格)→ getUsageUnits(取/算 token)→ matchPricingTier(选价目层)→ calculateUsageCosts(算钱)。
  • provided 压倒 computed:你上报了 usage/cost 就原样用;成本只要给了任一项就全用你的,绝不与估算混用。
  • computed 有前提:匹配到模型 + level≠ERROR + 你没提供,才用 tokenizerId 对 input/output 分词计数。
  • findModel:Postgres ~ match_pattern 正则匹配;项目模型盖过全局,start_date 最新者胜——历史按旧价、新数据按新价。
  • 分词在 worker/src/features/tokenisation,可走独立线程(tokenCountAsync);结果分别落第 8 课的 provided_* 与 usage_details 列,互不污染。

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

1. 你的 SDK 上报了 usage={input:1000, output:500},也报了 cost={total: 0.01}。Langfuse 会怎么处理 token 数和成本?
  1. 把你报的和系统估算的取平均
  2. 都直接采用你报的:token 跳过分词、用你的 1000/500;成本因为你给了至少一项,全用你的 0.01,连价目表都不查——provided 压倒 computed
  3. 忽略你报的,一律自己算
  4. 重新分词验证你的 token 数,再按价目表重算成本
看答案与解析 点击展开
答案:B。provided 压倒 computed 是贯穿 Langfuse 的原则。getUsageUnits 见有 provided_usage 就直接用;calculateUsageCosts 见“给了任一 cost 点”就全用 provided、不再算别的。系统从不把估算和你报的真实值混用,因为半真半估的账更危险。
2. 你用裸 HTTP 调了一个模型,埋点里没有任何 usage。什么情况下 Langfuse 会替你分词数 token?
  1. 只有当 level=ERROR 时才分词
  2. 三条同时满足:模型名能正则匹配到一条 model 记录、observation 不是 ERROR、且你没提供 usage——这时用该模型的 tokenizerId 对 input/output 分词计数
  3. 只有付费版才会分词
  4. 任何时候都会无条件分词
看答案与解析 点击展开
答案:B。computed 是有前提的兜底:匹配到模型(才知道用哪种 tokenizer、什么价)、非 ERROR(错误生成没必要算)、且无 provided。三者缺一就不分词。分词用 model.tokenizerId,在 worker/src/features/tokenisation 里、可走独立线程以免阻塞。
3. findModel 把模型名匹配到价目时,如果一个项目自定义了 gpt-4o 的价格,同时内置也有 gpt-4o,会用哪条?
  1. 用项目自定义的:SQL 是 project_id = ? OR project_id IS NULL,再 ORDER BY project_id,项目模型排在全局(NULL)前面;同名下还按 start_date 最新取一条
  2. 两条价格相加
  3. 随机选一条
  4. 用内置的,自定义会被忽略
看答案与解析 点击展开
答案:A。findModel 用 Postgres 正则 ~ match_pattern 匹配,WHERE 同时取项目模型与全局(project_id IS NULL),ORDER BY project_id 让自定义优先、start_date DESC 取最新生效价、LIMIT 1。于是项目能覆盖默认定价或加自有模型,历史数据按当时价、新数据按新价。
💭 发散思考(没有标准答案,动手或动脑想想)
  • “可信来源优先、估算只做兜底、两者严格隔离”——这条原则不止用于 token/成本。想想你自己的系统里,有没有“用户/上游给的权威数据”和“本地推断/估算”并存的场景?你会怎么设计它们的优先级与隔离,避免半真半估造成的误判?

At step 6 of Lesson 15's merge we said in passing: "compute tokens & cost". This lesson unfolds that step. The core is a principle running through all of Langfuse — provided beats computed: the usage / cost you (or the LLM provider) report are always trusted first; only when you report nothing does Langfuse roll up its sleeves: count tokens with a tokenizer, price it from a rate card. This is the origin of Lesson 3's two field sets provided_* and usage_details, and the reason Lesson 8's provided_* Map columns exist.

📞 Analogy
Think of a phone bill. If the carrier already itemized it (how many minutes you used, the charge for each), the system just files it as is — that's provided. But if all you have is a pile of call logs with no amounts, the system must do it itself: first count the call durations (tokenize, count tokens), then pull up this plan's rate card (model prices) and compute what's owed — that's computed. Note: the moment the carrier gives even one amount, the system trusts its bill wholesale, never mixing "its own estimate" with "the carrier's figure" — mixing the two scrambles the books.

getGenerationUsage: a four-step pipeline

Once the observation is merged, getGenerationUsage takes over, threading "model → tokens → price → cost" into one pipeline. Each step's input may come from your reported provided_*, or be backfilled by the system:

getGenerationUsage: model → tokens → price → cost ① findModelprovided_model_name~ match_pattern regex ② getUsageUnitsuse provided if anyelse tokenize-count ③ matchPricingTierpick tier by usageunit price per usage type ④ calculateUsageCostsuse provided cost if anyelse price×units outputs: usage_details · cost_details · total_cost+ internal_model_id · pricing tier id/name
A four-step pipeline: findModel regex-matches the model name to a price → getUsageUnits takes/counts tokens → matchPricingTier picks the tier → calculateUsageCosts computes the cost. The outputs land in the observation's usage_details/cost_details. Source: IngestionService/index.ts:1057-1143.
Example: token usage and cost breakdown usageDetails (tokens) input 512 tok output 88 = 600 tok costDetails (cost) input · unit price output · unit price (usually pricier) total_cost ≈ $0.0041 cost = input_tok × in_price + output_tok × out_price unit price looked up by model (match-pattern); if usage absent, estimate via tokenizer, then cost it
Tokens drive cost (fields per usageDetails/costDetails in domain/observations.ts, price structure per worker/src/constants/default-model-prices.json; values illustrative): usage splits into input/output tokens, each converted at the model's own unit price into costDetails and summed into total_cost. Output is usually pricier than input, so “talking a lot” costs more than “reading a lot”. On a prompt-cache hit, input is charged at the lower input_cached_tokens tier (e.g. gpt-4o ≈ $1.25/1M, half of normal input).
worker/src/services/IngestionService/index.tsgetGenerationUsage
// ① model match: regex-match provided_model_name against the models table
const { model, pricingTiers } = provided_model_name
  ? await findModel({ projectId, model: provided_model_name })
  : { model: null, pricingTiers: [] };

// ② take/count tokens; ③ pick tier; ④ compute cost
const usage = await getUsageUnits(observationRecord, model);
const tier  = matchPricingTier(pricingTiers, usage.usage_details);
const cost  = IngestionService.calculateUsageCosts(tier.prices, obs, usage.usage_details);

return { ...usage, ...cost, internal_model_id: model?.id, /* tier */ };

provided beats computed: two forks

Tokens and cost each have a "provided or computed" fork, with identical rules: if you gave it, use yours; only if you didn't does the system compute. But "the system computes" has preconditions.

tokens / cost: provided first, computed as backup Did you report it?provided_usage / provided_cost ✅ given → use directly (provided)usage: take your token counts as-iscost: any one point given → all yoursnever mixed with system estimate 🤖 not given → system computesprecond: model matched AND level≠ERRORtokens: count via model.tokenizerIdcost: Σ price[usageType] × units yes no estimation is a backup, never overrides your reported real values — trusted source first
Estimation backstops, never overrides: provided always wins; computed happens only when all three hold — model matched + non-ERROR + you provided nothing. Source: getUsageUnits (index.ts:1145+, tokenization condition) and calculateUsageCosts ("any provided cost point disables computation").
dimensionprovided (you reported)computed (system, only if you didn't)
token usagetake provided_usage_details as-isif model matched + level≠ERROR, tokenize-count input/output via tokenizerId
costany cost point given → use provided_cost_details wholesale, compute nothing elseper usage type: price × units, summed to total_cost
whymore trustworthy source (LLM APIs often return exact token counts)a reasonable estimate absent a source; never mixed with provided

In real instrumentation, the two forks map to two very common integration styles:

🅰️ Official SDK: provided

You use the official OpenAI / Anthropic SDK; the response body carries usage (exact prompt/completion token counts). The SDK reports it as-is and Langfuse takes it whole — accurate, and no tokenization cost. The ideal case.

🅱️ Raw HTTP / homegrown: computed

You hit a model over raw HTTP, or via a gateway that doesn't return usage, so there's no token count. As long as the model name matches and it isn't ERROR, Langfuse tokenizes and prices it for you, so you still get usage and cost.

A concrete number: a gpt-4o call, the computed path counts input 1,000 tokens, output 500; the rate card prices this model at $2.5/1M input, $10/1M output, giving cost_details = { input: 0.0025, output: 0.005, total: 0.0075 }. If your SDK already reported usage = {input:1000, output:500}, step ② skips tokenization and uses those directly; if you also reported cost, step ④ skips even the multiplication and trusts your cost entirely. Compute as little as possible, trust you whenever possible.

Step ③ matchPricingTier is a newer fine-grained capability: a model can have several pricing tiers, each with a set of conditions — sum the usage matching some pattern and compare to a threshold (>, >=, <…, multiple conditions AND-ed). So "over 128k tokens → long-context higher price" or "cached tokens → discounted price" — usage-volume tiered pricing — can be expressed precisely. The matched tier provides per-usage-type unit prices for step ④. Match no tier, and it falls back to the model's base price — tiers just add flexibility on top of "one flat price".

Model matching: one regex to the price

The computed path begins by mapping your reported model-name string (e.g. gpt-4o-2024-08-06) to a price record. findModel does this on Postgres's models table, via a single regex match:

reported model name gpt-4o-2024-08-06 findModel(...) Postgres models · each row a regex match_pattern (?i)^(openai/)?(gpt-4o-2024-08-06)$ ✓match (?i)^(openai/)?(gpt-4o)$ (?i)^(claude-3-5-sonnet-.*)$ WHERE project_id=? OR NULL ORDER BY project_id, start_date DESC · LIMIT 1 matched row → price tiers → tokenizer_id (picks tokenizer) project model beats global, new price beats old: regex gives flex, project_id gives override, start_date gives time
how it matchesmodel ~ match_pattern
Each model row carries a regex match_pattern; Postgres's ~ operator tests your model name against it. The regex allows case-insensitivity, an optional provider prefix, and other flexible forms.
who winsproject model > global
WHERE project_id = ? OR project_id IS NULL, then ORDER BY project_id — your custom model prices override the built-in ones, handy for overriding default pricing or adding your own models.
which onelatest start_date
ORDER BY start_date DESC + LIMIT 1: prices change over time, so matching takes the one with the most recent effective date — historical data priced at its time, new data at the new price.

Beyond price, a matched model row has a key field tokenizer_id — it decides which tokenizer counts tokens when computing (different model families tokenize differently). Tokenization itself lives in worker/src/features/tokenisation, even running in a separate worker thread (tokenCountAsync) so large-text tokenization doesn't block the main flow; on failure it falls back to synchronous tokenCount. This too is the write-side origin of Lesson 8's split between provided_* and computed columns: provided goes into provided_usage_details, the computed into usage_details, never polluting each other.

Look at one real built-in rule (Langfuse ships ~150+ model prices): gpt-4o's match_pattern is (?i)^(openai/)?(gpt-4o)$ — case-insensitive, with or without the openai/ prefix, matching gpt-4o exactly. Dated versions (like gpt-4o-2024-05-13) have their own separate price rows, since pricing can differ by release date. This "regex + built-in rate card" lets Langfuse recognize mainstream models out of the box, and you can add your own rows in models to override or extend — the regex gives flexibility, project_id precedence gives override rights, start_date gives a time dimension. Together they make a living pricing system that evolves with the model market.

🎯 Design trade-off
Why so adamant that "provided beats computed" — rather than averaging the two, or taking the larger? Because it's a stance on data trustworthiness. The token count an LLM provider returns in its response is the authoritative basis for billing — it knows how it tokenized, far more accurately than Langfuse estimating from outside with a generic tokenizer. The system's job is to record faithfully, not to "correct" an authoritative source with its own estimate. So the rule is unambiguous: if provided, use it verbatim; even for cost, "give one point and all of it is yours", never mixing in estimates — because a half-real, half-estimated bill is more dangerous than a pure estimate (you'd mistake it for exact). Computed appears only in an information vacuum (you gave nothing), as a reasonable backstop. Trust first, estimate as backup, keep the two isolated — this principle pervades every corner of usage and cost.
🎯 Key points
  • getGenerationUsage in four steps: findModel (match model/price) → getUsageUnits (take/count tokens) → matchPricingTier (pick tier) → calculateUsageCosts (compute cost).
  • Provided beats computed: report usage/cost and it's used as-is; for cost, any one provided point means all yours, never mixed with estimates.
  • Computed has preconditions: model matched + level≠ERROR + you provided nothing, then tokenize-count input/output via tokenizerId.
  • findModel: Postgres ~ match_pattern regex; project models override global, latest start_date wins — history at old price, new data at new.
  • Tokenization in worker/src/features/tokenisation, optionally on a separate thread (tokenCountAsync); results land in Lesson 8's provided_* vs usage_details columns, never polluting each other.

🧪 Self-test - think about the design

1. Your SDK reported usage={input:1000, output:500} and cost={total: 0.01}. How does Langfuse handle the token counts and cost?
  1. averages your reported values with the system's estimate
  2. uses yours directly: tokens skip tokenization and use your 1000/500; since you gave at least one cost point, it uses your 0.01 wholesale and doesn't even consult the rate card — provided beats computed
  3. ignores your values and computes everything itself
  4. re-tokenizes to verify your counts, then recomputes cost from the rate card
Show answer & explanation click to expand
Answer: B. Provided beats computed runs through all of Langfuse. getUsageUnits uses provided_usage directly when present; calculateUsageCosts, seeing 'any cost point given', uses provided wholesale and computes nothing else. The system never mixes estimates with your real reported values, since a half-real, half-estimated bill is more dangerous.
2. You called a model over raw HTTP with no usage in the event. When will Langfuse tokenize-count for you?
  1. only when level=ERROR
  2. all three hold: the model name regex-matches a model row, the observation isn't ERROR, and you provided no usage — then it counts input/output tokens using that model's tokenizerId
  3. only on the paid plan
  4. always, unconditionally
Show answer & explanation click to expand
Answer: B. Computed is a precondition-gated backstop: a matched model (so it knows which tokenizer and price), non-ERROR (no point counting a failed generation), and no provided usage. Missing any one, no tokenization. It counts via model.tokenizerId, in worker/src/features/tokenisation, optionally on a separate thread to avoid blocking.
3. When findModel matches a model name to a price, if a project customized gpt-4o's price while a built-in gpt-4o also exists, which is used?
  1. the project's custom one: the SQL is project_id = ? OR project_id IS NULL, then ORDER BY project_id so project rows rank before global (NULL); among those it also takes the latest start_date
  2. adds the two prices together
  3. picks one at random
  4. the built-in; the customization is ignored
Show answer & explanation click to expand
Answer: A. findModel matches via Postgres regex ~ match_pattern, the WHERE takes both project and global (project_id IS NULL) models, ORDER BY project_id prioritizes custom, start_date DESC takes the latest effective price, LIMIT 1. So projects can override defaults or add own models; history is priced at its time, new data at the new price.
💭 Open questions (no single right answer - just think or try)
  • 'Trusted source first, estimation only as backup, the two strictly isolated' — this principle isn't only for tokens/cost. In your own systems, where do 'authoritative data from users/upstream' and 'local inference/estimates' coexist? How would you design their precedence and isolation to avoid the misjudgment of half-real, half-estimated data?