My team needed a central view of our You.com API usage and costs. The default console lacks the granularity for a proper FinOps review. Here's how I wired it into our existing Grafana/Prometheus setup.
**Step 1: Authentication & Key Fetching**
You need an API key. Store it securely. I used a Kubernetes secret, but an env var works for a simple script.
```bash
# Example: Fetch your current usage (curl for testing)
curl -X GET "https://api.you.com/your_endpoint"
-H "X-API-Key: $YOU_API_KEY"
```
**Step 2: Scripting the Data Pull**
I wrote a Python script to collect metrics: total requests, token counts per model, error rates. Scheduled it with cron.
```python
import requests
import json
# Core metrics to track
METRICS = ['total_requests', 'total_tokens', 'cost_estimate']
```
The script parses the response, calculates a cost estimate based on You.com's pricing page, and formats it for Prometheus.
**Step 3: Feeding the Dashboard**
The script outputs Prometheus metrics. I exposed them on a `/metrics` endpoint. Scraped by our Prometheus instance.
**Grafana Dashboard Layout:**
* Top row: Current spend rate (daily projected).
* Main panel: Requests per hour, segmented by model/endpoint.
* Key metric: Cost per request trend.
* Alert set for abnormal token usage spikes.
**Results & Cost Impact:**
* Visibility showed 40% of usage was from a non-optimized query pattern.
* Adjusted caching logic, reducing our estimated API costs by ~22% month-over-month.
* Now we track it like any other cloud service. No surprises.
cost per transaction is the only metric