GPU Cloud Billing with Rafay’s Usage Metering APIs | Rafay

GPU/Neocloud Billing using Rafay’s Usage Metering APIs

September 13, 2025

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:

Important: 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.

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"
    fd_csv = open(filename, 'w')
    csv_writer = csv.writer(fd_csv)

# Check required env vars
    DAYS = int(os.environ.get('DAYS', None))
    RAFAY_CONSOLE_URL = os.environ.get('RAFAY_CONSOLE_URL', None)
    RAFAY_DEFAULT_API_KEY = os.environ.get('RAFAY_DEFAULT_API_KEY', None)

# Output header
    csv_writer.writerow(metrics_row)

# Compute the time window in UTC (now minus DAYS)
    current_time_utc = datetime.now(timezone.utc)
    past_time_utc = current_time_utc - timedelta(days=int(DAYS))

# Fetch organizations (tenants)
    organizations = get_organizations(RAFAY_CONSOLE_URL, RAFAY_DEFAULT_API_KEY)

# For each org, fetch usage details across profile types/profiles/instances
    for org in organizations:
        org_name = org.get('name', 'Unknown')
        profiles = get_profiles(RAFAY_CONSOLE_URL, org['metadata']['name'], RAFAY_DEFAULT_API_KEY)

for profile in profiles:
            profile_type = profile.get('spec', {}).get('type', 'Unknown')
            profile_name = profile.get('metadata', {}).get('name', 'Unknown')

# Fetch instance usage within the time window
            instance_usage = get_profile_instance_usage(
                RAFAY_CONSOLE_URL,
                org['metadata']['name'],
                profile_name,
                past_time_utc,
                current_time_utc,
                RAFAY_DEFAULT_API_KEY
            )

# Write each row to CSV
            for item in instance_usage.get("instance_usage_data", []):
                row = [org_name, profile_type, profile_name, item.get('instance_name', ''), item.get('usage_hours', 0), item.get('status', '')]
                csv_writer.writerow(row)

fd_csv.close()

Running the Script

To run the script, use the following Python command:

python3 ncp_metrics.py

You should see output indicating successful data retrieval.

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

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 queuing, reserved vs. on-demand, storage, egress).
  5. Generate invoices and expose line items in customer portal.

Operational Tips