Claude Code for Azure Cost Management Workflow
Managing Azure costs effectively requires consistent monitoring, automated alerts, and actionable insights. By combining Claude Code with Azure’s Cost Management APIs, you can create powerful workflows that automate cost tracking and optimization tasks. This guide shows you how to build Claude skills specifically designed for Azure cost management.
Understanding Azure Cost Management APIs
Before building Claude skills, you need to understand how Azure exposes cost data. The Azure Cost Management API provides programmatic access to:
- Cost and usage data: Daily, monthly, or hourly cost breakdowns
- Budget alerts: Threshold-based notifications
- Resource costs: Granular per-resource cost analysis
- Forecasts: Predicted spending based on historical data
To authenticate, you’ll need an Azure service principal with appropriate permissions:
# Authenticate using Azure CLI
az login --service-principal -u $APP_ID -p $PASSWORD --tenant $TENANT_ID
# Get access token
TOKEN=$(az account get-access-token --query accessToken -o tsv)
Building a Basic Azure Cost Query Skill
The foundation of any cost management workflow is querying cost data. Here’s a skill that retrieves cost summaries for a specified time period:
---
name: azure-cost-summary
description: "Get Azure cost summary for a specified time period"
---
## Cost Summary Query
This skill retrieves cost data from Azure Cost Management API.
### Input Parameters
- **subscription_id**: Azure subscription ID
- **start_date**: Start date (YYYY-MM-DD format)
- **end_date**: End date (YYYY-MM-DD format)
### Query Execution
Call the Azure Cost Management API with the following endpoint:
```bash
curl -s -X GET \
"https://management.azure.com/subscriptions/{{ subscription_id }}/providers/Microsoft.CostManagement/query?api-version=2023-03-01" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"type": "ActualCost",
"timeframe": "Custom",
"timePeriod": {
"from": "{{ start_date }}",
"to": "{{ end_date }}"
},
"dataset": {
"granularity": "Daily",
"aggregation": {
"totalCost": {
"name": "Cost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceGroup"
}
]
}
}'
This returns daily cost breakdowns grouped by resource group, giving you visibility into where your spending occurs.
## Creating a Budget Monitoring Skill
Proactive cost management requires budget alerts. This skill creates and manages budget alerts:
```yaml
---
name: azure-budget-alert
description: "Create or check Azure budget alerts"
---
## Budget Alert Management
### Creating a Budget
To set up a budget alert for a resource group:
```bash
# Create budget with 80% and 100% thresholds
az consumption budget create \
--amount 1000 \
--budget-name "monthly-development" \
--category "Cost" \
--end-date "2026-03-31" \
--resource-group "dev-rg" \
--threshold "80" "100" \
--notifications '{"ActualCostGreaterThan80Percent": {"enabled": true, "operator": "GreaterThan", "threshold": 80, "contactEmails": ["team@example.com"]}}'
Querying Budget Status
Check current budget status using the API:
curl -s -X GET \
"https://management.azure.com/subscriptions/$SUB_ID/providers/Microsoft.Consumption/budgets?api-version=2023-05-01" \
-H "Authorization: Bearer $TOKEN"
This returns all budgets with their current spending versus limits.
## Implementing Cost Anomaly Detection
Unexpected cost spikes can indicate issues like misconfigured resources or security breaches. This skill analyzes cost patterns:
```yaml
---
name: azure-cost-anomaly
description: "Detect Azure cost anomalies by comparing current vs historical spending"
---
## Cost Anomaly Detection
This skill compares current spending against historical averages to identify unusual patterns.
### Historical Analysis Approach
```bash
# Get last 30 days cost data
START_DATE=$(date -v-30d +%Y-%m-%d)
END_DATE=$(date +%Y-%m-%d)
# Query API and extract daily costs
DAILY_COSTS=$(curl -s -X POST \
"https://management.azure.com/subscriptions/$SUB_ID/providers/Microsoft.CostManagement/query?api-version=2023-03-01" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d @- << EOF
{
"type": "ActualCost",
"timeframe": "Custom",
"timePeriod": { "from": "$START_DATE", "to": "$END_DATE" },
"dataset": {
"granularity": "Daily",
"aggregation": { "totalCost": { "name": "Cost", "function": "Sum" } }
}
}
EOF
)
# Calculate average and flag anomalies (e.g., > 2x average)
echo "$DAILY_COSTS" | jq '[.properties.rows[][0]] | map(select(. > (add / length) * 2))'
This approach helps identify days where spending exceeded twice the average, requiring investigation.
## Automating Cost Optimization Recommendations
Azure provides recommendations through the Azure Advisor API. This skill fetches and displays cost-saving recommendations:
```yaml
---
name: azure-cost-recommendations
description: "Fetch Azure Advisor cost optimization recommendations"
---
## Cost Optimization Recommendations
### Retrieving Recommendations
```bash
# Get cost-related recommendations from Azure Advisor
curl -s -X GET \
"https://management.azure.com/subscriptions/$SUB_ID/providers/Microsoft.Advisor/recommendations?api-version=2023-01-01" \
-H "Authorization: Bearer $TOKEN" | jq '.[] | select(.category == "Cost") | {name: .properties.shortDescription.solution, impact: .properties.impact, savings: .properties.extendedProperties.annualSavingsAmount}'
Common Cost Optimization Actions
- Right-size virtual machines: Identify underutilized VMs
- Remove unused resources: Find unattached disks and unused IPs
- Use reserved instances: Calculate potential savings
- Enable auto-shutdown: Configure DevTest VMs to shut down outside hours
Integrating with Azure Monitor
For real-time cost tracking, integrate with Azure Monitor:
---
name: azure-cost-dashboard
description: "Set up Azure Monitor cost tracking with custom metrics"
tools:
- Bash
- Read
---
## Azure Monitor Integration
### Publishing Cost Metrics
Send cost data to Azure Monitor for custom dashboards:
```bash
# Send custom metric to Azure Monitor
az monitor metrics create \
--resource-group "cost-mgmt-rg" \
--namespace "CustomCosts" \
--name "DailyCost" \
--dimension "ResourceGroup" "ServiceName" \
--aggregation "Sum"
This enables real-time dashboards and alerts beyond what Azure Cost Management provides natively. ```
Best Practices for Azure Cost Management Skills
When building Claude skills for Azure cost management, follow these practices:
- Use service principals with minimal permissions: Create dedicated SPs with only Cost Management reader roles
- Cache authentication tokens: Avoid repeated authentication calls within a session
- Implement retry logic: Handle rate limiting with exponential backoff
- Structure queries efficiently: Use granularity appropriate for your needs—daily for analysis, monthly for reporting
- Combine multiple data sources: Merge Cost Management data with Azure Advisor and Monitor for comprehensive insights
Conclusion
Claude Code skills for Azure cost management transform manual monitoring into automated, actionable workflows. By using Azure’s APIs directly through Claude skills, you can build solutions that alert on budget overruns, detect anomalies, surface optimization recommendations, and integrate with broader monitoring infrastructure.
Start with the basic cost query skill, then progressively add budget alerts, anomaly detection, and optimization recommendations as your cost management mature.
Related Reading
- Claude Code for Beginners: Complete Getting Started Guide
- Best Claude Skills for Developers in 2026
- Claude Skills Guides Hub
Built by theluckystrike — More at zovo.one