Overview
Tiered pricing cost calculation engine for Helicone's usage-based billing, computing costs for both storage (GB) and API requests with detailed tier breakdowns and savings estimates.
Description
This module provides three core functions for calculating usage-based costs:
- calculateGBCost -- Computes storage costs by iterating through the
GB_PRICING_TIERS, distributing the total GB across tiers from most expensive to least expensive. For each tier, it calculates how many GB fall within that tier's capacity, applies the per-GB rate, and accumulates the result. It also computes savings compared to paying the first-tier flat rate for all GB.
- calculateRequestCost -- Computes request costs using
REQUEST_PRICING_TIERS with the same tiered distribution approach. The first 10,000 requests are free, and savings are calculated by comparing the actual tiered cost against a flat rate at the first paid tier's price.
- calculateTotalCost -- Combines both calculations, returning individual breakdowns plus aggregate totals for cost and savings.
Each function returns a CostResult containing the total cost, the savings compared to flat-rate pricing, and a breakdown array of TierBreakdown objects showing the units, rate, and cost for each tier.
Usage
Use these functions in pricing pages, billing dashboards, and cost estimation UIs across the web frontend and Bifrost marketing site. The detailed breakdown is useful for rendering tiered pricing tables, while the savings figure highlights the benefit of volume usage.
Code Reference
Source Location
Signature
export interface TierBreakdown {
tier: string;
units: number;
rate: number;
cost: number;
}
export interface CostResult {
cost: number;
savings: number;
breakdown: TierBreakdown[];
}
export function calculateGBCost(totalGB: number): CostResult;
export function calculateRequestCost(totalRequests: number): CostResult;
export function calculateTotalCost(
totalRequests: number,
totalGB: number
): {
requestsCost: CostResult;
gbCost: CostResult;
totalCost: number;
totalSavings: number;
};
Import
import {
calculateGBCost,
calculateRequestCost,
calculateTotalCost,
CostResult,
TierBreakdown,
} from "@helicone/pricing/calculator";
I/O Contract
calculateGBCost
| Parameter |
Type |
Description
|
totalGB |
number |
Total gigabytes of storage used
|
| Returns |
Type |
Description
|
cost |
number |
Total tiered cost in dollars
|
savings |
number |
Savings vs. flat first-tier rate
|
breakdown |
TierBreakdown[] |
Per-tier detail (tier label, units, rate, cost)
|
calculateRequestCost
| Parameter |
Type |
Description
|
totalRequests |
number |
Total number of API requests
|
| Returns |
Type |
Description
|
cost |
number |
Total tiered cost in dollars
|
savings |
number |
Savings vs. flat rate (second tier rate applied to all paid requests)
|
breakdown |
TierBreakdown[] |
Per-tier detail (tier label, units, rate, cost)
|
calculateTotalCost
| Parameter |
Type |
Description
|
totalRequests |
number |
Total number of API requests
|
totalGB |
number |
Total gigabytes of storage used
|
| Returns |
Type |
Description
|
requestsCost |
CostResult |
Request cost breakdown
|
gbCost |
CostResult |
Storage cost breakdown
|
totalCost |
number |
Sum of both costs
|
totalSavings |
number |
Sum of both savings
|
Usage Examples
import { calculateGBCost, calculateRequestCost, calculateTotalCost } from "@helicone/pricing/calculator";
// Calculate storage cost for 100 GB
const gbResult = calculateGBCost(100);
// gbResult.cost = 30 * 3.25 + 50 * 2.0 + 20 * 1.25 = 97.50 + 100 + 25 = 222.50
// gbResult.breakdown has 3 tier entries
// Calculate request cost for 50,000 requests
const reqResult = calculateRequestCost(50_000);
// First 10K free, 10K-30K at $0.0007, 30K-50K at $0.00035
// Calculate combined cost
const total = calculateTotalCost(50_000, 100);
// total.totalCost = reqResult.cost + gbResult.cost
// total.totalSavings = reqResult.savings + gbResult.savings
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.