Get started with Metering & Billing generic meters

TL;DR

To meter AI agent runs in Konnect, create a generic meter with the Count agent runs template and COUNT aggregation to count agent run events per billing period. Then define a feature and plan to invoice customers based on their usage. Create a customer that includes usage from a subject and assign the customer to your plan. Finally, send events with the subject associated with the customer to generate an invoice.

Prerequisites

If you don’t have a Konnect account, you can get started quickly with our onboarding wizard.

  1. The following Konnect items are required to complete this tutorial:
    • Personal access token (PAT): Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.
  2. Set the personal access token as an environment variable:

    export KONNECT_TOKEN='YOUR KONNECT TOKEN'
    

You need the Metering & Billing Admin role in Konnect to configure Metering & Billing.

Generic metering is a flexible way to meter events from a variety of sources. This guide shows you how to use generic metering in Metering & Billing by demonstrating how to track and invoice customers based on the number of AI agent runs per month.

Pay-per-run billing is a common pricing model for AI products where customers are charged for each time an agent executes. By using the COUNT aggregation grouped by agent_name, you can track total runs and break down usage by agent type.

In this guide, you’ll:

  • Create a generic meter that counts agent runs
  • Create a feature to make that usage billable
  • Create a usage-based plan
  • Start a subscription for a customer
  • Send usage events and validate the invoice

Create a meter

In Metering & Billing, meters track and record the consumption of a resource or service over time. For billing per agent run, you’ll use the built-in Count agent runs template, which pre-configures a COUNT meter that increments once per agent_run event and groups results by agent_name.

  1. In the Konnect sidebar, click Metering & Billing.
  2. Click New meter.
  3. Click the Templates dropdown menu.
  4. Select Count agent runs.
  5. Click Save.

The template creates a meter with the following configuration:

Field

Value

Key agent_runs_total
Event type filter agent_run
Aggregation COUNT
Labels to group by agent_name

Create a feature

Meters collect raw usage data, but features make that data billable. Without a feature, usage is tracked but not invoiced. Now that you’re metering agent runs, you need to associate that meter with a feature.

  1. In the Konnect sidebar, click Metering & Billing.
  2. In the Metering & Billing sidebar, click Product Catalog.
  3. Click Create Feature.
  4. In the Name field, enter Agent Runs.
  5. From the Meter dropdown menu, select “Count Agent Runs”.
  6. Click Save.

Create a plan and rate card

Plans are the core building blocks of your product catalog. They are a collection of rate cards that define the price and access of a feature. Plans can be assigned to customers by starting a subscription.

A rate card describes the price and usage limits or access control for a feature. Rate cards are made up of the associated feature, price, and optional entitlements.

In this section, you’ll create an Premium Plan plan that charges customers $1 per agent run per month:

  1. In the Konnect sidebar, click Metering & Billing.
  2. In the Metering & Billing sidebar, click Product Catalog.
  3. Click the Plans tab.
  4. Click Create Plan.
  5. In the Name field, enter Premium Plan.
  6. From the Billing cadence dropdown menu, select “1 month”.
  7. Click Save.
  8. Click Add Rate Card.
  9. From the Feature dropdown menu, select “Agent Runs”.
  10. Click Next Step.
  11. From the Pricing model dropdown menu, select “Usage based”.
  12. In the Price per unit field, enter 1.

    We’re using $1 here to make it easy to see invoice amount changes in the customer invoice. Change this price in a production instance to match your own pricing model.

  13. Click Next Step.
  14. Select Boolean.
  15. Click Save Rate Card.
  16. Click Publish Plan.
  17. Click Publish.

Start a subscription

Customers are the entities that pay for consumption. Here you’ll create a customer and subscribe them to the Premium Plan plan.

  1. In the Konnect sidebar, click Metering & Billing.
  2. In the Metering & Billing sidebar, click Billing.
  3. Click Create Customer.
  4. In the Name field, enter Acme Inc.
  5. In the Key field, enter acme-inc.

    This value links incoming usage events to this customer. Events with "subject": "acme-inc" will be attributed to Acme Inc.

  6. Click Save.
  7. Click the Subscription tab.
  8. Click Create a Subscription.
  9. From the Subscribed Plan dropdown, select Premium Plan.
  10. Click Next Step.
  11. Click Start Subscription.

Validate

Send usage events to Metering & Billing using the CloudEvents format. Each event represents one agent run. The meter counts every event, so three events equal three runs.

Important: When you send events, they must have a unique id. Metering & Billing deduplicates events with the same id.

  1. Export the current time:
    export EVENT_TIME=$(date -u +%Y-%m-%dT%H:%M:%SZ)
    

    Metering & Billing only invoices and meters events that are sent after the subscription is created.

  2. Send a run event for the summarizer agent:

    curl -X POST "https://us.api.konghq.com/v3/openmeter/events" \
         --no-progress-meter --fail-with-body  \
         -H "Authorization: Bearer $KONNECT_TOKEN"\
         -H "Content-Type: application/cloudevents+json" \
         --json '{
           "specversion": "1.0",
           "type": "agent_run",
           "id": "8655CDD7-0775-4AEA-AF8C-89C47EBC8828",
           "source": "acme-platform",
           "time": "'$EVENT_TIME'",
           "datacontenttype": "application/json",
           "subject": "acme-inc",
           "data": {
             "agent_name": "summarizer"
           }
         }'
    
  3. Send a second run event for the summarizer agent:

    curl -X POST "https://us.api.konghq.com/v3/openmeter/events" \
         --no-progress-meter --fail-with-body  \
         -H "Authorization: Bearer $KONNECT_TOKEN"\
         -H "Content-Type: application/cloudevents+json" \
         --json '{
           "specversion": "1.0",
           "type": "agent_run",
           "id": "3EC7EFD0-5B78-47A4-AE9E-15965675CF95",
           "source": "acme-platform",
           "time": "'$EVENT_TIME'",
           "datacontenttype": "application/json",
           "subject": "acme-inc",
           "data": {
             "agent_name": "summarizer"
           }
         }'
    
  4. Send a run event for the translator agent:

    curl -X POST "https://us.api.konghq.com/v3/openmeter/events" \
         --no-progress-meter --fail-with-body  \
         -H "Authorization: Bearer $KONNECT_TOKEN"\
         -H "Content-Type: application/cloudevents+json" \
         --json '{
           "specversion": "1.0",
           "type": "agent_run",
           "id": "48FC220C-C124-4B73-B378-7DC5CF88DD1A",
           "source": "acme-platform",
           "time": "'$EVENT_TIME'",
           "datacontenttype": "application/json",
           "subject": "acme-inc",
           "data": {
             "agent_name": "translator"
           }
         }'
    

Now check the invoice:

  1. In the Konnect sidebar, click Metering & Billing.
  2. In the Metering & Billing sidebar, click Billing.
  3. Click Acme Inc.
  4. Click the Invoicing tab.
  5. Click Preview Invoice.

You’ll see agent-runs listed in Lines with a quantity of 3, reflecting three agent runs (two for summarizer and one for translator).

In this guide, you’re using the sandbox for invoices. To deploy your subscription in production, configure a payments integration in Metering & Billing > Settings, like Stripe.

Cleanup

If you created a new control plane and want to conserve your free trial credits or avoid unnecessary charges, delete the new control plane used in this tutorial.

FAQs

One reason why you might not see events in a customer’s invoice is if the event was sent before the subscription was created. Metering & Billing only invoices and meters events that are sent after the subscription is created.

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!