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.
Monetize LLM traffic in Konnect
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
Kong Konnect
This is a Konnect tutorial and requires a Konnect personal access token.
-
Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.
-
Export your token to an environment variable:
export KONNECT_TOKEN='YOUR_KONNECT_PAT'Copied! -
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-outputCopied!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'Copied!Copy and paste these into your terminal to configure your session.
decK v1.43+
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.
Required entities
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:
-
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 -Copied!
To learn more about entities, you can read our entities documentation.
OpenAI
This tutorial uses OpenAI:
- Create an OpenAI account.
- Get an API key.
-
Create a decK variable with the API key:
export DECK_OPENAI_API_KEY='YOUR OPENAI API KEY'Copied!
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.
- In the Konnect sidebar, click Metering & Billing.
- For AI Gateway Tokens, click Enable Related API Gateways.
- Select the
quickstartcontrol plane. - 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.
- In the Konnect sidebar, click Metering & Billing.
- In the Metering & Billing sidebar, click Product Catalog.
- Click Create Feature.
- In the Name field, enter
ai-token. - From the Meter dropdown menu, select “AI Gateway Tokens”.
- Click Add group by filter. The group by filter ensures you only bill for LLM tokens from a specific provider.
- From the Group by dropdown menu, select “Provider”.
- From the Operator dropdown menu, select “Equals”.
- In the Value dropdown menu, enter
openai. - Click Add group by filter.
- From the Group by dropdown menu, select “type”.
- From the Operator dropdown menu, select “Equals”.
- In the Value dropdown menu, enter
request. - 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.
- In the Konnect sidebar, click Metering & Billing.
- In the Metering & Billing sidebar, click Product Catalog.
- Click the Plans tab.
- Click Create Plan.
- In the Name field, enter
Token. - In the Billing cadence dropdown menu, select “1 month”.
- Click Save.
- Click Add Rate Card.
- From the Feature dropdown menu, select “ai-token”.
- Click Next Step.
- From the Pricing model dropdown menu, select “Usage Based”.
-
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.
- Click Next Step.
- Select Boolean.
- Click Save Rate Card.
- Click Publish Plan.
- 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.
- In the Konnect sidebar, click Metering & Billing.
- In the Metering & Billing sidebar, click Billing.
- Click Create Customer.
- In the Name field, enter
Kong Air. - In the Include usage from dropdown, select “kong-air”.
- Click Save.
- Click the Subscriptions tab.
- Click Create a Subscription.
- From the Subscribed Plan dropdown, select “Token”.
- Click Next Step.
- 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.
- In the Konnect sidebar, click Metering & Billing.
- In the Metering & Billing sidebar, click Billing.
- Click the Invoices tab.
- Click Kong Air.
- Click the Invoicing tab.
- 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
Clean up Konnect environment
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.