This guide shows how to meter LLM traffic from AI Gateway and convert that usage into billable revenue with Metering & Billing in Konnect.
Monetize AI Gateway traffic
Create an AI Consumer, an AI Model Provider, and an AI Model. Attach the Metering & Billing Policy to the AI Model to emit usage events for LLM token consumption. Then, use Metering & Billing to turn those events into billable usage by creating a meter for LLM tokens, defining a feature, creating a plan with rate cards, and starting a subscription for billing.
Prerequisites
AI Gateway running
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' -
Run the AI Gateway quickstart script to automatically provision a control plane and data plane in Kong Konnect, and configure your environment:
curl -Ls https://get.konghq.com/ai | bash -s -- -k $KONNECT_TOKEN
This sets up a AI Gateway control plane named ai-quickstart, provisions a local data plane, and prints out the following environment variables export:
export AI_GATEWAY_ID=your-gateway-id
export KONNECT_TOKEN=$KONNECT_TOKEN
export KONNECT_CONTROL_PLANE_NAME=ai-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.
kongctl v1.15.1+
This tutorial uses kongctl to manage Konnect resources programmatically. We recommend keeping kongctl up to date with the latest version (1.15.1).
- Install kongctl from developer.konghq.com/kongctl.
-
Verify the installation:
kongctl version
OpenAI
- Create an OpenAI account.
- Get an API key.
- Export the API key as a variable:
export OPENAI_API_KEY="<YOUR_OPENAI_API_KEY>" export OPENAI_AUTH_HEADER="Bearer $OPENAI_API_KEY"
Konnect system account token
You need a Konnect system account token (spat_) with the Ingest role for Metering.
This token authenticates the Metering & Billing plugin when it sends events to the Konnect ingest endpoint.
Export your system account token:
export AUTH_TOKEN='YOUR SPAT TOKEN'For more information, see system accounts and access tokens.
Create an AI Consumer and API key credential
Before you configure Metering & Billing, set up an AI Consumer. AI Consumers identify the client that’s interacting with AI Gateway. Later in this guide, you’ll map this AI Consumer to a customer in Metering & Billing and assign them to a Premium plan, so existing AI Consumers that are already consuming your APIs become billable.
-
Create the AI Consumer:
kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF' ai_gateway_consumers: - ref: kong-air ai_gateway: !lookup {id: !env AI_GATEWAY_ID} display_name: "Kong Air" name: kong-air type: api-key policies: [] EOF -
Fetch the
kong-airAI Consumer’s UUID:export CONSUMER_ID="$(kongctl get ai-gateway consumers \ --gateway-id "$AI_GATEWAY_ID" kong-air \ --output json --jq '.id' --jq-raw-output \ --pat "$KONNECT_TOKEN")" -
Create an API key credential for Kong Air:
CONSUMER_API_KEY=$(curl -X POST "https://us.api.konghq.com/v1/ai-gateways/$AI_GATEWAY_ID/consumers/$CONSUMER_ID/credentials" \ --no-progress-meter --fail-with-body \ -H "Authorization: Bearer $KONNECT_TOKEN"\ -H "Content-Type: application/json"\ -H "Accept: application/json, application/problem+json" \ --json '{ "display_name": "Kong Air key", "name": "kong-air-key", "type": "api-key" }' | jq -r ".api_key" )The
api_keyvalue in the response is generated by the server and can’t be retrieved later, so$CONSUMER_API_KEYcaptures it for use later in this guide.
Create AI Gateway entities and a Metering & Billing Policy
Create the following entities:
- A
key-authAI Auth Strategy that accepts an API key in theapikeyheader - An AI Model Provider to connect to OpenAI
- A Metering & Billing Policy to meter LLM token usage by AI Consumers
- An AI Model that authenticates with the AI Auth Strategy and has the Policy attached
kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF'
ai_gateway_auth_strategies:
- ref: kong-air-key-auth
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
display_name: "Kong Air key auth"
name: kong-air-key-auth
type: key-auth
config:
key_names: [apikey]
key_in_header: true
key_in_query: false
hide_credentials: true
ai_gateway_model_providers:
- ref: generic-openai
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: generic-openai
display_name: "generic-openai"
type: openai
config:
auth:
type: basic
headers:
- name: Authorization
value: !secret {source: !env OPENAI_AUTH_HEADER}
ai_gateway_policies:
- ref: meter-llm-tokens
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
name: meter-llm-tokens
display_name: "Meter LLM tokens"
type: metering-and-billing
global: false
config:
ingest_endpoint: https://us.api.konghq.com/v3/openmeter/events
api_token: !env AUTH_TOKEN
meter_api_requests: false
meter_ai_token_usage: true
subject:
look_up_value_in: consumer
ai_gateway_models:
- ref: kong-air-chat
ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
display_name: "Kong Air chat"
name: kong-air-chat
type: model
enabled: true
formats:
- type: openai
capabilities:
- generate
access:
auth_strategies:
- !ref kong-air-key-auth#name
policies:
- !ref meter-llm-tokens#name
config:
route:
paths: [/v1]
model:
body_param: model
values: [kong-air-chat]
targets:
- name: gpt-4o
provider: generic-openai
config:
type: openai
EOFThe Metering & Billing Policy’s subject.look_up_value_in: consumer resolves the billable customer from the authenticated AI Consumer on each request, and meter_ai_token_usage: true emits an event per request with input and output token counts.
Create a meter
In Metering & Billing, meters track and record the consumption of a resource or service over time. In this case, you want to track the number of AI tokens consumed:
METER_ID=$(curl -X POST "https://us.api.konghq.com/v3/openmeter/meters" \
--no-progress-meter --fail-with-body \
-H "Authorization: Bearer $KONNECT_TOKEN" \
--json '{
"name": "LLM Tokens",
"key": "llm_tokens_total",
"description": "Number of input and output tokens across models",
"event_type": "kong.llm_request",
"aggregation": "sum",
"value_property": "$.tokens",
"dimensions": {
"model": "$.model",
"provider": "$.provider",
"type": "$.type"
}
}' | jq -r ".id"
)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, label that as something you want to price or govern.
Create a feature for the kong-air-chat AI Model you created earlier. The group by filters ensure you only bill for LLM tokens from a specific provider and request type:
FEATURE_ID=$(curl -X POST "https://us.api.konghq.com/v3/openmeter/features" \
--no-progress-meter --fail-with-body \
-H "Authorization: Bearer $KONNECT_TOKEN" \
--json '{
"name": "ai-token",
"key": "ai_token",
"meter": {
"id": "'$METER_ID'",
"filters": {
"provider": {
"eq": "openai"
},
"type": {
"eq": "request"
}
}
}
}' | jq -r ".id"
)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.
-
Create the plan and rate card:
PLAN_ID=$(curl -X POST "https://us.api.konghq.com/v3/openmeter/plans" \ --no-progress-meter --fail-with-body \ -H "Authorization: Bearer $KONNECT_TOKEN" \ --json '{ "name": "Token", "key": "token", "currency": "USD", "billing_cadence": "P1M", "phases": [ { "name": "Main phase", "key": "main", "rate_cards": [ { "name": "ai-token", "key": "ai_token", "billing_cadence": "P1M", "feature": { "id": "'$FEATURE_ID'" }, "price": { "type": "unit", "amount": "1" }, "entitlement": { "type": "boolean" } } ] } ] }' | jq -r ".id" )We’re using a price of
1here 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. -
Publish the plan so it can be subscribed to:
curl -X POST "https://us.api.konghq.com/v3/openmeter/plans/$PLAN_ID/publish" \ --no-progress-meter --fail-with-body \ -H "Authorization: Bearer $KONNECT_TOKEN"
Start a subscription
Customers are the entities who pay for the consumption. In many cases, it’s equal to your AI Consumer. Create a customer and map your AI Consumer to it.
-
Create a customer for Kong Air:
curl -X POST "https://us.api.konghq.com/v3/openmeter/customers" \ --no-progress-meter --fail-with-body \ -H "Authorization: Bearer $KONNECT_TOKEN" \ --json '{ "name": "Kong Air", "key": "kong-air", "usage_attribution": { "subject_keys": [ "consumer:'$CONSUMER_ID'" ] } }' -
Start a subscription for Kong Air on the Token plan:
curl -X POST "https://us.api.konghq.com/v3/openmeter/subscriptions" \ --no-progress-meter --fail-with-body \ -H "Authorization: Bearer $KONNECT_TOKEN" \ --json '{ "customer": { "key": "kong-air" }, "plan": { "key": "token" } }'
Validate
-
Send a test request to verify that the Kong Air AI Consumer is invoiced correctly:
curl -X POST "$KONNECT_PROXY_URL/v1/chat/completions" \ --no-progress-meter --fail-with-body \ -H "Accept: application/json"\ -H "Content-Type: application/json"\ -H "apikey: $CONSUMER_API_KEY" \ --json '{ "model": "kong-air-chat", "messages": [ { "role": "system", "content": "You are a mathematician" }, { "role": "user", "content": "What is 1+1?" } ] }'This generates AI LLM token usage that Metering & Billing captures.
Entitlement enforcement: AI Gateway does not automatically block traffic when a customer’s entitlement is exhausted. To enforce limits, set up a webhook notification rule and cut off access in your own infrastructure, or attach an AI Rate Limiting Advanced Policy to the AI Model. See Enforcing entitlements for details.
- 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. This guide uses the sandbox for invoices. To deploy your subscription in production, configure a payments integration in Metering & Billing > Settings.
Cleanup
Clean up AI Gateway resources
To clean up all AI Gateway resources created in this guide, run:
curl -Ls https://get.konghq.com/ai | bash -s -- -d