Monetize LLM traffic in Konnect

TL;DR

To meter LLM traffic in Konnect, you can use the Metering & Billing to track and invoice usage based on defined products, plans, and features. This guide walks you through setting up a Consumer, creating a meter for LLM tokens, defining a feature, creating a Plan with Rate Cards, and starting a subscription for billing.

Prerequisites

This is a Konnect tutorial and requires a Konnect personal access token.

  1. Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.

  2. Export your token to an environment variable:

     export KONNECT_TOKEN='YOUR_KONNECT_PAT'
    
  3. Run the quickstart script to automatically provision a Control Plane and Data Plane, and configure your environment:

     curl -Ls https://get.konghq.com/quickstart | bash -s -- -k $KONNECT_TOKEN --deck-output
    

    This sets up a Konnect Control Plane named quickstart, provisions a local Data Plane, and prints out the following environment variable exports:

     export DECK_KONNECT_TOKEN=$KONNECT_TOKEN
     export DECK_KONNECT_CONTROL_PLANE_NAME=quickstart
     export KONNECT_CONTROL_PLANE_URL=https://us.api.konghq.com
     export KONNECT_PROXY_URL='http://localhost:8000'
    

    Copy and paste these into your terminal to configure your session.

decK is a CLI tool for managing Kong Gateway declaratively with state files. To complete this tutorial, install decK version 1.43 or later.

This guide uses deck gateway apply, which directly applies entity configuration to your Gateway instance. We recommend upgrading your decK installation to take advantage of this tool.

You can check your current decK version with deck version.

For this tutorial, you’ll need Kong Gateway entities, like Gateway Services and Routes, pre-configured. These entities are essential for Kong Gateway to function but installing them isn’t the focus of this guide. Follow these steps to pre-configure them:

  1. Run the following command:

    echo '
    _format_version: "3.0"
    services:
      - name: example-service
        url: http://httpbin.konghq.com/anything
    routes:
      - name: example-route
        paths:
        - "/anything"
        service:
          name: example-service
    ' | deck gateway apply -
    

To learn more about entities, you can read our entities documentation.

This tutorial uses OpenAI:

  1. Create an OpenAI account.
  2. Get an API key.
  3. Create a decK variable with the API key:

    export DECK_OPENAI_API_KEY='YOUR OPENAI API KEY'
    

This getting-started guide shows how to meter LLM traffic—such as token consumption or model-specific usage—from Kong Gateway and convert that raw LLM activity into billable usage with Metering & Billing in Konnect.

Create a Consumer

Before you configure Metering & Billing, you can set up a Consumer, Kong Air. Consumers let you identify the client that’s interacting with Kong Gateway. Later in this guide, you’ll be mapping this Consumer to a customer in Metering & Billing and assigning them to a Premium plan. Doing this allows you map existing Consumers that are already consuming your APIs to customers to make them billable.

echo '
_format_version: "3.0"
consumers:
  - username: kong-air
    keyauth_credentials:
    - key: hello_world
' | deck gateway apply -

To connect LLM usage to the Consumer, you’ll need to configure an authentication plugin. In this tutorial, we’ll use Key Authentication. This will require the Consumer to use an API key to access any Kong Gateway Services.

Configure the Key Auth plugin on the Service:

echo '
_format_version: "3.0"
plugins:
  - name: key-auth
    service: example-service
    config:
      key_names:
      - apikey
' | deck gateway apply -

Configure the AI Proxy plugin

To set up AI Proxy with OpenAI, specify the model and set the appropriate authentication header. To collect meters, you must also enable log_payloads and log_statistics.

In this example, we’ll use the gpt-4o model:

echo '
_format_version: "3.0"
plugins:
  - name: ai-proxy
    config:
      route_type: llm/v1/chat
      auth:
        header_name: Authorization
        header_value: Bearer ${{ env "DECK_OPENAI_API_KEY" }}
      model:
        provider: openai
        name: gpt-4o
      logging:
        log_payloads: true
        log_statistics: true
' | deck gateway apply -

Enable Metering

In Metering & Billing, meters track and record the consumption of a resource or service over time.

  1. In the Konnect sidebar, click Metering & Billing.
  2. For AI Gateway Tokens, click Enable Related API Gateways.
  3. Select the quickstart control plane.
  4. Click Enable 1 Gateway.

You will see quickstart in the list of available meters.

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 LLM token usage, you need to label that as something you want to price or govern.

In this guide, you’ll create a feature for the example-service you created in the prerequisites.

  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 ai-token.
  5. From the Meter dropdown menu, select “AI Gateway Tokens”.
  6. Click Add group by filter. The group by filter ensures you only bill for LLM tokens from a specific provider.
  7. From the Group by dropdown menu, select “Provider”.
  8. From the Operator dropdown menu, select “Equals”.
  9. In the Value dropdown menu, enter openai.
  10. Click Add group by filter.
  11. From the Group by dropdown menu, select “type”.
  12. From the Operator dropdown menu, select “Equals”.
  13. In the Value dropdown menu, enter request.
  14. 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.

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

In this section, you’ll create a Premium plan that charges customers based on the AI token usage at a rate of $0.00002 per use.

  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 Token.
  6. In the Billing cadence dropdown menu, select “1 month”.
  7. Click Save.
  8. Click Add Rate Card.
  9. From the Feature dropdown menu, select “ai-token”.
  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 the cost changes in the customer invoice. Be sure to 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 who pay for the consumption. In many cases, it’s equal to your Consumer. Here you are going to create a customer and map our Consumer to it.

  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 Kong Air.
  5. In the Include usage from dropdown, select “kong-air”.
  6. Click Save.
  7. Click the Subscriptions tab.
  8. Click Create a Subscription.
  9. From the Subscribed Plan dropdown, select “Token”.
  10. Click Next Step.
  11. Click Start Subscription.

Validate

You can run the following command to test the that the Kong Air Consumer is invoiced correctly:

curl -X POST "$KONNECT_PROXY_URL/anything" \
     --no-progress-meter --fail-with-body  \
     -H "Accept: application/json"\
     -H "Content-Type: application/json"\
     -H "apikey: hello_world" \
     --json '{
       "messages": [
         {
           "role": "system",
           "content": "You are a mathematician"
         },
         {
           "role": "user",
           "content": "What is 1+1?"
         }
       ]
     }'

This will generate AI LLM token usage that will be captured by Metering & Billing.

  1. In the Konnect sidebar, click Metering & Billing.
  2. In the Metering & Billing sidebar, click Billing.
  3. Click the Invoices tab.
  4. Click Kong Air.
  5. Click the Invoicing tab.
  6. Click Preview Invoice.

You’ll see in Lines that ai-token is listed and was used once. In this guide, you’re using the sandbox for invoices. To deploy your subscription in production, configure a payments integration in Metering & Billing > Settings.

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.

Something wrong?

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!