VisibleBase
Understand VisibleBase

usage 与商业模式

如何把用量记录、套餐、余额和扣费放到 hook 中。

VisibleBase 不替你决定商业模式。它提供 hook,让你把自己的套餐、余额、限额、日志和扣费系统接进 AI 调用生命周期。

before hook

before 适合做调用前判断:

  • 用户是否有权限
  • 套餐是否允许该模型
  • 今日额度是否足够
  • 是否触发风控或频控

after hook

after 适合做调用后记录:

  • 记录 provider usage
  • 写调用日志
  • 扣除余额
  • 同步账单
  • 按产品维度分析成本
base.text()
  .before(async (ctx) => {
    await quotaService.check(ctx.user.user_id, ctx.model.model_id);
  })
  .after(async (ctx) => {
    await usageService.record({
      user_id: ctx.user.user_id,
      product_id: ctx.product.product_id,
      model: ctx.model.model_id,
      output: ctx.output,
    });
  });

这样多个 AI 产品可以共享同一套商业逻辑,而不是每个项目各写一遍。

On this page