Cloud costs are the silent killer of startup runway. You spin up an EC2 instance for a side project, forget about it, and three months later you have burned ₹30,000 on an idle server. You attach an EBS volume to a testing container, delete the container, and the volume stays behind at ₹0.10/GB-month forever. Multiply this across a team of 5 developers, and you are looking at thousands of dollars of annual waste.
The problem is not that engineers are careless — it is that cloud cost optimization is boring, manual, and time-consuming. Nobody wants to spend Friday afternoon cross-referencing CloudWatch metrics with Cost Explorer reports. So the waste accumulates.
When building SubTrackHub, I asked a different question: what if an AI could do this analysis for you? What if it could read your cloud metrics, identify waste, and generate safe, context-aware recommendations — without you ever opening the AWS Console?
Here is exactly how I built that pipeline.
The Problem with Manual Cloud Cost Optimization
Traditional cloud cost tools give you dashboards. AWS Cost Explorer shows you a line chart of spending over time. Third-party tools like CloudHealth or Spot.io give you recommendations. But they all share the same limitation: they require a human to interpret and act on the data.
The typical flow:
- Open AWS Cost Explorer
- See that EC2 costs are up 20%
- Dig into which instances are driving the increase
- Cross-reference with CloudWatch CPU metrics
- Decide which instances can be downsized
- Manually resize or stop each instance
- Repeat monthly
This process takes 2–4 hours per month for a small team. And it requires domain expertise: a junior developer might not know that a t3.medium running at 2% CPU for 14 days can safely be downsized to a t3.micro.
The Architecture: LLM-Powered Cloud Analysis
SubTrackHub automates this entire process. Here is the pipeline:
┌─────────────────┐ AWS SDK ┌───────────────────┐
│ AWS Account ├─────────────────────>│ Ingestion Worker │
│ (EC2, EBS, │ (Daily Cron) │ (Node.js) │
│ CloudWatch) │ └─────────┬─────────┘
└─────────────────┘ │
Stores │ Raw metrics
▼
┌───────────────────┐
│ MongoDB Hub │
│ (Resource State) │
└─────────┬─────────┘
│
Aggregates │ Health indices
▼
┌─────────────────┐ Structured ┌───────────────────┐
│ OpenAI API │<─────── Prompt ──────┤ Analytics Engine │
│ (GPT-4o) │─────── Response ────>│ (Safety Guard) │
└─────────────────┘ JSON + Actions └─────────┬─────────┘
│
Renders │ Recommendations
▼
[ Developer Dashboard ]
Step 1: Ingestion — Collecting AWS Telemetry
The ingestion worker runs daily via AWS EventBridge (cron). It queries three AWS APIs:
- EC2: List all instances, get configuration (instance type, state, launch date)
- CloudWatch: Fetch 14-day CPU utilization, network I/O, and disk read/write metrics
- EBS: List all volumes, check attachment status, identify detached volumes
import { EC2Client, DescribeInstancesCommand } from "@aws-sdk/client-ec2";
import { CloudWatchClient, GetMetricStatisticsCommand } from "@aws-sdk/client-cloudwatch";
const ec2Client = new EC2Client({ region: "us-east-1" });
const cwClient = new CloudWatchClient({ region: "us-east-1" });
async function fetchInstanceMetrics(instanceId: string) {
const startTime = new Date();
startTime.setDate(startTime.getDate() - 14);
const command = new GetMetricStatisticsCommand({
Namespace: "AWS/EC2",
MetricName: "CPUUtilization",
Dimensions: [{ Name: "InstanceId", Value: instanceId }],
StartTime: startTime,
EndTime: new Date(),
Period: 86400, // 1-day intervals
Statistics: ["Average"],
});
const response = await cwClient.send(command);
const datapoints = response.Datapoints || [];
if (datapoints.length === 0) return 0;
return datapoints.reduce((sum, dp) => sum + (dp.Average || 0), 0) / datapoints.length;
}
Step 2: Health Index Computation
Raw metrics are noisy. An EC2 instance might show 80% CPU utilization during a weekly batch job but 0% the rest of the week. SubTrackHub computes a composite health index (0–100) for each resource:
Health Index = (CPU Score × 0.4) + (Memory Score × 0.3) + (Network Score × 0.2) + (Cost Efficiency × 0.1)
Resources scoring below 20 are flagged as "idle." Resources scoring 20–40 are "underutilized." This gives the LLM a compressed, actionable view of infrastructure state.
Step 3: LLM Analysis with Safety Constraints
This is where the magic happens. The analytics engine constructs a structured prompt for GPT-4o that includes:
- The compressed resource state (resource IDs, types, costs, health indices)
- Strict safety constraints (never recommend stopping production databases)
- A JSON schema for the response (ensuring parseable output)
const safetyPrompt = `You are a Senior Cloud FinOps Engineer. Analyze these AWS resources:
${JSON.stringify(compressedResources, null, 2)}
CONSTRAINTS — YOU MUST FOLLOW THESE:
1. NEVER recommend deleting or stopping resources marked 'production' unless CPU average is 0% for 14+ consecutive days.
2. For 'development' or 'staging' environments, recommend stopping if CPU is under 5%.
3. For detached EBS volumes, recommend taking a snapshot BEFORE deletion.
4. For over-provisioned instances, recommend specific downsize targets (e.g., t3.medium → t3.micro).
5. Always estimate monthly savings in USD.
6. Output ONLY valid JSON matching the schema.`;
The safety constraints are critical. An LLM without guardrails might recommend shutting down a database server because it has low CPU utilization — the database is idle because it is waiting for queries, not because it is unused.
Step 4: Dashboard and Action
The recommendations render on a developer dashboard with:
- Per-resource savings estimates
- One-click execution (stop, downsize, snapshot-and-delete)
- Audit log of all actions taken
- Historical savings tracking
Real Results: 35% Cost Reduction
Running this pipeline on a pilot staging environment with 14 virtual machines, 6 idle databases, and 24 detached EBS volumes:
| Category | Waste Found | Monthly Savings |
|---|---|---|
| Idle staging servers (running 24/7, used 9–5) | 8 instances | $180 |
| Detached EBS volumes from deleted containers | 12 volumes | $45 |
| Over-provisioned instances (t3.medium → t3.micro) | 4 instances | $96 |
| Unattached elastic IPs | 3 IPs | $9 |
| Total | 27 resources | $330/month |
Total monthly cost dropped from $940 → $610 — a 35% reduction with zero impact on developer workflows. The staging environment still works exactly the same; it just does not burn money at night when nobody is using it.
Why LLMs, Not Rules
You might ask: why not just write if CPU < 5% for 14 days, recommend stop? That would cover the simple cases. But cloud cost optimization is full of edge cases that rules cannot handle:
-
Context matters. An EC2 instance running at 3% CPU might be a critical production server that handles occasional spikes. The LLM can infer this from the resource name, tags, and environment metadata.
-
Compound recommendations. "Stop this server at night" + "downsize it during the day" + "migrate its workload to Lambda" is a strategy, not a single rule. LLMs can compose multi-step plans.
-
Natural language explanations. Instead of "CPU < 5%," the dashboard shows: "This staging server runs at 2% CPU on average and has been idle outside business hours for 14 days. Stopping it from 7 PM to 8 AM IST would save $15/month without affecting any scheduled jobs."
-
Continuous adaptation. As your infrastructure changes, the LLM adapts its recommendations. A rules engine needs manual updates; the LLM reads the new state and reasons about it.
The Cost of AI Optimization
Running GPT-4o for this analysis costs approximately $0.02–0.05 per analysis cycle (daily). At $1.50/month, the AI cost is negligible compared to the $330/month in savings.
AI Cost: $1.50/month (GPT-4o API calls)
Savings: $330/month (cloud cost reduction)
ROI: 220x
How to Build Your Own Cloud Cost Optimizer
If you want to replicate this approach for your own infrastructure:
-
Start with AWS Cost Anomaly Detection (free) — it identifies unusual spending patterns automatically.
-
Write a simple ingestion script that pulls CloudWatch metrics for all EC2 instances. You only need the AWS SDK and a cron job.
-
Compute basic health scores — average CPU over 14 days is a powerful signal. Below 5% = idle. Below 20% = underutilized.
-
Feed the data to an LLM with strict safety constraints. Start with GPT-4o or Claude. Use JSON mode for structured output.
-
Build a read-only dashboard first. Show recommendations without executing them. Let a human approve each action before automating.
-
Add execution later. Once you trust the recommendations, add one-click actions using AWS SDK calls (stop instance, resize, delete volume).
The Bottom Line
Manual cloud cost optimization does not scale. As your infrastructure grows, the waste grows with it — and the time required to find and fix it grows faster.
AI-powered optimization changes the equation: instead of a human spending 4 hours/month digging through dashboards, an LLM analyzes your entire infrastructure in seconds, generates safe recommendations, and presents them in plain English.
The 35% savings I demonstrated are not theoretical — they are from a real staging environment. For production workloads, the savings are typically 15–25% (less waste to find, but the waste that exists is more expensive).
Want to add AI-powered cost optimization to your cloud infrastructure? I build FinOps tooling and AI-integrated SaaS platforms. Get in touch or explore SubTrackHub for the full architecture.