Set up a budget with Slack alerts in AI Cost Management

TL;DR

To send Slack alerts when AI spend reaches a limit, create a Slack notification channel from an incoming webhook URL, then create a limit budget scoped to your provider with a monthly budget window.

Add a notification rule to the budget to trigger the alerts, and list the Slack channel on the rule.

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 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.

Want a fully-hosted AI Gateway? See AI Gateway 2.0 on Dedicated Cloud Gateways.

This tutorial uses kongctl to manage Konnect resources programmatically. We recommend keeping kongctl up to date with the latest version (1.24.0).

  1. Install kongctl from developer.konghq.com/kongctl.
  2. Verify the installation:

    kongctl version
  1. Create an OpenAI account.
  2. Get an API key.
  3. Export the API key as a variable:
     export OPENAI_API_KEY="<YOUR_OPENAI_API_KEY>"
     export OPENAI_AUTH_HEADER="Bearer $OPENAI_API_KEY"

A Slack notification channel posts alerts to an incoming webhook that you create in your own Slack workspace. AI Cost Management has no Slack app of its own, so you create the webhook from an app in your own workspace.

To create the webhook:

  1. Open Create a Slack app.
  2. Select Blank app.
  3. In the App name field, enter a name like Kong AI Cost Management.
  4. Select the Slack workspace that receives the alerts, then click Continue.
  5. Set bot scopes to incoming-webhook, then click Continue.
  6. Click Continue to skip What it can respond to, which you don’t need for this example.
  7. Click Create and Install.
  8. In the auth dialog, choose a channel in your workspace.
  9. Click Allow.
  10. Click Go to App Settings.
  11. Under Features, click Incoming Webhooks.
  12. Copy the Webhook URL for your webhook. The webhook URL must start with https://hooks.slack.com/services/.
  13. Export the webhook URL as an environment variable:

    export SLACK_WEBHOOK_URL='YOUR_SLACK_WEBHOOK_URL'

Caution: Anyone with the URL can post to the Slack channel, so store it securely.

A budget tracks the AI spend of requests that pass through AI Gateway against a limit, then sends alerts as spend grows toward that limit.

This guide shows how AI Cost Management turns AI spend into Slack alerts. You create a monthly budget for OpenAI usage and a Slack channel, then connect them with a notification rule that alerts at 50, 80, and 100 percent of the limit.

Create an AI Model Provider and an AI Model

Create an AI Model Provider to connect AI Gateway to OpenAI and store your credentials, and an AI Model that routes requests to that provider. Requests to the model will produce the spend that the budget tracks:

kongctl apply -f - --auto-approve --pat "$KONNECT_TOKEN" << 'EOF'
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_models:
  - ref: budget-gpt-4o
    ai_gateway: !lookup {id: !env AI_GATEWAY_ID}
    name: budget-gpt-4o
    display_name: "budget-gpt-4o"
    type: model
    formats:
      - type: openai
    capabilities:
      - generate
    config:
      route:
        paths:
          - /v1
        model:
          body_param: model
          values:
            - budget-gpt-4o
    targets:
      - name: gpt-4o
        provider: generic-openai
        config:
          type: openai
    policies: []
EOF

!env AI_GATEWAY_ID references the AI Gateway that the quickstart script created in the prerequisites, instead of creating a new one.

Create a Slack notification channel

A notification channel is a destination for budget alerts. Create a Slack channel using the /v1/ai-cost-management/notification-channels endpoint. The channel posts each alert to the incoming webhook in slack.webhook_url:

CHANNEL_ID=$(curl -X POST "https://us.api.konghq.com/v1/ai-cost-management/notification-channels" \
     --no-progress-meter --fail-with-body  \
     -H "Authorization: Bearer $KONNECT_TOKEN"\
     -H "Content-Type: application/json"\
     -H "Accept: application/json, application/problem+json" \
     --json '{
       "name": "budget-alerts-slack",
       "type": "slack",
       "slack": {
         "webhook_url": "'$SLACK_WEBHOOK_URL'"
       }
     }' | jq -r ".id"
)

Create a budget

Create a limit budget using the /v1/ai-cost-management/budgets endpoint. The scope filter selects requests to the openai provider, and the limit applies to all of them together. Each budget window lasts one month (P1M). The anchor is January 1, 2026 in UTC, so every window starts on the first day of a month:

BUDGET_ID=$(curl -X POST "https://us.api.konghq.com/v1/ai-cost-management/budgets" \
     --no-progress-meter --fail-with-body  \
     -H "Authorization: Bearer $KONNECT_TOKEN"\
     -H "Content-Type: application/json"\
     -H "Accept: application/json, application/problem+json" \
     --json '{
       "name": "openai-monthly-budget",
       "effect": "limit",
       "scope": {
         "filters": [
           {
             "field": "provider",
             "op": "eq",
             "value": "openai"
           }
         ]
       },
       "limit": {
         "currency": "USD",
         "value": "500"
       },
       "window": {
         "type": "fixed",
         "interval": "P1M",
         "anchor": "2026-01-01T00:00:00+00:00[UTC]"
       }
     }' | jq -r ".id"
)

The budget observes spend and sends alerts. Requests to OpenAI continue after spend reaches the $500 limit.

Add a notification rule

Add a notification rule to the budget using the /v1/ai-cost-management/budgets/{budgetId}/notification-rule endpoint.

The rule alerts the Slack channel when spend reaches 50, 80, and 100 percent of the limit. Each threshold triggers an alert at most once per budget window:

curl -X PUT "https://us.api.konghq.com/v1/ai-cost-management/budgets/$BUDGET_ID/notification-rule" \
     --no-progress-meter --fail-with-body  \
     -H "Authorization: Bearer $KONNECT_TOKEN"\
     -H "Content-Type: application/json"\
     -H "Accept: application/json, application/problem+json" \
     --json '{
       "thresholds": [
         {
           "percent": 50
         },
         {
           "percent": 80
         },
         {
           "percent": 100
         }
       ],
       "channels": [
         {
           "id": "'$CHANNEL_ID'"
         }
       ]
     }'

Validate

Send a request through AI Gateway so the budget has spend to track:

curl -X POST "$KONNECT_PROXY_URL/v1/chat/completions" \
     --no-progress-meter --fail-with-body  \
     -H "Accept: application/json"\
     -H "Content-Type: application/json" \
     --json '{
       "model": "budget-gpt-4o",
       "messages": [
         {
           "role": "user",
           "content": "Say this is a test!"
         }
       ]
     }'

Next, send a test message to the Slack channel to confirm that alerts arrive:

curl -X POST "https://us.api.konghq.com/v1/ai-cost-management/notification-channels/$CHANNEL_ID/tests" \
     --no-progress-meter --fail-with-body  \
     -H "Authorization: Bearer $KONNECT_TOKEN"

Check the Slack channel that your webhook posts to. You should see a message titled Test message from channel "budget-alerts-slack".

Check the balance of the budget:

curl -X GET "https://us.api.konghq.com/v1/ai-cost-management/budgets/$BUDGET_ID/balance" \
     --no-progress-meter --fail-with-body  \
     -H "Authorization: Bearer $KONNECT_TOKEN"

The response shows the limit (500 US dollars), the spend so far in consumed, the amount left in remaining, and the spend expected by the end of the budget window in projected.

Usage takes a short time to reach the service, so consumed can show no spend right after the request. The latest_record_at field shows when the newest counted usage arrived, and it’s null until usage matches the budget. The projected field rounds to the cent, so it can show 0 when spend is a fraction of a cent.

Cleanup

Delete the budget:

curl -X DELETE "$KONNECT_CONTROL_PLANE_URL/v1/ai-cost-management/budgets/$BUDGET_ID" \
  -H "Authorization: Bearer $KONNECT_TOKEN"

Delete the notification channel:

curl -X DELETE "$KONNECT_CONTROL_PLANE_URL/v1/ai-cost-management/notification-channels/$CHANNEL_ID" \
  -H "Authorization: Bearer $KONNECT_TOKEN"

To clean up all AI Gateway resources created in this guide, run:

curl -Ls https://get.konghq.com/ai | bash -s -- -d

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!