Skip to content

feat: Token Usage Tracking implementation#383

Open
AjitPadhi-Microsoft wants to merge 10 commits into
devfrom
PSL-US-43250
Open

feat: Token Usage Tracking implementation#383
AjitPadhi-Microsoft wants to merge 10 commits into
devfrom
PSL-US-43250

Conversation

@AjitPadhi-Microsoft

Copy link
Copy Markdown
Contributor

Purpose

  • This pull request adds comprehensive LLM token usage telemetry and monitoring to the codebase, enabling detailed tracking of token consumption at the agent, model, and user levels. It introduces robust extraction and aggregation of token usage from API responses and streaming updates, emits standardized telemetry events, and provides a suite of KQL queries for monitoring and cost analysis in Application Insights.

LLM Token Usage Telemetry and Monitoring

Token usage extraction and telemetry emission:

  • Introduced helper functions (_extract_usage_from_dict, _extract_usage_from_update, _extract_usage_from_response, _track_token_usage) in chat.py to extract input, output, and total token counts from various response formats and emit three types of telemetry events: LLM_Token_Usage_Summary, LLM_Agent_Token_Usage, and LLM_Model_Token_Usage. These events include relevant metadata such as agent name, model deployment, user ID, and conversation ID.
  • Added similar _track_title_token_usage helpers to both history.py and history_sql.py to emit token usage telemetry for title generation requests. [1] [2]

Integration into API flows:

  • Updated stream_openai_text and stream_openai_text_workshop in chat.py to accumulate token usage across all response iterations/streaming updates and emit telemetry at the end of each request. [1] [2] [3] [4] [5] [6] [7]
  • Integrated token usage telemetry emission into the title generation flow in both Cosmos and SQL history modules.

Monitoring and Analytics Support

Application Insights KQL queries:

  • Added a new token-usage-queries.kql file with a comprehensive set of KQL queries for monitoring LLM token usage, top consumers, cost estimation, usage breakdowns by agent/model/user, and visualizations (area and pie charts). These queries support detailed analysis and reporting in Application Insights.

Does this introduce a breaking change?

  • Yes
  • No

Golden Path Validation

  • I have tested the primary workflows (the "golden path") to ensure they function correctly without errors.

Deployment Validation

  • I have validated the deployment process successfully and all services are running as expected with this change.

What to Check

Verify that the following are valid

  • ...

Other Information

@github-actions

github-actions Bot commented May 13, 2026

Copy link
Copy Markdown

@github-actions

github-actions Bot commented May 13, 2026

Copy link
Copy Markdown

Unit Test Results

520 tests   520 ✅  8s ⏱️
  1 suites    0 💤
  1 files      0 ❌

Results for commit 6a5bfce.

♻️ This comment has been updated with latest results.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds LLM token usage telemetry for chat and title-generation flows, plus KQL snippets for monitoring usage and cost in Application Insights.

Changes:

  • Added token usage extraction and telemetry emission helpers in chat/history modules.
  • Integrated token accumulation into standard and workshop chat streams.
  • Added Application Insights KQL queries for usage summaries, breakdowns, and cost estimates.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.

File Description
src/api/python/chat.py Adds chat token extraction, aggregation, and telemetry emission.
src/api/python/history.py Adds Cosmos history title-generation token telemetry.
src/api/python/history_sql.py Adds SQL history title-generation token telemetry.
infra/dashboards/token-usage-queries.kql Adds monitoring and analytics queries for emitted token events.
Comments suppressed due to low confidence (1)

src/api/python/chat.py:177

  • Telemetry extraction can raise before the chat response is streamed when usage is present but its token fields are missing or non-numeric (for example, the existing tests' mock Responses objects do not define usage fields). Because this helper is called directly from the request path, a telemetry-shape issue turns an otherwise successful chat response into a 500 instead of simply skipping token telemetry; validate/coerce defensively or catch extraction errors at the call sites.
    inp = getattr(usage_obj, "input_tokens", 0) or getattr(usage_obj, "prompt_tokens", 0) or 0
    out = getattr(usage_obj, "output_tokens", 0) or getattr(usage_obj, "completion_tokens", 0) or 0
    tot = getattr(usage_obj, "total_tokens", 0) or (inp + out)
    if tot > 0:
        return (int(inp), int(out), int(tot))

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/api/python/chat.py Outdated
Comment thread src/api/python/chat.py Outdated
Comment thread src/api/python/history.py Outdated
Comment thread src/api/python/history_sql.py Outdated
Comment thread infra/dashboards/token-usage-queries.kql Outdated
Comment thread src/api/python/history.py Outdated
Comment thread src/api/python/history_sql.py Outdated
Copilot AI review requested due to automatic review settings May 13, 2026 11:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Comment thread src/api/python/chat.py
Comment thread src/api/python/history.py
Comment thread src/api/python/history_sql.py
Comment thread src/api/python/token_usage.py Outdated
Comment thread src/api/python/token_usage.py
Copilot AI review requested due to automatic review settings May 13, 2026 12:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Comment thread src/api/python/token_usage.py Outdated
Comment thread src/api/python/history_sql.py Outdated
Copilot AI review requested due to automatic review settings May 13, 2026 12:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Comment thread src/api/python/token_usage.py Outdated
"user_id": user_id or "",
"conversation_id": conversation_id or "",
})
logger.info(
Copilot AI review requested due to automatic review settings May 19, 2026 12:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

"""Unit tests for token_usage helpers."""

from types import SimpleNamespace
from unittest.mock import MagicMock, patch
Comment on lines +222 to +223
"agent_count": "1",
"model_count": "1",
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants