본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

미터링 & 빌링: AI 입력·출력 토큰 측정

원문 보기 위키 갱신

미터링 & 빌링: AI 입력·출력 토큰 측정 (Metering & Billing: Meter AI input and output tokens)

이 문서는 AI Gateway 요청마다 입력(프롬프트) 토큰과 출력(컴플리션) 토큰을 각각 별도 이벤트로 기록하도록 Metering & Billing 플러그인을 설정하는 방법을 안내해요. 입력과 출력 토큰을 따로 기록하기 때문에 두 비용을 각각 다르게 책정할 수 있어요. 사용량을 집계해 인보이스를 생성하려면 Konnect Metering & Billing이나 자체 호스팅 OpenMeter가 필요하고, AI Proxy 플러그인과 인증 플러그인이 같은 Gateway Service나 Route에 구성되어 있어야 해요. decK, Admin API, Konnect API 방식별 설정 예시와 필요한 환경 변수도 함께 소개해요

출처: 문서

본문

AI 입력 및 출력 토큰 미터링v3.14+

이 예제는 AI 토큰 미터링을 활성화해요. AI Gateway 요청이 있을 때마다 플러그인은 두 개의 이벤트를 발생시켜요. 하나는 입력(prompt) 토큰에 대한 이벤트이고, 다른 하나는 출력(completion) 토큰에 대한 이벤트예요. 이 덕분에 입력과 출력에 대해 각각 독립적으로 가격을 책정할 수 있어요.

사용량을 집계하고 청구서를 생성하려면 Konnect Metering & Billing 또는 셀프 호스팅 OpenMeter 인스턴스가 필요해요.

사전 준비

환경 변수

플러그인 설정

Global

decK

다음 섹션을 kong.yaml 구성 파일에 추가하세요:

_format_version: "3.0"
plugins:
  - name: metering-and-billing
    config:
      ingest_endpoint: ${{ env "DECK_INGEST_ENDPOINT" }}
      api_token: ${{ env "DECK_AUTH_TOKEN" }}
      ssl_verify: true
      meter_api_requests: false
      meter_ai_token_usage: true
      subject:
        look_up_value_in: consumer

{: data-file="kong.yaml" data-tool="deck" }

Admin API

다음 요청을 전송하세요:

curl -i -X POST http://localhost:8001/plugins/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "name": "metering-and-billing",
      "config": {
        "ingest_endpoint": "'$INGEST_ENDPOINT'",
        "api_token": "'$AUTH_TOKEN'",
        "ssl_verify": true,
        "meter_api_requests": false,
        "meter_ai_token_usage": true,
        "subject": {
          "look_up_value_in": "consumer"
        }
      }
    }
    '
Konnect API

다음 요청을 전송하세요:

curl -X POST https://{region}.api.konghq.com/v2/control-planes/{controlPlaneId}/core-entities/plugins/ \
    --header "accept: application/json" \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer $KONNECT_TOKEN" \
    --data '
    {
      "name": "metering-and-billing",
      "config": {
        "ingest_endpoint": "'$INGEST_ENDPOINT'",
        "api_token": "'$AUTH_TOKEN'",
        "ssl_verify": true,
        "meter_api_requests": false,
        "meter_ai_token_usage": true,
        "subject": {
          "look_up_value_in": "consumer"
        }
      }
    }
    '

다음 플레이스홀더는 반드시 여러분의 값으로 바꾸세요:

  • region: Kong Konnect가 호스팅되고 운영되는 지리적 리전.

  • KONNECT_TOKEN: Konnect 계정과 연결된 개인 액세스 토큰(PAT).

  • controlPlaneId: 컨트롤 플레인의 id.

리전별 URL과 개인 액세스 토큰에 대해 자세히 알아보려면 Konnect Control Planes Config API 참조를 확인하세요.

KIC
echo "
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
  name:
  namespace: kong
  annotations:
    kubernetes.io/ingress.class: kong
  labels:
    global: 'true'
config:
  ingest_endpoint: '$INGEST_ENDPOINT'
  api_token: '$AUTH_TOKEN'
  ssl_verify: true
  meter_api_requests: false
  meter_ai_token_usage: true
  subject:
    look_up_value_in: consumer
plugin: metering-and-billing
" | kubectl apply -f -
Terraform
사전 준비: 개인 액세스 토큰 구성
terraform {
  required_providers {
    konnect = {
      source  = "kong/konnect"
    }
  }
}

provider "konnect" {
  personal_access_token = "$KONNECT_TOKEN"
  server_url            = "https://us.api.konghq.com/"
}

Konnect Gateway Plugin을 생성하려면 Terraform 구성에 다음을 추가하세요:

resource "konnect_gateway_plugin_metering_and_billing" "my_metering_and_billing" {
  enabled = true

  config = {
    ingest_endpoint = var.ingest_endpoint
    api_token = var.auth_token
    ssl_verify = true
    meter_api_requests = false
    meter_ai_token_usage = true

    subject = {
      look_up_value_in = "consumer"
    }
  }

  control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
}

이 예제를 사용하려면 매니페스트에 다음 변수를 추가해야 해요. TF_VAR_name=value를 설정하면 런타임에 값을 지정할 수 있어요.

variable "auth_token" {
  type = string
}
variable "ingest_endpoint" {
  type = string
}

Service

decK

다음 섹션을 kong.yaml 구성 파일에 추가하세요:

_format_version: "3.0"
plugins:
  - name: metering-and-billing
    service: serviceName|Id
    config:
      ingest_endpoint: ${{ env "DECK_INGEST_ENDPOINT" }}
      api_token: ${{ env "DECK_AUTH_TOKEN" }}
      ssl_verify: true
      meter_api_requests: false
      meter_ai_token_usage: true
      subject:
        look_up_value_in: consumer

{: data-file="kong.yaml" data-tool="deck" }

다음 플레이스홀더는 반드시 여러분의 값으로 바꾸세요:

  • serviceName|Id: 플러그인 구성의 대상이 될 서비스의 id 또는 name.
Admin API

다음 요청을 전송하세요:

curl -i -X POST http://localhost:8001/services/{serviceName|Id}/plugins/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "name": "metering-and-billing",
      "config": {
        "ingest_endpoint": "'$INGEST_ENDPOINT'",
        "api_token": "'$AUTH_TOKEN'",
        "ssl_verify": true,
        "meter_api_requests": false,
        "meter_ai_token_usage": true,
        "subject": {
          "look_up_value_in": "consumer"
        }
      }
    }
    '

다음 플레이스홀더는 반드시 여러분의 값으로 바꾸세요:

  • serviceName|Id: 플러그인 구성의 대상이 될 서비스의 id 또는 name.
Konnect API

다음 요청을 전송하세요:

curl -X POST https://{region}.api.konghq.com/v2/control-planes/{controlPlaneId}/core-entities/services/{serviceId}/plugins/ \
    --header "accept: application/json" \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer $KONNECT_TOKEN" \
    --data '
    {
      "name": "metering-and-billing",
      "config": {
        "ingest_endpoint": "'$INGEST_ENDPOINT'",
        "api_token": "'$AUTH_TOKEN'",
        "ssl_verify": true,
        "meter_api_requests": false,
        "meter_ai_token_usage": true,
        "subject": {
          "look_up_value_in": "consumer"
        }
      }
    }
    '

다음 플레이스홀더는 반드시 여러분의 값으로 바꾸세요:

  • region: Kong Konnect가 호스팅되고 운영되는 지리적 리전.

  • KONNECT_TOKEN: Konnect 계정과 연결된 개인 액세스 토큰(PAT).

  • controlPlaneId: 컨트롤 플레인의 id.

  • serviceId: 플러그인 구성의 대상이 될 서비스의 id.

리전별 URL과 개인 액세스 토큰에 대해 자세히 알아보려면 Konnect Control Planes Config API 참조를 확인하세요.

KIC
echo "
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name:
  namespace: kong
  annotations:
    kubernetes.io/ingress.class: kong
config:
  ingest_endpoint: '$INGEST_ENDPOINT'
  api_token: '$AUTH_TOKEN'
  ssl_verify: true
  meter_api_requests: false
  meter_ai_token_usage: true
  subject:
    look_up_value_in: consumer
plugin: metering-and-billing
" | kubectl apply -f -

다음으로, service 리소스에 어노테이션을 추가해서 KongPlugin 리소스를 적용하세요:

kubectl annotate -n kong service SERVICE_NAME konghq.com/plugins=
Terraform
사전 준비: 개인 액세스 토큰 구성
terraform {
  required_providers {
    konnect = {
      source  = "kong/konnect"
    }
  }
}

provider "konnect" {
  personal_access_token = "$KONNECT_TOKEN"
  server_url            = "https://us.api.konghq.com/"
}

Konnect Gateway Plugin을 생성하려면 Terraform 구성에 다음을 추가하세요:

resource "konnect_gateway_plugin_metering_and_billing" "my_metering_and_billing" {
  enabled = true

  config = {
    ingest_endpoint = var.ingest_endpoint
    api_token = var.auth_token
    ssl_verify = true
    meter_api_requests = false
    meter_ai_token_usage = true

    subject = {
      look_up_value_in = "consumer"
    }
  }

  control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
  service = {
    id = konnect_gateway_service.my_service.id
  }
}

이 예제를 사용하려면 매니페스트에 다음 변수를 추가해야 해요. TF_VAR_name=value를 설정하면 런타임에 값을 지정할 수 있어요.

variable "auth_token" {
  type = string
}
variable "ingest_endpoint" {
  type = string
}

Route

decK

다음 섹션을 kong.yaml 구성 파일에 추가하세요:

_format_version: "3.0"
plugins:
  - name: metering-and-billing
    route: routeName|Id
    config:
      ingest_endpoint: ${{ env "DECK_INGEST_ENDPOINT" }}
      api_token: ${{ env "DECK_AUTH_TOKEN" }}
      ssl_verify: true
      meter_api_requests: false
      meter_ai_token_usage: true
      subject:
        look_up_value_in: consumer

{: data-file="kong.yaml" data-tool="deck" }

다음 플레이스홀더는 반드시 여러분의 값으로 바꾸세요:

  • routeName|Id: 플러그인 구성의 대상이 될 라우트의 id 또는 name.
Admin API

다음 요청을 전송하세요:

curl -i -X POST http://localhost:8001/routes/{routeName|Id}/plugins/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "name": "metering-and-billing",
      "config": {
        "ingest_endpoint": "'$INGEST_ENDPOINT'",
        "api_token": "'$AUTH_TOKEN'",
        "ssl_verify": true,
        "meter_api_requests": false,
        "meter_ai_token_usage": true,
        "subject": {
          "look_up_value_in": "consumer"
        }
      }
    }
    '

다음 플레이스홀더는 반드시 여러분의 값으로 바꾸세요:

  • routeName|Id: 플러그인 구성의 대상이 될 라우트의 id 또는 name.
Konnect API

다음 요청을 전송하세요:

curl -X POST https://{region}.api.konghq.com/v2/control-planes/{controlPlaneId}/core-entities/routes/{routeId}/plugins/ \
    --header "accept: application/json" \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer $KONNECT_TOKEN" \
    --data '
    {
      "name": "metering-and-billing",
      "config": {
        "ingest_endpoint": "'$INGEST_ENDPOINT'",
        "api_token": "'$AUTH_TOKEN'",
        "ssl_verify": true,
        "meter_api_requests": false,
        "meter_ai_token_usage": true,
        "subject": {
          "look_up_value_in": "consumer"
        }
      }
    }
    '

다음 플레이스홀더는 반드시 여러분의 값으로 바꾸세요:

  • region: Kong Konnect가 호스팅되고 운영되는 지리적 리전.

  • KONNECT_TOKEN: Konnect 계정과 연결된 개인 액세스 토큰(PAT).

  • controlPlaneId: 컨트롤 플레인의 id.

  • routeId: 플러그인 구성의 대상이 될 라우트의 id.

리전별 URL과 개인 액세스 토큰에 대해 자세히 알아보려면 Konnect Control Planes Config API 참조를 확인하세요.

KIC
echo "
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name:
  namespace: kong
  annotations:
    kubernetes.io/ingress.class: kong
config:
  ingest_endpoint: '$INGEST_ENDPOINT'
  api_token: '$AUTH_TOKEN'
  ssl_verify: true
  meter_api_requests: false
  meter_ai_token_usage: true
  subject:
    look_up_value_in: consumer
plugin: metering-and-billing
" | kubectl apply -f -

다음으로, httproute 또는 ingress 리소스에 어노테이션을 추가해서 KongPlugin 리소스를 적용하세요:

Gateway API
kubectl annotate -n kong httproute  konghq.com/plugins=
Ingress
kubectl annotate -n kong ingress  konghq.com/plugins=
Terraform
사전 요구 사항: 개인 액세스 토큰 구성하기
terraform {
  required_providers {
    konnect = {
      source  = "kong/konnect"
    }
  }
}

provider "konnect" {
  personal_access_token = "$KONNECT_TOKEN"
  server_url            = "https://us.api.konghq.com/"
}

Konnect Gateway 플러그인을 생성하려면 Terraform 구성에 다음 내용을 추가하세요:

resource "konnect_gateway_plugin_metering_and_billing" "my_metering_and_billing" {
  enabled = true

  config = {
    ingest_endpoint = var.ingest_endpoint
    api_token = var.auth_token
    ssl_verify = true
    meter_api_requests = false
    meter_ai_token_usage = true

    subject = {
      look_up_value_in = "consumer"
    }
  }

  control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
  route = {
    id = konnect_gateway_route.my_route.id
  }
}

이 예제를 사용하려면 다음 변수를 매니페스트에 추가해야 해요. TF_VAR_name=value를 설정하면 런타임에 값을 지정할 수 있어요.

variable "auth_token" {
  type = string
}
variable "ingest_endpoint" {
  type = string
}

컨슈머

decK

다음 섹션을 kong.yaml 구성 파일에 추가하세요:

_format_version: "3.0"
plugins:
  - name: metering-and-billing
    consumer: consumerName|Id
    config:
      ingest_endpoint: ${{ env "DECK_INGEST_ENDPOINT" }}
      api_token: ${{ env "DECK_AUTH_TOKEN" }}
      ssl_verify: true
      meter_api_requests: false
      meter_ai_token_usage: true
      subject:
        look_up_value_in: consumer

{: data-file="kong.yaml" data-tool="deck" }

다음 플레이스홀더를 여러분의 값으로 바꿔야 해요:

  • consumerName|Id: 플러그인 구성이 대상으로 지정할 컨슈머의 id 또는 name이에요.
Admin API

다음 요청을 보내세요:

curl -i -X POST http://localhost:8001/consumers/{consumerName|Id}/plugins/ \
    --header "Accept: application/json" \
    --header "Content-Type: application/json" \
    --data '
    {
      "name": "metering-and-billing",
      "config": {
        "ingest_endpoint": "'$INGEST_ENDPOINT'",
        "api_token": "'$AUTH_TOKEN'",
        "ssl_verify": true,
        "meter_api_requests": false,
        "meter_ai_token_usage": true,
        "subject": {
          "look_up_value_in": "consumer"
        }
      }
    }
    '

다음 플레이스홀더를 여러분의 값으로 바꿔야 해요:

  • consumerName|Id: 플러그인 구성이 대상으로 지정할 컨슈머의 id 또는 name이에요.
Konnect API

다음 요청을 보내세요:

curl -X POST https://{region}.api.konghq.com/v2/control-planes/{controlPlaneId}/core-entities/consumers/{consumerId}/plugins/ \
    --header "accept: application/json" \
    --header "Content-Type: application/json" \
    --header "Authorization: Bearer $KONNECT_TOKEN" \
    --data '
    {
      "name": "metering-and-billing",
      "config": {
        "ingest_endpoint": "'$INGEST_ENDPOINT'",
        "api_token": "'$AUTH_TOKEN'",
        "ssl_verify": true,
        "meter_api_requests": false,
        "meter_ai_token_usage": true,
        "subject": {
          "look_up_value_in": "consumer"
        }
      }
    }
    '

다음 플레이스홀더를 여러분의 값으로 바꿔야 해요:

  • region: Kong Konnect가 호스팅되고 운영되는 지리적 리전이에요.

  • KONNECT_TOKEN: Konnect 계정에 연결된 개인 액세스 토큰(PAT)이에요.

  • controlPlaneId: 컨트롤 플레인의 id예요.

  • consumerId: 플러그인 구성이 대상으로 지정할 컨슈머의 id예요.

리전별 URL과 개인 액세스 토큰에 대해 자세히 알아보려면 Konnect Control Planes Config API 레퍼런스를 확인하세요.

KIC
echo "
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name:
  namespace: kong
  annotations:
    kubernetes.io/ingress.class: kong
config:
  ingest_endpoint: '$INGEST_ENDPOINT'
  api_token: '$AUTH_TOKEN'
  ssl_verify: true
  meter_api_requests: false
  meter_ai_token_usage: true
  subject:
    look_up_value_in: consumer
plugin: metering-and-billing
" | kubectl apply -f -

다음으로, KongConsumer 리소스에 어노테이션을 붙여서 KongPlugin 리소스를 적용하세요:

kubectl annotate -n kong kongconsumer CONSUMER_NAME konghq.com/plugins=
Terraform
사전 요구 사항: 개인 액세스 토큰 구성하기
terraform {
  required_providers {
    konnect = {
      source  = "kong/konnect"
    }
  }
}

provider "konnect" {
  personal_access_token = "$KONNECT_TOKEN"
  server_url            = "https://us.api.konghq.com/"
}

Konnect Gateway 플러그인을 생성하려면 Terraform 구성에 다음 내용을 추가하세요:

resource "konnect_gateway_plugin_metering_and_billing" "my_metering_and_billing" {
  enabled = true

  config = {
    ingest_endpoint = var.ingest_endpoint
    api_token = var.auth_token
    ssl_verify = true
    meter_api_requests = false
    meter_ai_token_usage = true

    subject = {
      look_up_value_in = "consumer"
    }
  }

  control_plane_id = konnect_gateway_control_plane.my_konnect_cp.id
  consumer = {
    id = konnect_gateway_consumer.my_consumer.id
  }
}

이 예제를 사용하려면 다음 변수를 매니페스트에 추가해야 해요. TF_VAR_name=value를 설정하면 런타임에 값을 지정할 수 있어요.

variable "auth_token" {
  type = string
}
variable "ingest_endpoint" {
  type = string
}

관련 리소스

더 알아보기 (Learn more)