Skip to content

API Usage & Metering

If you want to productize your API, you might want to keep track of how much any given user uses it.

The code base includes the concept of Credits.
You can quickly configure endpoints with Credits(20) to make this endpoint statically cost 20 credits.
Dynamic usage is also possible, you can just use CaptureCredits(18, "Storing a 2gb file").

Credits only get persisted after SUCCESSFULL endpoint execution.
That means no exceptions and no 4xx / 5xx responses.

This is implemented in the ConsumeCredits PostProcessor.

A ledger is being kept per user in the CreditUsages collection.
It tells you who used how many credits when and for what reason.

request-log

Final Thoughts

There is no mechanism build in to increase credits or decrease credits or stop working when credits are gone.
It purely keeps track of what happened.
Who got charged how many credits when and for what reason.

There are just too many different systems and billing schemes people can come up with.

  • Buy credits upfront then decrease?
  • Stop working at 0 or just send an email and allow negative?
  • Fully pay as you go with credits increasing and manually write invoices at the end of the month?
  • Count up and charge the actual credit card every day like google or microsoft?
  • Does a user get charged or his organization?

You might also want to change the fact that users only get charged for successful endpoints.

Maybe you hit OpenAI’s endpoint and something happened after.
Maybe you want to charge your users for faulty requests.
Maybe you want to track them, mark them as failed and make a billing decision later.