Skip to main content

How do I calculate and retrieve realized cost savings using the API?

Last updated 5 min read

Send a GET request to `/api/v2/savings` with your private API key to retrieve realized cost savings programmatically. Coolhand compares the cost of requests served by active prompt templates against what those same requests would have cost on the deprecated templates or models they replaced over the preceding three months.

To track your return on investment or feed automated FinOps dashboards, you can retrieve realized cost savings programmatically using Coolhand's REST API or Model Context Protocol (MCP) interface.

How realized savings are calculated

Coolhand tracks template version history and model deprecation chains across your workloads. When you replace a prompt template or model version with a more efficient alternative, the previous version is marked as deprecated.

For all template deprecations that occurred within the trailing three months, Coolhand calculates:

  1. Current active cost: What your requests served by the active replacement templates actually cost.
  2. Baseline cost: What those exact same requests would have cost if they had been executed using the deprecated template versions and model pricing rates they replaced.
  3. Realized savings: The difference between the baseline cost and the current active cost.

This metric matches the savings figures displayed on the Coolhand dashboard.

Retrieving savings with the REST API

Send a GET request to /api/v2/savings using your private API key. Public API keys cannot access this endpoint.

curl -H "X-API-Key: $COOLHAND_PRIVATE_API_KEY" \
  https://api.coolhandlabs.com/api/v2/savings

You can also pass your private API key via the Authorization header:

curl -H "Authorization: Bearer $COOLHAND_PRIVATE_API_KEY" \
  https://api.coolhandlabs.com/api/v2/savings

Response structure

The endpoint returns aggregate dollar savings, the baseline cost, the overall savings percentage, and a breakdown across workloads that had template deprecations in the last three months:

{
  "total": 128.4521,
  "cost_at_previous_versions": 412.1852,
  "percent": 31.2,
  "workloads": [
    {
      "id": "wkld_8x92k1mz",
      "name": "Customer Support Triage",
      "savings": 82.1245,
      "cost_at_previous_versions": 260.4012,
      "active_requests": 24150
    },
    {
      "id": "wkld_3f82kd9a",
      "name": "Document Summarization",
      "savings": 46.3276,
      "cost_at_previous_versions": 151.7840,
      "active_requests": 8420
    }
  ]
}

Response fields

  • total: Total dollar savings (USD) realized across all active templates compared to their deprecated versions, rounded to four decimal places.
  • cost_at_previous_versions: Total baseline cost (USD) had those requests run on the deprecated template versions, rounded to four decimal places.
  • percent: Savings expressed as a percentage of the baseline cost (total / cost_at_previous_versions * 100), rounded to one decimal place.
  • workloads: An array of workload-level savings records. Only workloads with at least one template deprecated within the preceding three months are included.
    • id: The workload identifier, matching the id returned by GET /api/v2/workloads.
    • name: The display name of the workload.
    • savings: Dollar savings realized for this workload, rounded to four decimal places.
    • cost_at_previous_versions: Baseline cost for this workload on previous versions, rounded to four decimal places.
    • active_requests: Number of requests processed on the active replacement templates during the deprecation comparison window.

Caching and error handling

  • Caching: Results are cached for one hour.
  • No deprecations: If your account has no template deprecations in the preceding three months, the endpoint returns total: 0.0, cost_at_previous_versions: 0.0, percent: 0.0, and an empty workloads: [] array.
  • Service unavailable (503): If the comparison cannot be computed at request time, the API returns HTTP 503 Service Unavailable:
    {
      "errors": {
        "system": [
          "Realized savings could not be computed right now. Please retry."
        ]
      }
    }
    

    A 503 status code indicates a temporary computation failure rather than zero savings. Scripts should catch 503 responses and retry after a brief backoff.

Retrieving savings with MCP

If you query Coolhand observability data using an AI assistant or agent over the Model Context Protocol (MCP), you can call the get_client_roi_summary tool at POST /mcp.

The tool returns total spend, request volume, per-workload costs, and realized savings in a single call:

{
  "client_id": "cli_9x82mk1a",
  "client_name": "Acme Corp",
  "days_analyzed": 30,
  "totals": {
    "requests": 32570,
    "total_cost": 283.7331
  },
  "realized_savings": {
    "total": 128.4521,
    "cost_at_previous_versions": 412.1852,
    "percent": 31.2
  },
  "workloads": [
    {
      "workload_id": "wkld_8x92k1mz",
      "name": "Customer Support Triage",
      "requests": 24150,
      "total_cost": 178.2767,
      "avg_cost_per_request": 0.007382,
      "error_rate": 0.0012,
      "sentiment_score": 0.89,
      "realized_savings": 82.1245
    }
  ]
}

Tool parameters

  • client_id (string, required): Your client ID.
  • days_back (integer, optional): Number of days of spend and request volume to summarize (default is 30, clamped between 1 and 365).

Window behavior differences

The days_back parameter applies only to request counts and active spend in the totals and workloads objects.

By contrast, realized_savings always covers the full three-month deprecation comparison window. Because realized savings accounts for up to three months of version history regardless of days_back, a workload's realized_savings can exceed its windowed total_cost if you query a short lookback window such as days_back: 7.

If savings cannot be computed when calling the tool, realized_savings returns {"unavailable": true} instead of failing the entire tool call.

Pairing savings with workload metrics

To analyze both current workload performance and historical savings, FinOps reporting scripts often correlate /api/v2/savings with /api/v2/workloads:

  1. Call GET /api/v2/savings to fetch total and per-workload realized savings.
  2. Call GET /api/v2/workloads?include_metrics=true to retrieve active spend, token counts, error rates, and quality scores over your chosen days_back window (see How do I list and analyze my workloads using the API?).
  3. Match records using the common workload id field.

This provides a complete view of what your AI features cost today alongside the cumulative spend reductions achieved through prompt optimization and model right-sizing.

Related articles