Monetizing AI APIs with Billing Meters in Moesif
You’ve built an incredible AI API and are ready to release this functionality to your users. The issue is that you’re not sure exactly how to monetize it. Generally, monetizing APIs is challenging at scale, but monetizing AI APIs can be even more difficult. Some AI APIs may be charged on a “per API call” basis, but many AI APIs require charging users for input and output tokens used within an API call. Others may charge per unique user or API key.
Monetizing APIs based on these metrics becomes increasingly challenging as you move from the most straightforward metric, charging per API call, to the more complex task of aggregating token usage. In this blog post, I will show you how to implement a Billing Meter in Moesif to expedite monetizing your AI APIs. We will first review what a Billing Meter is and then walk through a step-by-step guide on implementing monetization for AI APIs. Let’s get started!
What is a Billing Meter?
A Billing Meter is a functionality offered within Moesif that allows you to aggregate API usage on a per-subscription basis and send this usage data to billing providers to charge users for the usage. Within a billing meter, the three main areas are:
Billing Provider and Subscription Item to Associate Usage To
When you meter your usage, you want it reported to your billing provider, such as Stripe, Zuora, Chargebee, etc. Each billing provider usually has plans and prices that you want this usage counted towards. This part of the Billing Meter allows you to precisely report the usage to a particular price so a user can be charged accordingly.
Filter Criteria
The filter denotes what you want to monetize. For example, you may have a filter that says, “I want to monetize every API call to the /generate route that returns a 200 OK response”. A filter can be high-level, like the example just discussed, or could go down to a deep level, such as only monetizing API calls to a specific route where a field within the body contains a particular string. Anything that does not match the filter will not be eligible to be monetized by that particular meter.
Billing Metric
The metric is how you will monetize the API calls that match your filter. For example, you may monetize your APIs by metering event count (each API call is charged for) or more commonly for AI APIs, have your metric set to input or output tokens for that specific meter. This will be the scenario we will demonstrate below in the practical portion of this blog.
Once the Billing Meter is live, these different components work together to seamlessly monetize your AI APIs. In the next section, let’s walk through exactly how to set up a billing meter to monetize your AI APIs quickly.
Monetizing Your AI API With Moesif
Step 1: Create a Moesif Account and Integrate Your AI APIs
Once you’ve signed up for a Moesif account or are logged in as an existing user, our first step is integrating our AI APIs to stream usage data into the Moesif platform. For this, you can use one of our SDKs or plugins for many of the most popular languages, frameworks, and gateways to create AI APIs.
Step 2: Integrate With Your Billing Provider
After creating an account and integrating your APIs, the next step will be configuring your selected billing provider so that usage can be synced. There are a few out-of-the-box options to choose from:
You can follow the links in the entries to walk you through how to set up each of these platforms with Moesif. If you don’t currently use one of these providers, you can also set up custom integrations using either the Moesif API or Custom Webhook functionality.
Step 3: Create Your Billing Meter
Next, you will go to the Billing Meters screen in Moesif by selecting Billing Meters from the left-side menu. On the next screen, click Add Billing Meter in the top-right to create a new meter.
On the Create New Billing Meter screen, add the name of your meter and select the billing provider, plan, and price that you’d like the usage to be synced over to. Here’s an example of a plan and price in Stripe being added to a meter.
From here, let’s dial in the filter. For this demonstration, in the Filter section, we will make it so that we monetize any API call to the /v1/text/generate
route that receives a 200 OK
response. Additionally, I’ve also added another criterion where the response.body.generated_text.usage.total_tokens
body field is greater than 15. Any API calls that use less than 15 tokens will not be included in the monetized APIs. This condition type is optional but possible since Moesif allows you to use values in the request and response header and body fields. Here’s what that example filter would look like.
For this particular meter, I want to aggregate the number of output tokens (known as completion_tokens
in the response payload for this particular API) and charge for this usage. To do this, we will need to use Moesif’s Custom Metric functionality and select the Select Field… option from the Metrics dropdown.
With the Custom Metric type selected, we will click on the field selector dropdown and choose the response.body.generated_text.usage.completion_tokens
field and set the aggregation type to “sum”. This will add all the output tokens used in API calls that match our filter criteria. Here’s what that custom field would look like in Moesif.
Below the meter configuration, you’ll see a Preview of Historical Usage table showing how the filter and selected metric would have picked up on past events that have streamed into Moesif. This is a great way to do a quick logic check to ensure the meter is doing what you intended. Here’s an example of what that output would look like.
With our Billing Meter configuration complete, we can click the Create button in the top-right to create and activate the meter. At this point, the meter is active, and will begin monetizing our APIs based on what we’ve dialed in. Optionally, but suggested, is to run through a test of the meter, ensuring that each component is running as expected. You can do this by clicking the Test Meter button on the meter you just created.
Step 4: Drive Revenue
Our last step is to start getting some API traffic so that it can be monetized and drive revenue! As API traffic flows in, if it meets your meter’s criteria, the usage will be logged and synced over to your billing provider based on your selected metric. This is all required to begin monetizing your AI APIs with Moesif.
Conclusion
Monetizing AI APIs can be complex, but with Moesif’s Billing Meter feature, you can seamlessly track usage and charge users based on specific metrics like token consumption. By integrating Moesif with your billing provider and setting up customized filters and metrics, you can ensure accurate and transparent monetization for your AI services. Ready to unlock the revenue potential of your AI APIs? Sign up for Moesif today and start building Billing Meters to monetize your AI APIs in minutes!