How to intermediate · 3 min read

Azure OpenAI usage monitoring

Quick answer
Use the Azure Portal's Metrics and Usage + Quotas page to monitor your Azure OpenAI resource usage and costs. Programmatically, use the Azure SDK for Python to query usage details via the Azure Consumption API or Azure Monitor. This enables tracking API calls, token usage, and spending in real time.

PREREQUISITES

  • Python 3.8+
  • Azure subscription with Azure OpenAI resource
  • Azure CLI installed and logged in
  • pip install azure-identity azure-mgmt-consumption azure-mgmt-monitor

Setup

Install the required Azure SDK packages and authenticate using DefaultAzureCredential which supports environment variables or Azure CLI login.

bash
pip install azure-identity azure-mgmt-consumption azure-mgmt-monitor
output
Collecting azure-identity
Collecting azure-mgmt-consumption
Collecting azure-mgmt-monitor
Successfully installed azure-identity azure-mgmt-consumption azure-mgmt-monitor-<version>

Step by step

This example shows how to list usage details for your Azure OpenAI resource using the Azure Consumption Management client.

python
import os
from datetime import datetime, timedelta
from azure.identity import DefaultAzureCredential
from azure.mgmt.consumption import ConsumptionManagementClient

# Authenticate
credential = DefaultAzureCredential()
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]

# Initialize client
client = ConsumptionManagementClient(credential, subscription_id)

# Define time range for usage (last 7 days)
end_date = datetime.utcnow().date()
start_date = end_date - timedelta(days=7)

# Query usage details for Azure OpenAI
usage_list = client.usage_details.list(
    scope=f"/subscriptions/{subscription_id}",
    filter=f"properties/usageStart ge '{start_date}' and properties/usageEnd le '{end_date}' and properties/meterCategory eq 'AI Services'"
)

# Print usage records
for usage in usage_list:
    print(f"Date: {usage.properties.usage_start}, Resource: {usage.properties.instance_name}, Quantity: {usage.properties.quantity}, Cost: {usage.properties.cost}")
output
Date: 2026-03-28, Resource: my-azure-openai, Quantity: 1234, Cost: 12.34
Date: 2026-03-29, Resource: my-azure-openai, Quantity: 1500, Cost: 15.00
... (more records)

Common variations

  • Use azure-mgmt-monitor to query metrics like TokenCount or RequestCount for more granular monitoring.
  • Use asynchronous Azure SDK clients for non-blocking calls.
  • Monitor usage via Azure Portal under your Azure OpenAI resource's Metrics and Usage + Quotas tabs for real-time dashboards.
python
from azure.monitor.query import MetricsQueryClient
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()
metrics_client = MetricsQueryClient(credential)

resource_id = "/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.CognitiveServices/accounts/{resource_name}"

response = metrics_client.query(
    resource_id,
    metric_names=["TokenCount", "RequestCount"],
    timespan="PT24H"
)

for metric in response.metrics:
    print(f"Metric: {metric.name}")
    for time_series in metric.timeseries:
        for data in time_series.data:
            print(f"Time: {data.timestamp}, Value: {data.total}")
output
Metric: TokenCount
Time: 2026-04-01T00:00:00Z, Value: 10000
Time: 2026-04-01T01:00:00Z, Value: 12000
Metric: RequestCount
Time: 2026-04-01T00:00:00Z, Value: 500
Time: 2026-04-01T01:00:00Z, Value: 600

Troubleshooting

  • If you get authentication errors, ensure AZURE_SUBSCRIPTION_ID is set and you are logged in via az login or have set environment variables for service principal credentials.
  • If usage data is missing, verify the time range and filter parameters match your Azure OpenAI resource.
  • Check Azure Portal quotas if usage metrics seem inconsistent.

Key Takeaways

  • Use Azure ConsumptionManagementClient to programmatically retrieve usage and cost data for Azure OpenAI.
  • Azure Monitor MetricsQueryClient provides detailed token and request counts for fine-grained monitoring.
  • Always authenticate with DefaultAzureCredential and set your subscription ID in environment variables.
  • Azure Portal offers built-in dashboards for real-time usage and quota monitoring.
  • Check authentication and filter parameters if usage data is incomplete or missing.
Verified 2026-04
Verify ↗