-
Notifications
You must be signed in to change notification settings - Fork 202
feat: Token Usage Tracking implementation #383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AjitPadhi-Microsoft
wants to merge
10
commits into
dev
Choose a base branch
from
PSL-US-43250
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fb32a7e
Added token usage taking detils code
AjitPadhi-Microsoft fdbb2af
updated kql query
AjitPadhi-Microsoft b736781
updated token usage
AjitPadhi-Microsoft abf0938
optimized token usage
AjitPadhi-Microsoft 0447fc9
fixed lint and test issue
AjitPadhi-Microsoft 6dbbb0b
fixed copilot comments
AjitPadhi-Microsoft de6802d
comments updated
AjitPadhi-Microsoft 83a848c
fixed as per comment
AjitPadhi-Microsoft 694446d
updated token usage
AjitPadhi-Microsoft 6a5bfce
udpated doc
AjitPadhi-Microsoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| // ============================================================ | ||
| // KQL Queries for LLM Token Usage Monitoring | ||
| // Run these in Application Insights > Logs | ||
| // ============================================================ | ||
|
|
||
| // 1. Overall token usage summary (last 7 days) | ||
| customEvents | ||
| | where name == 'LLM_Token_Usage_Summary' | ||
| | where timestamp > ago(7d) | ||
| | extend input_tokens = toint(customDimensions['total_input_tokens']) | ||
| | extend output_tokens = toint(customDimensions['total_output_tokens']) | ||
| | extend total_tokens = toint(customDimensions['total_tokens']) | ||
| | summarize | ||
| TotalRequests = count(), | ||
| TotalInputTokens = sum(input_tokens), | ||
| TotalOutputTokens = sum(output_tokens), | ||
| TotalTokens = sum(total_tokens), | ||
| AvgTokensPerRequest = round(avg(total_tokens), 0); | ||
|
|
||
| // 2. Token usage by agent | ||
| customEvents | ||
| | where name == 'LLM_Agent_Token_Usage' | ||
| | where timestamp > ago(7d) | ||
| | extend agent = tostring(customDimensions['agent_name']) | ||
| | extend input_tokens = toint(customDimensions['input_tokens']) | ||
| | extend output_tokens = toint(customDimensions['output_tokens']) | ||
| | extend total_tokens = toint(customDimensions['total_tokens']) | ||
| | summarize | ||
| InputTokens = sum(input_tokens), | ||
| OutputTokens = sum(output_tokens), | ||
| TotalTokens = sum(total_tokens), | ||
| Invocations = count() | ||
| by Agent = agent | ||
| | order by TotalTokens desc; | ||
|
|
||
| // 3. Token usage over time (hourly) | ||
| customEvents | ||
| | where name == 'LLM_Token_Usage_Summary' | ||
| | where timestamp > ago(7d) | ||
| | extend input_tokens = toint(customDimensions['total_input_tokens']) | ||
| | extend output_tokens = toint(customDimensions['total_output_tokens']) | ||
| | summarize InputTokens = sum(input_tokens), OutputTokens = sum(output_tokens) by bin(timestamp, 1h) | ||
| | order by timestamp asc | ||
| | render areachart; | ||
|
|
||
| // 4. Estimated cost (GPT-4o pricing: $2.50/1M input, $10.00/1M output) | ||
| let input_price_per_million = 2.50; | ||
| let output_price_per_million = 10.00; | ||
| customEvents | ||
| | where name == 'LLM_Token_Usage_Summary' | ||
| | where timestamp > ago(30d) | ||
| | extend input_tokens = toint(customDimensions['total_input_tokens']) | ||
| | extend output_tokens = toint(customDimensions['total_output_tokens']) | ||
| | summarize TotalInput = sum(input_tokens), TotalOutput = sum(output_tokens) by bin(timestamp, 1d) | ||
| | extend InputCost = round(TotalInput * input_price_per_million / 1000000.0, 4) | ||
| | extend OutputCost = round(TotalOutput * output_price_per_million / 1000000.0, 4) | ||
| | extend TotalCost = InputCost + OutputCost | ||
| | project Day = timestamp, TotalInput, TotalOutput, InputCost, OutputCost, TotalCost | ||
| | order by Day desc; | ||
|
|
||
| // 5. Top token consumers by user | ||
| customEvents | ||
| | where name == 'LLM_Token_Usage_Summary' | ||
| | where timestamp > ago(7d) | ||
| | extend total_tokens = toint(customDimensions['total_tokens']) | ||
| | extend user_id = tostring(customDimensions['user_id']) | ||
| | summarize TotalTokens = sum(total_tokens), Requests = count() by user_id | ||
| | order by TotalTokens desc | ||
| | take 20; | ||
|
|
||
| // 6. Agent token distribution (pie chart) | ||
| customEvents | ||
| | where name == 'LLM_Agent_Token_Usage' | ||
| | where timestamp > ago(7d) | ||
| | extend agent = tostring(customDimensions['agent_name']) | ||
| | extend total_tokens = toint(customDimensions['total_tokens']) | ||
| | summarize TotalTokens = sum(total_tokens) by agent | ||
| | render piechart; | ||
|
|
||
| // 7. Token usage percentiles per task | ||
| customEvents | ||
| | where name == 'LLM_Token_Usage_Summary' | ||
| | where timestamp > ago(7d) | ||
| | extend total_tokens = toint(customDimensions['total_tokens']) | ||
| | summarize | ||
| p50 = percentile(total_tokens, 50), | ||
| p90 = percentile(total_tokens, 90), | ||
| p95 = percentile(total_tokens, 95), | ||
| p99 = percentile(total_tokens, 99), | ||
| Max = max(total_tokens); | ||
|
|
||
| // 8. Token usage by model deployment | ||
| customEvents | ||
| | where name == 'LLM_Model_Token_Usage' | ||
| | where timestamp > ago(7d) | ||
| | extend model = tostring(customDimensions['model_deployment_name']) | ||
| | extend input_tokens = toint(customDimensions['input_tokens']) | ||
| | extend output_tokens = toint(customDimensions['output_tokens']) | ||
| | extend total_tokens = toint(customDimensions['total_tokens']) | ||
| | summarize | ||
| InputTokens = sum(input_tokens), | ||
| OutputTokens = sum(output_tokens), | ||
| TotalTokens = sum(total_tokens), | ||
| Invocations = count() | ||
| by Model = model | ||
| | order by TotalTokens desc; | ||
|
|
||
| // 9. Token usage by model over time (hourly) | ||
| customEvents | ||
| | where name == 'LLM_Model_Token_Usage' | ||
| | where timestamp > ago(7d) | ||
| | extend model = tostring(customDimensions['model_deployment_name']) | ||
| | extend total_tokens = toint(customDimensions['total_tokens']) | ||
| | summarize TotalTokens = sum(total_tokens) by bin(timestamp, 1h), model | ||
| | order by timestamp asc | ||
| | render areachart; | ||
|
|
||
| // 10. Model token distribution (pie chart) | ||
| customEvents | ||
| | where name == 'LLM_Model_Token_Usage' | ||
| | where timestamp > ago(7d) | ||
| | extend model = tostring(customDimensions['model_deployment_name']) | ||
| | extend total_tokens = toint(customDimensions['total_tokens']) | ||
| | summarize TotalTokens = sum(total_tokens) by model | ||
| | render piechart; | ||
|
|
||
| // 11. Estimated cost by model (adjust pricing per model) | ||
| let gpt41_input = 2.00; | ||
| let gpt41_output = 8.00; | ||
| let gpt41_mini_input = 0.40; | ||
| let gpt41_mini_output = 1.60; | ||
| customEvents | ||
| | where name == 'LLM_Model_Token_Usage' | ||
| | where timestamp > ago(30d) | ||
| | extend model = tolower(tostring(customDimensions['model_deployment_name'])) | ||
| | extend input_tokens = toint(customDimensions['input_tokens']) | ||
| | extend output_tokens = toint(customDimensions['output_tokens']) | ||
| | summarize TotalInput = sum(input_tokens), TotalOutput = sum(output_tokens) by model | ||
| | extend InputPrice = case( | ||
| model has "4.1-mini", gpt41_mini_input, | ||
| model has "4.1", gpt41_input, | ||
| gpt41_mini_input) | ||
| | extend OutputPrice = case( | ||
| model has "4.1-mini", gpt41_mini_output, | ||
| model has "4.1", gpt41_output, | ||
| gpt41_mini_output) | ||
| | extend InputCost = round(TotalInput * InputPrice / 1000000.0, 4) | ||
| | extend OutputCost = round(TotalOutput * OutputPrice / 1000000.0, 4) | ||
| | extend TotalCost = InputCost + OutputCost | ||
| | project Model = model, TotalInput, TotalOutput, InputCost, OutputCost, TotalCost | ||
| | order by TotalCost desc; | ||
|
|
||
| // 12. Agent-to-model mapping with token usage | ||
| customEvents | ||
| | where name == 'LLM_Agent_Token_Usage' | ||
| | where timestamp > ago(7d) | ||
| | extend agent = tostring(customDimensions['agent_name']) | ||
| | extend model = tostring(customDimensions['model_deployment_name']) | ||
| | extend input_tokens = toint(customDimensions['input_tokens']) | ||
| | extend output_tokens = toint(customDimensions['output_tokens']) | ||
| | extend total_tokens = toint(customDimensions['total_tokens']) | ||
| | summarize | ||
| InputTokens = sum(input_tokens), | ||
| OutputTokens = sum(output_tokens), | ||
| TotalTokens = sum(total_tokens), | ||
| Invocations = count() | ||
| by Agent = agent, Model = model | ||
| | order by TotalTokens desc; | ||
|
|
||
| // 13. OpenTelemetry auto-instrumented OpenAI calls (if available) | ||
| dependencies | ||
| | where name has "openai" or target has "openai" | ||
| | where timestamp > ago(7d) | ||
| | extend input_tokens = tolong(customDimensions["gen_ai.usage.input_tokens"]) | ||
| | extend output_tokens = tolong(customDimensions["gen_ai.usage.output_tokens"]) | ||
| | extend model = tostring(customDimensions["gen_ai.request.model"]) | ||
| | where isnotnull(input_tokens) | ||
| | summarize | ||
| Calls = count(), | ||
| TotalInput = sum(input_tokens), | ||
| TotalOutput = sum(output_tokens) | ||
| by model | ||
| | order by TotalInput desc; | ||
|
|
||
| // 14. Total time taken per invocation (conversation) | ||
| customEvents | ||
| | where name == 'LLM_Model_Token_Usage' | ||
| | where timestamp > ago(7d) | ||
| | extend conversation_id = tostring(customDimensions['conversation_id']) | ||
| | extend model = tostring(customDimensions['model_deployment_name']) | ||
| | extend total_tokens = toint(customDimensions['total_tokens']) | ||
| | summarize | ||
| StartTime = min(timestamp), | ||
| EndTime = max(timestamp), | ||
| TotalTokens = sum(total_tokens), | ||
| Calls = count() | ||
| by conversation_id | ||
| | extend TotalDuration_sec = round(datetime_diff('second', EndTime, StartTime), 0) | ||
| | order by EndTime desc; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.