GPU/Neo Cloud Billing using Rafay’s Usage Metering APIs - Rafay Product Documentation

GPU/Neo Cloud Billing using Rafay’s Usage Metering APIs

Cloud providers offering GPU or Neo Cloud services need accurate and automated mechanisms to track resource consumption. Usage data becomes the foundation for billing, showback, or chargeback models that customers expect. The Rafay Platform provides usage metering APIs that can be easily integrated into a provider’s billing system.

In this blog, we’ll walk through how to use these APIs with a sample Python script to generate detailed usage reports.


Prerequisites & Environment

This exercise assumes that you have access to an instance of the Rafay Platform. Also ensure that you have Org Admin level access to the Default Org so that you can use the API Keys to programmatically retrieve the usage metering data.

Set the following environment variables on your system. Ensure you update with the correct values for your environment.

export DAYS=30
export RAFAY_CONSOLE_URL=rafay.acme.com
export RAFAY_DEFAULT_API_KEY=default_org_api_key

Type "env" in Terminal to verify if the variables were set correctly.

Tip: Cloud Providers can run this script via a nightly cron/Kubernetes CronJob to keep metering data current on their systems.


What the Example Script Produces

The example script will use the APIs to retrieve the data and generate two timestamped CSVs in the working directory:

Info

Columns include organization, profile type, profile, instance, usage (hours), and status — ideal for billing ETL and dashboards.


Full Annotated Script

Shown below is working sample code in Python to retrieve the usage/metering data.

"""
ncp_metrics.py — Annotated

Purpose: Retrieve usage metering data from the Rafay Platform for a configurable window
         (DAYS env var) and write results to timestamped CSV files for billing.

Environment variables:
  - DAYS: Number of days to look back (e.g., 30 days).
  - RAFAY_CONSOLE_URL: Your Rafay Platform's base domain (e.g., rafay.acme.com).
  - RAFAY_DEFAULT_API_KEY: Default Org's Org Admin API key used for x-api-key auth.

Typical usage:
  $ export DAYS=30
  $ export RAFAY_CONSOLE_URL=rafay.acme.com
  $ export RAFAY_DEFAULT_API_KEY=default_org_api_key
  $ python ncp_metrics.py

Notes:
  - Output files: ncp-metrics-<timestamp>.csv and a sorted variant,
                  ncp-metrics-sorted-<timestamp>.csv
  - Safe to run as a cron job / Kubernetes CronJob for nightly metering pulls.
"""

import csv
import json
import os
import requests
import sys
import time
from datetime import datetime, timedelta, timezone

def main():
    timestr = time.strftime("%m%d%Y-%H%M%S")
    metrics_row = ["Organization", "Profile Type", "Profile", "Instance", "Usage(h)", "Status"]
    filename = "ncp-metrics-" + timestr + ".csv"
    filename_sorted = "ncp-metrics-sorted-" + timestr + ".csv"
    fd_csv = open(filename, 'w')
    csv_writer = csv.writer(fd_csv)
    csv_writer.writerow(metrics_row)

days = int(os.environ['DAYS'])
    api_key = os.environ['RAFAY_DEFAULT_API_KEY']
    url = os.environ['RAFAY_CONSOLE_URL']

v3_headers = {
        "accept": "application/json",
        "x-api-key": api_key,
        "Content-Type": "application/json"
    }
    now_utc = datetime.now(timezone.utc)
    current_time_str = get_formatted_utc_timestamp(now_utc)

time_delta = timedelta(days=days)
    past_time_utc = now_utc - time_delta
    past_time_str = get_formatted_utc_timestamp(past_time_utc)

compute_instances = list_compute_instances(v3_headers, url, past_time_str, current_time_str, "compute")
    if compute_instances:
        for instance in compute_instances["instance_usage_data"]:
            print(f"Organization: {instance["instance_organization_name"]}")
            print(f"Profile: {instance["profile_name"]}")
            print(f"Instance: {instance["instance_name"]}")
            print(f"Usage: {instance["usage"]}")
            if "deleted_at" in instance.keys():
                status = "Deleted"
            else:
                status = "Running"
            print(f"\n")
            csv_writer.writerow([instance["instance_organization_name"], "Compute", instance["profile_name"], instance["instance_name"], instance["usage"], status])

service_instances = list_compute_instances(v3_headers, url, past_time_str, current_time_str, "service")
    if service_instances:
        for instance in service_instances["instance_usage_data"]:
            print(f"Organization: {instance["instance_organization_name"]}")
            print(f"Profile: {instance["profile_name"]}")
            print(f"Instance: {instance["instance_name"]}")
            print(f"Usage: {instance["usage"]}")
            if "deleted_at" in instance.keys():
                status = "Deleted"
            else:
                status = "Running"
            print(f"\n")
            csv_writer.writerow([instance["instance_organization_name"], "Service", instance["profile_name"], instance["instance_name"], instance["usage"], status])
    fd_csv.close()
    sort_csv(filename, filename_sorted, "Organization", False, True)

def get_formatted_utc_timestamp(dt_object: datetime) -> str:
    return dt_object.strftime("%Y-%m-%dT%H:%M:%SZ")

def list_compute_instances(headers, url, start_time, end_time, profile_type):
    compute_url = f"https://{url}/apis/billing.envmgmt.io/v1/metrics/partner/instance/kind/{profile_type}/usage?range_from={start_time}&range_to={end_time}&limit=10000"
    response = requests.get(compute_url, headers=headers)
    if response.status_code == 200:
        return response.json()
    else:
        print(f"Failed to get compute instances. Status Code: {response.status_code}")
        return response.json()

def sort_csv(input_file: str, output_file: str, sort_column_name: str, is_numeric: bool = False, reverse: bool = False):
    print(f"--- Sorting CSV File ---")
    print(f"Reading from '{input_file}', sorting by column '{sort_column_name}'...")

try:
        with open(input_file, mode='r', newline='') as infile:
            reader = csv.reader(infile)
            header = next(reader)
            try:
                sort_column_index = header.index(sort_column_name)
            except ValueError:
                print(f"Error: Column '{sort_column_name}' not found in the CSV header.")
                return

data = list(reader)
            if is_numeric:
                sorted_data = sorted(data, key=lambda row: float(row[sort_column_index]), reverse=reverse)
            else:
                sorted_data = sorted(data, key=lambda row: row[sort_column_index], reverse=reverse)

with open(output_file, mode='w', newline='') as outfile:
            writer = csv.writer(outfile)
            writer.writerow(header)
            writer.writerows(sorted_data)

print(f"Successfully sorted data and saved to '{output_file}'.")

except FileNotFoundError:
        print(f"Error: The file '{input_file}' was not found.")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
    main()

Running the Script

To run the script, use the following Python command

python3 ncp_metrics.py

You should see something like the following. In the output, you can see that there are several tenants (Orgs) called Coke, Acme and Pepsi. The script is iterating through the instances of SKUs spanning both Coke and Pepsi tenants, reporting the usage for each instance.

Organization: Coke
Profile: managed-developer-pods-v2
Instance: demo-serverless-pod
Usage: 286.59h

Organization: Pepsi
Profile: openwebui
Instance: lan-openwebui
Usage: 52.54h

Organization: Pepsi
Profile: unsloth-finetune
Instance: test-abc
Usage: 0.12h

Organization: Coke
Profile: slurm-k8s
Instance: test-mohan
Usage: 17.73h

Organization: Acme
Profile: h110-small-vm
Instance: nvidia-h100-8gpu-vm
Usage: 0.34h

--- Sorting CSV File ---
Reading from 'ncp-metrics-09142025-075429.csv', sorting by column 'Organization'...
Successfully sorted data and saved to 'ncp-metrics-sorted-09142025-075429.csv'.

The API returns a lot more data. In this example script, we have limited the output to only select fields from the available output.

An example of results in the "unsorted" CSV is shown below

Shown below is an example of results in the "sorted" CSV. In this example,


Integrate Usage Metering Data into Billing System

Cloud Providers and Enterprises can use the following approach to integrate the usage and metering data into their billing or chargeback systems:

  1. ETL/ELT into your billing DB (e.g., Postgres, BigQuery).
  2. Join usage rows with your price book (e.g., by profile type/name or instance attributes).
  3. Calculate charges i.e. gpu_hours * rate
  4. Optionally add surcharges (priority queueing, reserved vs. on-demand, storage, egress).
  5. Generate invoices and expose line items in customer portal.

Operational Tips