Skip to content

LLM Cost Calculator — C source

Estimate LLM costs per request or per month at billion-token scale — with realistic prompt-cache hit rates, four-lane pricing, and side-by-side model comparison from a dated pricing snapshot.

This is the C implementation — the same logic the interactive tool runs, in a shareable, citable form.

/* llm-cost-calculator — C port: per-million-token cost math for LLM workloads. */
#include <math.h>
#include <stdbool.h>
#include <stdio.h>

/* Multiplier applied to Batch API pricing (the standard 50% discount). */
#define BATCH_DISCOUNT 0.5

/* Price pair for a model or a custom rate card. priced == false means
 * "unpriced" — the TS reference uses null rates and propagates them to a
 * null cost; C has no null doubles, so a flag + NaN carries the same shape. */
typedef struct {
    bool priced;
    double input_per_m;  /* USD per 1M input tokens. */
    double output_per_m; /* USD per 1M output tokens. */
} CostRates;

/* One workload to price. */
typedef struct {
    long input_tokens;  /* Input tokens per request. */
    long output_tokens; /* Output tokens per request. */
    long requests;      /* Request count. */
    bool batch;         /* Apply the BATCH_DISCOUNT multiplier. */
} CostInput;

/* Cost in USD, or NAN when either rate is unpriced:
 * ((in/1e6)*input_per_m + (out/1e6)*output_per_m) * requests * discount. */
static double cost_for(const CostRates *r, const CostInput *w)
{
    if (!r->priced)
        return NAN;
    double base = (double)w->input_tokens / 1e6 * r->input_per_m
                + (double)w->output_tokens / 1e6 * r->output_per_m;
    return base * (double)w->requests * (w->batch ? BATCH_DISCOUNT : 1.0);
}

/* Output tokens per USD: 1e6 / output_per_m, or NAN when unpriced. */
static double tokens_per_dollar(const CostRates *r)
{
    return r->priced ? 1e6 / r->output_per_m : NAN;
}

int main(void)
{
    /* Rates are never hardcoded in the TS lib — they flow from the model
     * snapshot. This demo pins three example cards, one of them unpriced. */
    const CostRates sonnet = { true, 3.0, 15.0 };
    const CostRates budget = { true, 0.5, 2.0 };
    const CostRates preview = { false, 0.0, 0.0 }; /* unpriced model */

    const struct {
        const char *id;
        const CostRates *rates;
    } models[] = {
        { "sonnet-class", &sonnet },
        { "budget-class", &budget },
        { "preview", &preview },
    };
    const CostInput work = { 12000, 3000, 250, true };

    for (size_t i = 0; i < sizeof models / sizeof models[0]; i++) {
        double cost = cost_for(models[i].rates, &work);
        if (isnan(cost)) {
            printf("%-14s unpriced\n", models[i].id);
            continue;
        }
        printf("%-14s $%.4f  (%.0f output tokens/USD)\n", models[i].id, cost,
               tokens_per_dollar(models[i].rates));
    }
    return 0;
}

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →