理解 Downcity City
usage 与商业模式
如何把用量记录、套餐、余额和扣费放到 hook 中。
Downcity 不替你决定商业模式。它提供 hook,让你把自己的套餐、余额、限额、日志和扣费系统接进 AI 调用生命周期。
before hook
before 适合做调用前判断:
- 用户是否有权限
- 套餐是否允许该模型
- 今日额度是否足够
- 是否触发风控或频控
after hook
after 适合做调用后记录:
- 记录 provider usage
- 写调用日志
- 扣除余额
- 同步账单
- 按产品维度分析成本
const ai = new AIService();
ai.hook
.before(async (ctx) => {
await quotaService.check(ctx.user?.user_id, ctx.variant?.id);
})
.after(async (ctx) => {
await usageService.record({
user_id: ctx.user?.user_id,
town_id: ctx.town?.town_id,
model: ctx.variant?.id,
output: ctx.output,
});
});
base.use(ai);这样多个 AI 产品可以共享同一套商业逻辑,而不是每个项目各写一遍。