apim-kql
Skill994 starsChanged 23 months agoMITView on GitHub
Guide for creating Kusto Query Language (KQL) queries for Azure API Management tables in Azure Monitor (Log Analytics Workspace). Use when users want to query, analyze, or monitor APIM logs including gateway logs, LLM/AI logs, MCP logs, WebSocket logs, and Application Insights data. This skill provides KQL syntax, table schemas, and query patterns for APIM monitoring scenarios.
In this repository7 other files it gives its agents.
- AGENTS.mdAGENTS.md
- apim-bicepSkill
- apim-policiesSkill
- apim-terraformSkill
- lab-creatorSkill
- mcp-builderSkill
- skill-creatorSkill
The file
We split this skill into the rules it gives an agent
1 rule. About 0 are common, 0 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 296
Always filter by time: - Use where TimeGenerated > ago(...) to limit data scanned
≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
APIM KQL Skill
Guide for creating Kusto Query Language (KQL) queries for Azure API Management monitoring data in Azure Monitor.
Quick Start
Basic Gateway Logs Query
ApiManagementGatewayLogs
| where TimeGenerated > ago(1h)
| project TimeGenerated, Method, Url, ResponseCode, TotalTime, BackendTime, CallerIpAddress
| order by TimeGenerated desc
| take 100LLM Token Usage Query
ApiManagementGatewayLlmLog
| where TimeGenerated > ago(24h)
| summarize
TotalPromptTokens = sum(PromptTokens),
TotalCompletionTokens = sum(CompletionTokens),
TotalTokens = sum(TotalTokens)
by DeploymentName, ModelNameTables Overview
| Table | Description | Use Case |
ApiManagementGatewayLogs | Gateway request/response logs | API traffic analysis, errors, latency |
ApiManagementGatewayLlmLog | LLM/AI model usage logs | Token usage, model performance |
ApiManagementGatewayMCPLog | MCP server logs | MCP tool calls, sessions |
ApiManagementWebSocketConnectionLogs | WebSocket connection events | Real-time API monitoring |
AppRequests | Application Insights requests | End-to-end request tracking |
AppMetrics | Application Insights metrics | Custom metrics analysis |
AppTraces | Application Insights traces | Debug and diagnostic traces |
For complete table schemas, see references/table-schemas.md.
Essential Query Patterns
Joining LLM Logs with Gateway Logs
let llmLogs = ApiManagementGatewayLlmLog
| where DeploymentName != '';
let logsWithSubscription = llmLogs
| join kind=leftouter ApiManagementGatewayLogs on CorrelationId
| project
SubscriptionId = ApimSubscriptionId,
DeploymentName,
ModelName,
PromptTokens,
CompletionTokens,
TotalTokens;
logsWithSubscription
| summarize
SumPromptTokens = sum(PromptTokens),
SumCompletionTokens = sum(CompletionTokens),
SumTotalTokens = sum(TotalTokens)
by SubscriptionId, DeploymentNameError Analysis
ApiManagementGatewayLogs
| where TimeGenerated > ago(24h)
| where ResponseCode >= 400
| summarize ErrorCount = count() by ResponseCode, ApiId, LastErrorReason
| order by ErrorCount descRequest Latency Analysis
ApiManagementGatewayLogs
| where TimeGenerated > ago(1h)
| summarize
AvgTotalTime = avg(TotalTime),
AvgBackendTime = avg(BackendTime),
P95TotalTime = percentile(TotalTime, 95),
RequestCount = count()
by bin(TimeGenerated, 5m), ApiId
| order by TimeGenerated descBackend Performance
ApiManagementGatewayLogs
| where TimeGenerated > ago(1h)
| where BackendId != ""
| summarize
AvgBackendTime = avg(BackendTime),
MaxBackendTime = max(BackendTime),
ErrorCount = countif(BackendResponseCode >= 400),
TotalRequests = count()
by BackendId
| order by AvgBackendTime descToken Usage by Time
ApiManagementGatewayLlmLog
| where TimeGenerated > ago(7d)
| summarize
TotalTokens = sum(TotalTokens),
PromptTokens = sum(PromptTokens),
CompletionTokens = sum(CompletionTokens)
by bin(TimeGenerated, 1h), DeploymentName
| order by TimeGenerated descMCP Tool Usage
ApiManagementGatewayMCPLog
| where TimeGenerated > ago(24h)
| summarize
CallCount = count(),
UniqueClients = dcount(ClientName)
by ToolName, ServerName, Method
| order by CallCount descWebSocket Connection Events
ApiManagementWebSocketConnectionLogs
| where TimeGenerated > ago(1h)
| summarize
EventCount = count()
by EventName, Source, Destination
| order by EventCount descKQL Operators Reference
Filtering
| where TimeGenerated > ago(1h)
| where ResponseCode == 200
| where ApiId contains "openai"
| where isnotempty(BackendId)Aggregation
| summarize count() by ApiId
| summarize avg(TotalTime), percentile(TotalTime, 95) by ApiId
| summarize sum(TotalTokens) by DeploymentName
| summarize dcount(CallerIpAddress) by bin(TimeGenerated, 1h)Time Functions
| where TimeGenerated > ago(1h)
| where TimeGenerated between (datetime(2024-01-01) .. datetime(2024-01-31))
| where TimeGenerated >= startofmonth(now()) and TimeGenerated <= endofmonth(now())
| summarize ... by bin(TimeGenerated, 5m)Joins
| join kind=leftouter OtherTable on CorrelationId
| join kind=inner OtherTable on $left.Field1 == $right.Field2Projections
| project TimeGenerated, Method, Url, ResponseCode
| project-away _BilledSize, _IsBillable
| extend Duration = TotalTime - BackendTime
| extend IsError = ResponseCode >= 400Ordering and Limiting
| order by TimeGenerated desc
| take 100
| top 10 by TotalTime descCommon Scenarios
Daily Request Summary
ApiManagementGatewayLogs
| where TimeGenerated > ago(7d)
| summarize
TotalRequests = count(),
SuccessfulRequests = countif(ResponseCode < 400),
FailedRequests = countif(ResponseCode >= 400),
AvgLatency = avg(TotalTime)
by bin(TimeGenerated, 1d), ApiId
| extend SuccessRate = round(100.0 * SuccessfulRequests / TotalRequests, 2)
| order by TimeGenerated descTop Consumers by Token Usage
let llmLogs = ApiManagementGatewayLlmLog
| where TimeGenerated > ago(30d);
llmLogs
| join kind=leftouter ApiManagementGatewayLogs on CorrelationId
| summarize
TotalTokens = sum(TotalTokens),
RequestCount = count()
by ApimSubscriptionId
| top 10 by TotalTokens descError Rate Over Time
ApiManagementGatewayLogs
| where TimeGenerated > ago(24h)
| summarize
Total = count(),
Errors = countif(ResponseCode >= 400)
by bin(TimeGenerated, 1h)
| extend ErrorRate = round(100.0 * Errors / Total, 2)
| project TimeGenerated, Total, Errors, ErrorRate
| order by TimeGenerated descCache Hit Analysis
ApiManagementGatewayLogs
| where TimeGenerated > ago(24h)
| where Cache != ""
| summarize
HitCount = countif(Cache == "hit"),
MissCount = countif(Cache == "miss")
by ApiId
| extend HitRate = round(100.0 * HitCount / (HitCount + MissCount), 2)Streaming vs Non-Streaming LLM Requests
ApiManagementGatewayLlmLog
| where TimeGenerated > ago(24h)
| summarize
StreamingRequests = countif(IsStreamCompletion == true),
NonStreamingRequests = countif(IsStreamCompletion == false),
AvgTokens = avg(TotalTokens)
by DeploymentNameRunning KQL Queries
Azure CLI
az monitor log-analytics query \
-w <workspace-id> \
--analytics-query "ApiManagementGatewayLogs | take 10"Python
query = """
ApiManagementGatewayLogs
| where TimeGenerated > ago(1h)
| take 100
"""
result = utils.run(f'az monitor log-analytics query -w {workspace_id} --analytics-query "{query}"')Best Practices
- Always filter by time - Use
where TimeGenerated > ago(...)to limit data scanned - Use summarize early - Aggregate data before joins when possible
- Project only needed columns - Reduce data transfer with explicit projections
- Use let statements - Break complex queries into readable parts
- Join on CorrelationId - Link APIM tables together using this common key
- Use bin() for time series - Group time data into meaningful intervals
References
- references/table-schemas.md - Complete table column definitions
- references/query-examples.md - Additional query examples from this repository
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

