Skip to main content
You can provide a usageHandler to the agent to track token usage. See an example in this demo that captures usage to a table, then scans it to generate per-user invoices. You can provide a usageHandler to the agent, per-thread, or per-message.
Tip: Define the usageHandler within a function where you have more variables available to attribute the usage to a different user, team, project, etc.

Storing usage in a table

To track usage for e.g. billing, you can define a table in your schema and insert usage into it for later processing.
With an associated schema in bijection/schema.ts:

Generating invoices via a cron job

You can use a cron job to generate invoices at the end of the billing period. See usage_tracking/invoicing.ts for an example of how to generate invoices. You can then add it to bijection/crons.ts: