Skip to content

feat: Add LLM token usage tracking to Application Insights#586

Open
Prachig-Microsoft wants to merge 27 commits into
devfrom
feature/token-usage-tracking
Open

feat: Add LLM token usage tracking to Application Insights#586
Prachig-Microsoft wants to merge 27 commits into
devfrom
feature/token-usage-tracking

Conversation

@Prachig-Microsoft

Copy link
Copy Markdown
Contributor

Implement comprehensive token usage tracking across all LLM call sites in ContentProcessor and ContentProcessorWorkflow, following the MACAE psl-token-usage branch pattern.

Changes:

  • Add token_usage_utils.py with extract/emit helpers for both projects
  • Instrument MapHandler (ContentProcessor) with per-call token events
  • Instrument RAI, Summarize, GapAnalysis executors (Workflow)
  • Add summary and per-model token events in save_handler
  • Add 18 KQL queries for Azure Workbook visualization
  • Add unit tests (18 tests) for token_usage_utils

Events emitted to Application Insights:

  • LLM_Agent_Token_Usage: per agent/step with model, process_id
  • LLM_Model_Token_Usage: per model deployment aggregation
  • LLM_Token_Usage_Summary: per document totals

Does this introduce a breaking change?

  • Yes
  • No

Golden Path Validation

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

Deployment Validation

  • [ x] 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 12, 2026

Copy link
Copy Markdown

Coverage

Coverage Report •
FileStmtsMissCoverMissing
TOTAL121716186% 
report-only-changed-files is enabled. No files were changed during this commit :)

Tests Skipped Failures Errors Time
244 0 💤 0 ❌ 0 🔥 4.955s ⏱️

@Prachig-Microsoft Prachig-Microsoft marked this pull request as ready for review May 25, 2026 10:13
Copilot AI review requested due to automatic review settings May 25, 2026 10:13
Prachig-Microsoft and others added 3 commits June 2, 2026 15:58
- Pass file metadata in TokenUsageScope for MapHandler, Summarize,
  GapAnalysis, and RAI executors so the workbook file-type query works
- Fix dedup in model cost KQL query (workbook + queries file)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add SHA-256 based event_id (event_name|process_id|agent_name|model)
to each emitted event, enabling KQL dedup without accelerator-specific
max() patterns. This makes the workbook generic across accelerators.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Change emit_all() logging from INFO to DEBUG to reduce production log volume
- Optional token-detail fields default to None when absent instead of 0
- Remove redundant extract_usage_from_stream_chunk() fallback in TokenUsageScope.add()
- Pin azure-monitor-events-extension to ==0.1.0 in both projects
- Revert enableMonitoring to false in default parameters

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ntProcessor)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 2, 2026 10:33

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 15 out of 17 changed files in this pull request and generated 3 comments.

Comment thread src/ContentProcessorWorkflow/src/steps/summarize/executor/summarize_executor.py Outdated
Comment thread src/ContentProcessorWorkflow/src/steps/rai/executor/rai_executor.py Outdated
Comment thread src/ContentProcessorWorkflow/src/steps/gap_analysis/executor/gap_executor.py Outdated
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 11, 2026 09:29
The devcontainers/python image on MCR is returning 403 Forbidden.
Use the official Python slim image instead, which is lighter and
appropriate for production containers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

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 16 out of 18 changed files in this pull request and generated 5 comments.

Comment thread src/ContentProcessorWorkflow/src/steps/summarize/executor/summarize_executor.py Outdated
Comment thread src/ContentProcessorWorkflow/src/steps/rai/executor/rai_executor.py Outdated
Comment thread src/ContentProcessorWorkflow/src/steps/gap_analysis/executor/gap_executor.py Outdated
Comment thread infra/main.json
Comment thread src/ContentProcessor/src/libs/pipeline/handlers/map_handler.py
…c file_types

- Pass agent_name and model_deployment_name to emit_summary for unique event_id
- Sort file_types set before joining for deterministic telemetry dimensions

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 11, 2026 09:43

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 16 out of 18 changed files in this pull request and generated 4 comments.

Comment thread infra/main.json
Comment thread infra/main.json
Comment thread src/ContentProcessor/src/libs/llm_token_telemetry.py
Comment thread src/ContentProcessorWorkflow/src/libs/llm_token_telemetry.py
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