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.
using System;
using System.Collections.Generic;
using System.Linq;
namespace LlmCostCalculator;
/// <summary>Multiplier applied to Batch API pricing (the standard 50% discount).</summary>
public static class LlmCost
{
public const double BatchDiscount = 0.5;
/// <summary>Price pair for a model or a custom rate card. null = unpriced.</summary>
public sealed record CostRates(double? InputPerM, double? OutputPerM);
/// <summary>One workload to price.</summary>
public sealed record CostInput(long InputTokens, long OutputTokens,
long Requests = 1, bool Batch = false);
/// <summary>Pricing projection of a model — only these three fields
/// of the full model record matter to cost math.</summary>
public sealed record Model(string Id, double? InputPerM, double? OutputPerM);
/// <summary>Cost in USD, or null when either rate is unpriced:
/// ((in/1e6)·in + (out/1e6)·out) × requests × batch discount.</summary>
public static double? CostFor(CostRates rates, CostInput input)
{
if (rates.InputPerM is null || rates.OutputPerM is null) return null;
double b = input.InputTokens / 1e6 * rates.InputPerM.Value
+ input.OutputTokens / 1e6 * rates.OutputPerM.Value;
return b * input.Requests * (input.Batch ? BatchDiscount : 1.0);
}
/// <summary>Output tokens per USD: 1e6 / OutputPerM, or null when unpriced.</summary>
public static double? TokensPerDollar(Model m) =>
m.OutputPerM is null ? null : 1e6 / m.OutputPerM.Value;
/// <summary>Cost every model for one workload, sorted: cost asc,
/// unpriced last, ties by id asc — the TS comparator verbatim.</summary>
public static IEnumerable<(string Id, double? Cost, double? TokensPerUsd)>
CompareModels(IEnumerable<Model> models, CostInput work) =>
models.Select(m => (m.Id,
CostFor(new CostRates(m.InputPerM, m.OutputPerM), work),
TokensPerDollar(m)))
.OrderBy(r => r.Item2.HasValue) // priced rows first
.ThenBy(r => r.Item2 ?? 0.0) // cost ascending
.ThenBy(r => r.Id, StringComparer.Ordinal);
public static void Main()
{
var models = new[] {
new Model("sonnet-class", 3.0, 15.0),
new Model("budget-class", 0.5, 2.0),
new Model("preview", null, null), // unpriced model
};
var work = new CostInput(InputTokens: 12_000, OutputTokens: 3_000,
Requests: 250, Batch: true);
foreach (var (id, cost, tpd) in CompareModels(models, work))
Console.WriteLine(cost is null
? $"{id,-14} unpriced"
: $"{id,-14} ${cost:F4} ({tpd:F0} output tokens/USD)");
}
}
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 →