get_cost_breakdown
Analyze AWS costs by service or usage type to identify spending patterns and optimize cloud expenses.
Instructions
Detailed cost analysis. If service_name is provided, breaks down that service by Usage Type. Otherwise, breaks down by Service.
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "get_cost_breakdown"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| start_date | No | Start date in YYYY-MM-DD format (default: 14 days ago). | |
| end_date | No | End date in YYYY-MM-DD format. | |
| service_name | No | Optional: Specific service to analyze (e.g., 'Amazon Elastic Compute Cloud - Compute'). |
Implementation Reference
- src/index.ts:1007-1041 (handler)Executes the get_cost_breakdown tool. Fetches AWS cost data using CostExplorerClient's GetCostAndUsageCommand. Groups by SERVICE (default) or USAGE_TYPE if service_name provided. Returns daily breakdown sorted by cost descending, top 100 items.
if (name === "get_cost_breakdown") { const endDate = (args as any)?.end_date || new Date().toISOString().split('T')[0]; const startDate = (args as any)?.start_date || new Date(Date.now() - 14 * 24 * 60 * 60 * 1000).toISOString().split('T')[0]; const serviceName = (args as any)?.service_name; const groupByKey = serviceName ? "USAGE_TYPE" : "SERVICE"; const filter = serviceName ? { Dimensions: { Key: "SERVICE", Values: [serviceName] } } as any : undefined; const command = new GetCostAndUsageCommand({ TimePeriod: { Start: startDate, End: endDate }, Granularity: "DAILY", Metrics: ["UnblendedCost"], GroupBy: [{ Type: "DIMENSION", Key: groupByKey }], Filter: filter }); const response = await costExplorerClient.send(command); const costs = response.ResultsByTime?.flatMap(r => r.Groups?.map(g => ({ Date: r.TimePeriod?.Start, [groupByKey === "USAGE_TYPE" ? "UsageType" : "Service"]: g.Keys?.[0], Cost: parseFloat(g.Metrics?.UnblendedCost?.Amount || "0").toFixed(4), Unit: g.Metrics?.UnblendedCost?.Unit })) ) .filter(c => c && parseFloat(c.Cost) > 0) // Filter out zero costs .sort((a, b) => parseFloat(b?.Cost || "0") - parseFloat(a?.Cost || "0")) || []; return { content: [{ type: "text", text: JSON.stringify(costs.slice(0, 100), null, 2) }] }; } - src/index.ts:209-218 (schema)Input schema definition for the get_cost_breakdown tool, defining parameters for date range and optional service_name.
name: "get_cost_breakdown", description: "Detailed cost analysis. If service_name is provided, breaks down that service by Usage Type. Otherwise, breaks down by Service.", inputSchema: { type: "object", properties: { start_date: { type: "string", description: "Start date in YYYY-MM-DD format (default: 14 days ago)." }, end_date: { type: "string", description: "End date in YYYY-MM-DD format." }, service_name: { type: "string", description: "Optional: Specific service to analyze (e.g., 'Amazon Elastic Compute Cloud - Compute')." } } } - src/index.ts:208-219 (registration)Registers the get_cost_breakdown tool in the ListTools response with name, description, and input schema.
{ name: "get_cost_breakdown", description: "Detailed cost analysis. If service_name is provided, breaks down that service by Usage Type. Otherwise, breaks down by Service.", inputSchema: { type: "object", properties: { start_date: { type: "string", description: "Start date in YYYY-MM-DD format (default: 14 days ago)." }, end_date: { type: "string", description: "End date in YYYY-MM-DD format." }, service_name: { type: "string", description: "Optional: Specific service to analyze (e.g., 'Amazon Elastic Compute Cloud - Compute')." } } } }, - src/index.ts:58-58 (helper)Initializes the AWS CostExplorerClient used by the get_cost_breakdown handler.
const costExplorerClient = new CostExplorerClient({});