Subchapter 13.16
cost-optimization/workflow.mdMarkdown8 KBView on GitHub
Use this workflow when the user wants to reduce their costs — find waste, orphaned resources, rightsizing opportunities.
Important: Always present the total bill and cost breakdown (from the Cost Query Workflow) alongside optimization recommendations.
Required Tools:
az login)costmanagement, resource-graphRequired Permissions:
Verification commands:
az --version
az account show
az extension show --name costmanagement
azqr versionazure__get_azure_bestpractices({
intent: "Get cost optimization best practices",
command: "get_bestpractices",
parameters: { resource: "cost-optimization", action: "all" }
})If the user specifically requests Redis cost optimization, use the specialized Redis reference:
Reference: Azure Redis Cost Optimization
When to use:
💡 Note: For general subscription-wide optimization, continue with Step 2. For Redis-only analysis, follow the Redis reference document.
If performing Redis cost optimization, ask the user to select:
Wait for user response before proceeding.
If the user requests Storage cost optimization, load: Azure Storage Cost Optimization
Triggers: “storage account cost”, “blob storage savings”, “LRS/GRS/ZRS downgrade”, “storage lifecycle savings”, “reduce storage spending”.
For Storage-only requests, follow the Storage reference. For general optimization that includes storage, continue to Step 2.
If the user specifically requests AKS cost optimization, use the specialized AKS reference files:
When to use AKS-specific analysis:
Tool Selection:
azure__aks for AKS operations (list clusters, get node pools, inspect configuration) — it provides richer metadata and is consistent with AKS skill conventions in this repoaz aks and kubectl only when the specific operation cannot be performed via the MCP surfaceReference files (load only what is needed for the request):
Note: For general subscription-wide cost optimization (including AKS resource groups), continue with Step 2. For AKS-focused analysis, follow the instructions in the relevant reference file above.
If performing AKS cost optimization, ask the user to select their analysis scope:
Prompt the user with these options:
Wait for user response before proceeding to Step 2.
Run azqr to find orphaned resources (immediate cost savings):
Reference: Azure Quick Review
azure__extension_azqr
subscription: "<SUBSCRIPTION_ID>"
resource-group: "<RESOURCE_GROUP>" # optionalWhat to look for:
Use Azure Resource Graph for efficient cross-subscription resource discovery. See Azure Resource Graph Queries for orphaned resource detection patterns.
az account show
az resource list --subscription "<SUBSCRIPTION_ID>" --resource-group "<RESOURCE_GROUP>"Get actual cost data from Azure Cost Management API (last 30 days). Use the Cost Query Workflow with this configuration:
Create temp/cost-query.json:
{
"type": "ActualCost",
"timeframe": "Custom",
"timePeriod": {
"from": "<START_DATE>",
"to": "<END_DATE>"
},
"dataset": {
"granularity": "None",
"aggregation": {
"totalCost": {
"name": "Cost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceId"
}
]
}
}Action Required: Calculate
<START_DATE>(30 days ago) and<END_DATE>(today) in ISO 8601 format.
Execute and save results to output/cost-query-result<timestamp>.json. Always include the --headers "ClientType=GitHubCopilotForAzure" header in all Cost Management API requests.
💡 Tip: Also run a cost-by-service query (grouping by
ServiceName) to present the total bill breakdown alongside optimization recommendations. See examples.md.
⚠️ Warning: Sequential queries to the same scope share the per-scope rate limit (4 requests/minute). If a 429 response is received, check all
x-ms-ratelimit-microsoft.costmanagement-*-retry-afterheaders and do not send further requests until the longest retry-after duration has elapsed.
Fetch current pricing from official Azure pricing pages using fetch_webpage:
Key services to validate:
Important: Check for free tier allowances — many Azure services have generous free limits.
Query Azure Monitor for utilization data (last 14 days) to support rightsizing recommendations:
$startTime = (Get-Date).AddDays(-14).ToString("yyyy-MM-ddTHH:mm:ssZ")
$endTime = Get-Date -Format "yyyy-MM-ddTHH:mm:ssZ"
# VM CPU utilization
az monitor metrics list `
--resource "<RESOURCE_ID>" `
--metric "Percentage CPU" `
--interval PT1H `
--aggregation Average `
--start-time $startTime `
--end-time $endTimeGenerate a report to output/costoptimizereport<YYYYMMDD_HHMMSS>.md that includes an executive summary, cost breakdown by service, free tier analysis, orphaned resources, prioritized optimization recommendations, and implementation commands. Save cost query results to output/cost-query-result<YYYYMMDD_HHMMSS>.json for audit trail, then clean up temporary files.
For the complete report template, see report-template.md.