Langfuse 是开源的——你现在读的这套源码,从摄取到查询、从评估到自动化,全都摆在公开仓库里。但它又是一家公司,得靠卖东西活下去。这两件事怎么共存?答案是 open-core(开源核)商业模式:核心免费开源,少数企业级功能(审计日志、数据保留策略、多租户 SSO、admin API…)需要付费计划或企业许可证才能启用。妙的是,这些「付费功能」的代码也是开源的、就在这个仓库里——你能读、能学,但要在生产里用,得有相应的 plan(计划)。
这一课讲这套「门票系统」怎么用极少的代码实现:一张 entitlementAccess 总表把每个 plan 映射到「能用哪些功能、各有什么额度」,一个 hasEntitlement() 函数就是所有功能门口的那道闸。还会看到自托管版怎么用一把 langfuse_ee_ 开头的许可证密钥解锁企业功能。
hasEntitlement)。自己在家照菜谱做(自托管)也想解锁高级服务?那得买一张「企业授权证书」(langfuse_ee_ 许可证密钥)。
先厘清一个常见误解:open-core 不是「一半开源一半闭源」。Langfuse 这个仓库里所有功能的代码都是开放的——包括那些「企业级」功能。区别只在运行时能不能启用:每个组织挂着一个 plan,免费的默认是 "oss"(自托管开源版),付费的有云上五档 cloud:hobby / core / pro / team / enterprise,以及自托管两档 self-hosted:pro / enterprise。isCloudPlan / isSelfHostedPlan 这两个小助手按前缀区分它们。功能用不用得了,全看你的 plan 在不在那张允许名单里。
web/src/features/entitlements/*、packages/shared/src/server/ee/* 也开源)。Plan 见 plans.ts:21(type Plan = keyof typeof planLabels,含 cloud:hobby/core/pro/team/enterprise + self-hosted:pro/enterprise),默认 "oss";plans.ts:26-27 isCloudPlan/isSelfHostedPlan 按前缀判断。features/entitlements/constants/entitlements.ts 的 entitlementAccess: Record<Plan, …>,真实映射):基础权益(trace-deletion)人人都有;data-retention 从 Cloud Pro 起;rbac-project-roles/audit-logs/admin-api 这些治理类要 Cloud Team 或自托管企业版;self-host-ui-customization 是自托管企业版独有。运行时一句 hasEntitlement("audit-logs") 按当前 plan 查表放行。所以你能读到全部源码,但启用企业特性仍需对应 plan 或 license——这正是「open-core」。plan ?? "oss" 兜底到这里——默认落到免费,而非默认付费。ee/ 与 shared/.../ee/。你能审计、能学习、能给自己改——只是生产启用需要授权。整套「门票系统」的心脏,是一张 entitlementAccess: Record<Plan, {entitlements, entitlementLimits}> 总表,把每个 plan 映射到两样东西:① entitlements(二元开关:某功能有/没有,如 data-retention、audit-logs、admin-api);② entitlementLimits(数值额度:某资源能用多少,如 organization-member-count、data-access-days、monitor-count,值是数字表示有上限,或 false 表示无限)。比如 cloud:hobby 限 2 个成员、30 天数据、1 个标注队列;cloud:core 放宽到成员无限、90 天;cloud:pro 再加上 data-retention 功能。
所有功能门口就一道闸:hasEntitlement({ sessionUser, orgId, entitlement })。它的逻辑朴素得让人安心:① admin 直接放行;② 找到所属组织、取其 plan,没有就兜底成 "oss";③ 查这个 plan 的 entitlements 名单里含不含所求功能。就这三步。功能代码里不写「if plan==pro」散落各处,而是统一问这道闸——和第 49 课 RBAC「问 scope」如出一辙:把「谁能用什么」收敛到一张表 + 一个函数。
entitlements.ts:54 entitlementAccess: Record<Plan, {entitlements, entitlementLimits}>(entitlements:6 二元功能、entitlementLimits:38 数值额度 number|false)。hasEntitlement.ts:17::18 admin→true、:25 org.plan ?? "oss"、:40 entitlementAccess[plan].entitlements.includes(entitlement)。// 二元功能开关 + 数值额度(number=有上限 / false=无限) export const entitlements = ["audit-logs", "data-retention", "admin-api", "cloud-multi-tenant-sso", "scheduled-blob-exports", …] as const; export const entitlementAccess: Record<Plan, {…}> = { "cloud:hobby": { entitlements: […], entitlementLimits: { "organization-member-count": 2, "data-access-days": 30 } }, "cloud:core": { entitlements: […], entitlementLimits: { "organization-member-count": false/*无限*/, "data-access-days": 90 } }, "cloud:pro": { entitlements: [… "data-retention"], … }, // self-hosted:pro/enterprise, cloud:enterprise … }; // 所有功能门口的同一道闸 export const hasEntitlement = (p) => { if (p.sessionUser.admin) return true; // admin 直接放行 const plan = org?.plan ?? "oss"; // 默认兜底到免费版 return entitlementAccess[plan].entitlements.includes(p.entitlement); };
某功能有或没有:audit-logs、data-retention、admin-api、cloud-multi-tenant-sso…由 hasEntitlement 查「plan 名单含不含」,返回 true/false。
某资源能用多少:organization-member-count、data-access-days、monitor-count…值是 number(有上限)或 false(无限),由 hasEntitlementLimit 查。
自托管解锁企业层:LANGFUSE_EE_LICENSE_KEY 以 langfuse_ee_ 开头才算企业(langfuse_pro_ 不算);Cloud 任何区域天然有企业功能。
云上版本由 Langfuse 托管,plan 写在数据库里。但自己部署的版本怎么证明「我买了企业版」?靠一把许可证密钥。isEnterpriseLicenseAvailable() 的判断很干脆:① 是 Langfuse Cloud(任何区域)→ 直接有企业功能;② 自托管 → 看环境变量 LANGFUSE_EE_LICENSE_KEY 是否以 langfuse_ee_ 开头。注意一个细节:langfuse_pro_ 开头的 Pro 许可证不算企业级——前缀本身就编码了授权层级。这把钥匙插对了,自托管实例才解锁那些企业 entitlement。
| 问题 | open-core 的答案 |
|---|---|
| 企业功能的代码闭源吗? | 不,全在公开仓库(ee/、shared/.../ee/),能读能学能自改 |
| 那靠什么区分免费/付费? | 运行时的 plan + hasEntitlement 闸,而非「代码有没有」 |
| 没有 plan 的组织怎么办? | plan ?? "oss" 兜底到免费——默认最安全的(最少权益),不是默认付费 |
| 自托管怎么解锁企业版? | 设 LANGFUSE_EE_LICENSE_KEY=langfuse_ee_…(Pro 的 langfuse_pro_ 不算企业) |
| EE 代码放哪、谁能依赖它? | 独立包 @langfuse/ee;依赖方向 web → @langfuse/ee → @langfuse/shared |
把这张表连起来看,open-core 的精巧就清楚了:「能不能用」与「有没有代码」彻底解耦。代码 100% 公开(透明、可审计、社区可贡献),而商业边界靠一层轻薄的运行时门控(plan + entitlement + license)划出。这让 Langfuse 既是真开源、又能有可持续的商业模式——两者不矛盾。
hasEntitlement 闸 + 一张 entitlementAccess 总表,而不是在每个功能里各自写「if plan == pro」? 这和第 49 课 RBAC 用「角色→scope」是同一种智慧,原因也一样:收敛权威、便于演进与审计。商业 plan 会变——加一档套餐、调一次额度、把某功能从 pro 下放到 core,都是常事。如果「pro 才能用 X」这种判断散落在几十处功能代码里,每次调整都要全仓库搜改,极易漏、易错、还难复盘「到底哪个 plan 能用什么」。把它收进一张声明式总表后:调整商业策略=改这张表的一行,功能代码完全不动;想知道「pro 包含什么」=读表的一行。把易变的商业规则从稳定的功能逻辑里抽出来、集中声明,是让系统能跟上商业节奏的关键。"oss"(免费),而不是报错或默认给最高权限? 这是一个「fail-safe(失败安全)」默认值的选择,方向很重要。如果默认给最高权限,一旦某组织的 plan 字段因 bug、迁移、数据缺失而读不到,它就会意外解锁所有付费功能——商业上漏损、安全上越权。如果默认报错,又会让「绝大多数自托管开源用户」(他们本就该是 oss)寸步难行。兜底到 "oss" 恰好两全:开源用户开箱即用(这是最常见、最该顺滑的路径),而付费功能默认是关的、必须由明确的 plan 或 license 主动打开。安全的默认值,应该是「权限最小、影响最轻」的那一个——entitlement 默认关、登录默认拒、IP 解析失败默认拦(第 44 课),都是同一条原则的不同侧影。
plan,默认 "oss"(免费自托管),付费有 cloud:hobby/core/pro/team/enterprise + self-hosted:pro/enterprise;isCloudPlan/isSelfHostedPlan 按前缀分。entitlementAccess: Record<Plan,{entitlements, entitlementLimits}> 把每个 plan 映射到「有哪些二元功能 + 各资源额度(number/false无限)」;hasEntitlement(admin→true、plan ?? "oss"、includes)是所有功能共用的那道闸——收敛权威、便于演进,同第49课 RBAC 一脉。isEnterpriseLicenseAvailable——Cloud(任何区域)有企业功能;自托管看 LANGFUSE_EE_LICENSE_KEY 是否 langfuse_ee_ 开头(langfuse_pro_ 不算企业,前缀编码授权层级)。"oss"(最小权益、付费功能默认关),而非默认报错或默认全开——与「登录默认拒、IP 解析失败默认拦」同源的安全默认观。EE 代码在独立包 @langfuse/ee,依赖方向 web→ee→shared。
Langfuse is open source — the source you're reading, from ingestion to query, from evaluation to automation, is all laid out in a public repo. Yet it's also a company that must sell something to survive. How do these coexist? The answer is the open-core business model: the core is free and open, while a few enterprise features (audit logs, data-retention policy, multi-tenant SSO, admin API…) require a paid plan or enterprise license to enable. The clever part: the code for these "paid features" is also open, right here in this repo — you can read and learn it, but to use it in production you need the corresponding plan.
This lesson shows how that "ticket system" is implemented with very little code: one entitlementAccess master table mapping each plan to "which features, with what limits," and one hasEntitlement() function that is the single gate in front of every feature. You'll also see how a self-hosted version unlocks enterprise features with a license key starting with langfuse_ee_.
hasEntitlement). Cooking from the recipe at home (self-hosting) and want premium service too? Then buy an "enterprise license certificate" (a langfuse_ee_ license key).
First clear up a common misconception: open-core is not "half open-source, half closed". In this Langfuse repo all features' code is open — including the "enterprise" ones. The difference is only whether they can be enabled at runtime: each org carries a plan, free defaults to "oss" (self-hosted open-source), paid has five cloud tiers cloud:hobby / core / pro / team / enterprise plus two self-hosted self-hosted:pro / enterprise. The helpers isCloudPlan / isSelfHostedPlan distinguish them by prefix. Whether a feature works hinges entirely on whether your plan is on its allow list.
web/src/features/entitlements/*, packages/shared/src/server/ee/* are open too). Plans at plans.ts:21 (type Plan = keyof typeof planLabels, including cloud:hobby/core/pro/team/enterprise + self-hosted:pro/enterprise), defaulting to "oss"; plans.ts:26-27 isCloudPlan/isSelfHostedPlan judge by prefix.entitlementAccess: Record<Plan, …> in features/entitlements/constants/entitlements.ts, real mapping): base entitlements (trace-deletion) are universal; data-retention starts at Cloud Pro; governance ones like rbac-project-roles/audit-logs/admin-api need Cloud Team or self-host Enterprise; self-host-ui-customization is self-host-Enterprise only. At runtime a single hasEntitlement("audit-logs") checks this table by the current plan. So you can read all the source, but enabling enterprise features still needs the matching plan or license — that's "open-core".plan ?? "oss" falls back here — defaulting to free, not to paid.ee/ and shared/.../ee/. You can audit, learn, and modify it for yourself — only production enablement needs a license.The heart of the whole "ticket system" is one entitlementAccess: Record<Plan, {entitlements, entitlementLimits}> master table, mapping each plan to two things: ① entitlements (binary switches: a feature on/off, like data-retention, audit-logs, admin-api); ② entitlementLimits (numeric quotas: how much of a resource you may use, like organization-member-count, data-access-days, monitor-count — the value is a number for a cap, or false for unlimited). For example cloud:hobby caps at 2 members, 30 days of data, 1 annotation queue; cloud:core loosens to unlimited members, 90 days; cloud:pro additionally adds the data-retention feature.
There's just one gate in front of every feature: hasEntitlement({ sessionUser, orgId, entitlement }). Its logic is reassuringly plain: ① admin passes outright; ② find the owning org, take its plan, falling back to "oss" if none; ③ check whether this plan's entitlements list includes the requested feature. Just those three steps. Feature code doesn't scatter "if plan==pro" everywhere but asks this one gate — exactly like Lesson 49's RBAC "ask the scope": converge "who can use what" into one table + one function.
entitlements.ts:54 entitlementAccess: Record<Plan, {entitlements, entitlementLimits}> (entitlements:6 binary features, entitlementLimits:38 numeric quotas number|false). hasEntitlement.ts:17: :18 admin→true, :25 org.plan ?? "oss", :40 entitlementAccess[plan].entitlements.includes(entitlement).// binary feature switches + numeric quotas (number=capped / false=unlimited) export const entitlements = ["audit-logs", "data-retention", "admin-api", "cloud-multi-tenant-sso", "scheduled-blob-exports", …] as const; export const entitlementAccess: Record<Plan, {…}> = { "cloud:hobby": { entitlements: […], entitlementLimits: { "organization-member-count": 2, "data-access-days": 30 } }, "cloud:core": { entitlements: […], entitlementLimits: { "organization-member-count": false/*unlimited*/, "data-access-days": 90 } }, "cloud:pro": { entitlements: [… "data-retention"], … }, // self-hosted:pro/enterprise, cloud:enterprise … }; // the same one gate in front of every feature export const hasEntitlement = (p) => { if (p.sessionUser.admin) return true; // admin passes outright const plan = org?.plan ?? "oss"; // default fall back to free return entitlementAccess[plan].entitlements.includes(p.entitlement); };
A feature on or off: audit-logs, data-retention, admin-api, cloud-multi-tenant-sso… hasEntitlement checks "is it in the plan's list", returning true/false.
How much of a resource: organization-member-count, data-access-days, monitor-count… the value is a number (capped) or false (unlimited), checked by hasEntitlementLimit.
Self-hosting unlocks the enterprise tier: LANGFUSE_EE_LICENSE_KEY must start with langfuse_ee_ to count as enterprise (langfuse_pro_ doesn't); Cloud in any region has enterprise features natively.
The cloud version is hosted by Langfuse, with the plan written in the database. But how does a self-deployed version prove "I bought enterprise"? Via a license key. isEnterpriseLicenseAvailable()'s decision is crisp: ① is it Langfuse Cloud (any region) → enterprise features directly; ② self-hosted → check whether the env var LANGFUSE_EE_LICENSE_KEY starts with langfuse_ee_. Note one detail: a Pro license starting with langfuse_pro_ does not count as enterprise — the prefix itself encodes the authorization tier. With this key plugged in correctly, a self-hosted instance unlocks those enterprise entitlements.
| Question | Open-core's answer |
|---|---|
| Is enterprise-feature code closed? | No, all in the public repo (ee/, shared/.../ee/), readable, learnable, self-modifiable |
| Then what distinguishes free/paid? | the runtime plan + hasEntitlement gate, not "whether the code exists" |
| What about orgs with no plan? | plan ?? "oss" falls back to free — defaults to the safest (least entitlement), not to paid |
| How does self-hosting unlock enterprise? | set LANGFUSE_EE_LICENSE_KEY=langfuse_ee_… (Pro's langfuse_pro_ isn't enterprise) |
| Where's EE code, who can depend on it? | a separate package @langfuse/ee; dependency direction web → @langfuse/ee → @langfuse/shared |
Connect the rows and open-core's elegance is clear: "can use" and "has code" are fully decoupled. The code is 100% public (transparent, auditable, community-contributable), while the commercial boundary is drawn by a thin runtime gate (plan + entitlement + license). This lets Langfuse be truly open source AND have a sustainable business model — the two aren't in conflict.
hasEntitlement gate + one entitlementAccess master table, rather than "if plan == pro" in each feature? Same wisdom as Lesson 49's RBAC "role→scope," for the same reason: converge authority, ease evolution and auditing. Commercial plans change — adding a tier, adjusting a quota, demoting a feature from pro to core are all routine. If "pro can use X" judgments are scattered across dozens of feature sites, every change means a repo-wide search-and-edit, easy to miss, error-prone, and hard to review "which plan can use what." Folding it into one declarative master table: change commercial strategy = edit one row of this table, feature code untouched; want to know "what does pro include" = read one row. Extracting volatile commercial rules from stable feature logic and declaring them centrally is the key to keeping the system in step with the business."oss" (free), rather than erroring or defaulting to the highest privileges? This is a "fail-safe" default choice, and the direction matters. Default to the highest privileges and the moment an org's plan field can't be read (a bug, a migration, missing data), it accidentally unlocks all paid features — a commercial leak and a privilege overreach. Default to erroring and you stall "the vast majority of self-hosted open-source users" (who should be oss anyway). Falling back to "oss" gets both right: open-source users work out of the box (the most common, smoothest path), while paid features are off by default, opened only by an explicit plan or license. A safe default should be the one with "least privilege, lightest impact" — entitlement defaults off, login defaults deny, IP-resolution failure defaults block (Lesson 44) — all facets of the same principle.
plan, defaulting to "oss" (free self-host), paid has cloud:hobby/core/pro/team/enterprise + self-hosted:pro/enterprise; isCloudPlan/isSelfHostedPlan split by prefix.entitlementAccess: Record<Plan,{entitlements, entitlementLimits}> maps each plan to "which binary features + each resource's quota (number/false unlimited)"; hasEntitlement (admin→true, plan ?? "oss", includes) is the shared gate in front of every feature — converging authority, easing evolution, of one lineage with Lesson 49's RBAC.isEnterpriseLicenseAvailable — Cloud (any region) has enterprise features; self-hosting checks whether LANGFUSE_EE_LICENSE_KEY starts with langfuse_ee_ (langfuse_pro_ isn't enterprise; the prefix encodes the tier)."oss" (least entitlement, paid features off by default), not defaulting to error or all-on — the same safe-default view as "login defaults deny, IP-resolution failure defaults block." EE code lives in a separate package @langfuse/ee, dependency direction web→ee→shared.