How to log context size (prompt-tokens response header) in azure apim policy

Das, D. (Debjyoti) 5 Reputation points
2026-08-11T09:32:16.57+00:00

Hello,

We have azure openai instances behind an azure api management. For gpt-5.6 models, the cost per 1M token depends on context size. We are using llm-emit-token-metric policy to log aggregated token usage (prompt, complete, total tokens) per minute, per client, per model in the apim policy and we calculate the cost for each client based on this. But how do we do it for the gpt-5.6 models where each request may be a short or long context? How to log the context size along with the aggregated token usage? What is the recommendation from Microsoft on this? We need to distribute cost among our consumers based on this logging.

Thank you,

Debjyoti Das

Azure API Management
Azure API Management

An Azure service that provides a hybrid, multi-cloud management platform for APIs.


5 answers

Sort by: Most helpful
  1. SHOUMIK CHAKRAVARTY 0 Reputation points
    2026-08-12T16:04:41.9833333+00:00

    Hello @Das, D. (Debjyoti) - The thread's already pointing at per-request logging, which is the right destination. What's worth spelling out is why the metric policy can't be stretched to cover context size - otherwise it reads as a preference rather than a constraint.

    Two hard limits in the llm-emit-token-metric reference. You get at most 5 custom dimensions per policy. And the active time series a policy produces is the product of the unique values across those dimensions, measured against an Azure Monitor ceiling of 50,000 active time series per region per subscription in any 12-hour window.

    Prompt-token counts have effectively unbounded cardinality - every distinct value creates its own series. Multiply that against your existing client and model dimensions and you'd cross the limit almost immediately.

    So per-request logging isn't just the cleaner option here - it's the only one that works.

    One thing worth checking on your current setup. The same reference notes token counts are inaccurate if a stream is interrupted, and that some OpenAI models don't return them at all when streaming unless you set include_usage: true in the request. If any of your traffic streams, the numbers you're billing from today may already be incomplete.

    Was this answer helpful?

    0 comments No comments

  2. Sanjib Kumar Saha 0 Reputation points
    2026-08-11T11:30:48.7566667+00:00

    We have Azure OpenAI instances behind Azure API Management and use the llm-emit-token-metric policy to track aggregated prompt, completion, and total tokens by client and model.

    Was this answer helpful?

    0 comments No comments

  3. Sanjib Kumar Saha 0 Reputation points
    2026-08-11T10:14:06.29+00:00

    If your GPT-5 pricing varies based on context size (prompt length), you should log the prompt token count for each request and associate it with metadata such as client ID, subscription, model, and deployment.

    Was this answer helpful?

    0 comments No comments

  4. Sanjib Kumar Saha 0 Reputation points
    2026-08-11T10:07:03.7666667+00:00

    For GPT-5 models behind Azure API Management (APIM), the recommended approach is to capture and persist the actual token usage returned by Azure OpenAI (prompt_tokens, completion_tokens, and total_tokens) rather than relying solely on the aggregated metrics generated by the llm-emit-token-metric policy. APIM token metrics are primarily intended for monitoring, analytics, and throttling scenarios, while detailed chargeback calculations typically require per-request token data.

    Was this answer helpful?


  5. Sanjib Kumar Saha 0 Reputation points
    2026-08-11T10:04:52.9133333+00:00

    If GPT-5 pricing tiers depend on prompt/context length, you should log the actual prompt token count for every request and categorize it into the applicable pricing bucket during ingestion or reporting. The token usage information is available in the Azure OpenAI response payload and can be captured through APIM policies, custom logging, Application Insights, Log Analytics, or Event Hub integration. A common enterprise pattern is:

    1. Route all Azure OpenAI traffic through APIM.
    2. Capture per-request token usage and client identifier.
    3. Store detailed logs in Log Analytics/Application Insights.
    4. Calculate chargeback externally using KQL, Power BI, or a billing pipeline based on the model-specific pricing rules.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.