Prompt
How do I track which API endpoints are used most and what they cost?
Latest observation
To track which API endpoints are used most and what they cost, you need two things:
- Usage data — who called what, when, how often, and with what size/latency/status.
- Cost data — the pricing rules that turn usage into dollars.
A practical setup looks like this:
1) Log every API request
Capture at least:
timestampendpoint/ routemethoduser_id/api_key/ tenantstatus_codelatency_msrequest_sizeresponse_sizeregionor deployment, if relevanttrace_id/request_id
Example log fields:
{
"timestamp": "2026-10-05T12:34:56Z",
"endpoint": "/v1/orders",
"method": "POST",
"user_id": "u_123",
"api_key_id": "key_abc",
"status_code": 200,
"latency_ms": 143,
"request_size": 842,
"response_size": 2048
}
2) Aggregate endpoint usage
Create metrics such as:
- Requests per endpoint
- Unique users per endpoint
- Error rate per endpoint
- Avg / p95 latency per endpoint
- Bytes in/out per endpoint
This helps you identify the “most used” endpoints by:
- total requests
- total users
- total traffic
- total cost
3) Map each endpoint to cost
Costs usually come from one or more of these:
- Per request cost
- Per GB transferred
- Compute time cost
- Database / downstream calls cost
- Third-party API calls
- Inference/token usage if it’s AI-related
You can assign a cost formula per endpoint, for example:
cost(endpoint) = request_count * request_unit_cost
+ data_out_gb * egress_cost_per_gb
+ cpu_seconds * cpu_cost_per_second
If your endpoints have different cost profiles, keep a pricing table like:
| Endpoint | Cost per 1k requests | Cost per GB | Notes |
|---|---|---|---|
| /v1/orders | $0.20 | $0.05 | DB-heavy |
| /v1/search | $0.10 | $0.02 | cacheable |
| /v1/recommendations | $1.50 | $0.05 | ML-heavy |
4) Use observability tools
Common options:
-
Logs + metrics + traces:
- OpenTelemetry
- Prometheus + Grafana
- Datadog
- New Relic
- Splunk
- ELK/Elastic
-
API gateway analytics:
- Kong
- Apigee
- AWS API Gateway
- Azure API Management
- Cloudflare API Gateway
These often provide endpoint-level request counts, latency, and sometimes cost attribution.
5) Build a cost dashboard
Useful dashboard views:
- Top endpoints by request volume
- Top endpoints by estimated cost
- Cost per request by endpoint
- Cost over time
- Cost by customer / team / API key
- Error rate vs cost
- Latency vs cost
Example query outputs:
/v1/search= 1.2M requests, $430 estimated cost/v1/orders= 180k requests, $290 estimated cost/v1/export= 12k requests, $510 estimated cost
That last one is a classic “low volume, high cost” endpoint.
6) If you want chargeback/showback
Track usage by:
- customer
- team
- environment
- project
- API key
Then attribute cost to each tenant:
tenant_cost = sum(endpoint_cost for requests made by tenant)
7) Best practices
- Use a request ID and trace ID to correlate logs, metrics, and downstream costs.
- Store raw logs in case pricing changes later.
- Separate estimated cost from actual billed cost.
- Watch for hidden costs like retries, timeouts, and failed requests.
- Normalize by endpoint version (
/v1,/v2) so you don’t mix old and new behavior.
Simple implementation pattern
- Instrument your API server with structured logging.
- Export metrics to Prometheus/Datadog.
- Maintain a cost mapping table per endpoint.
- Run a scheduled job to compute daily endpoint usage and estimated cost.
- Publish results in a dashboard.
If you want, I can also give you:
- a SQL query to calculate endpoint usage and cost,
- a Python example for logging and cost aggregation, or
- a Grafana/Datadog dashboard layout.