Usage Service

Usage and Credits Billing

AIService produces technical usage and reliably writes Credits charges.

A model or channel bill creates a Credits bill from the final output. AIService persists AI Usage with one usage_id and calls CreditsService with ai:${usage_id} as the idempotency key.

const credits_service = new CreditsService();
const ai_service = new AIService({ credits: credits_service });

ai_service.use({
  id: "example-model",
  channel_id: "example-provider",
  name: "Example Model",
  bill: ({ output }) => ({
    credits: calculate_credits(output),
    note: "AI model usage",
  }),
  runtime: provider_runtime,
});

Insufficient balance rejects settlement. Database or Credits infrastructure errors become retryable. Retries neither duplicate AI Usage nor double-charge Credits. Usage queries include only applied Credits charges, so accounting trends always follow the Credits ledger.