Rediger

Del via


Emit metrics for consumption of Azure OpenAI tokens

APPLIES TO: All API Management tiers

The azure-openai-emit-token-metric policy sends metrics to Application Insights about consumption of large language model tokens through Azure OpenAI Service APIs. Token count metrics include: Total Tokens, Prompt Tokens, and Completion Tokens.

Note

Set the policy's elements and child elements in the order provided in the policy statement. Learn more about how to set or edit API Management policies.

Prerequisites

Policy statement

<azure-openai-emit-token-metric
        namespace="metric namespace" >      
        <dimension name="dimension name" value="dimension value" />
        ...additional dimensions...
</azure-openai-emit-token-metric>

Attributes

Attribute Description Required Default value
namespace A string. Namespace of metric. Policy expressions aren't allowed. No API Management
value Value of metric expressed as a double. Policy expressions are allowed. No 1

Elements

Element Description Required
dimension Add one or more of these elements for each dimension included in the metric. Yes

dimension attributes

Attribute Description Required Default value
name A string or policy expression. Name of dimension. Yes N/A
value A string or policy expression. Value of dimension. Can only be omitted if name matches one of the default dimensions. If so, value is provided as per dimension name. No N/A

Default dimension names that may be used without value

  • API ID
  • Operation ID
  • Product ID
  • User ID
  • Subscription ID
  • Location
  • Gateway ID

Usage

Usage notes

  • This policy can be used multiple times per policy definition.
  • You can configure at most 10 custom definitions for this policy.
  • This policy can optionally be configured when adding an API from the Azure OpenAI Service using the portal.

Example

The following example sends Azure OpenAI token count metrics to Application Insights along with User ID, Client IP, and API ID as dimensions.

<policies>
  <inbound>
      <azure-openai-emit-token-metric
            namespace="AzureOpenAI">   
            <dimension name="User ID" />
            <dimension name="Client IP" value="@(context.Request.IpAddress)" />
            <dimension name="API ID" />
        </azure-openai-emit-token-metric> 
  </inbound>
  <outbound>
  </outbound>
</policies>

For more information about working with policies, see: