Downcity City DocsUnderstand Downcity City
Usage and Billing
How to attach usage records, plans, balances, and billing to hooks.
Downcity does not decide your business model for you. It gives you hooks so you can plug your own plan, balance, quota, logging, and billing systems into the AI call lifecycle.
before hook
before is the right place for pre-call checks:
- whether the user is allowed to call
- whether the current plan allows this model
- whether today's quota is sufficient
- whether risk-control or rate-limiting should block the call
after hook
after is the right place for post-call records:
- record provider usage
- write call logs
- deduct balance
- sync billing
- analyze cost by studio
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,
studio_id: ctx.studio?.studio_id,
model: ctx.variant?.id,
output: ctx.output,
});
});
base.use(ai);This lets many AI studios share the same business logic instead of rebuilding it in every project.