LLM Cost Calculator — Java 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 Java implementation — the same logic the interactive tool runs, in a shareable, citable form.
// llm-cost-calculator — Java port: per-million-token cost math for LLM workloads.
import java.util.Comparator;
import java.util.List;
import java.util.OptionalDouble;
/** Per-million-token cost math, ported from the canonical TS lib. */
public final class LlmCost {
/** Multiplier applied to Batch API pricing (the standard 50% discount). */
public static final double BATCH_DISCOUNT = 0.5;
/** Price pair for a model or a custom rate card. null = unpriced. */
public record CostRates(Double inputPerM, Double outputPerM) {}
/** One workload to price. */
public record CostInput(long inputTokens, long outputTokens, long requests, boolean batch) {
public CostInput(long inputTokens, long outputTokens) {
this(inputTokens, outputTokens, 1, false);
}
}
/** Pricing projection of a model — the only three fields cost math needs. */
public record Model(String id, Double inputPerM, Double outputPerM) {}
/** Cost in USD, or empty when either rate is unpriced. */
public static OptionalDouble costFor(CostRates r, CostInput w) {
if (r.inputPerM() == null || r.outputPerM() == null) return OptionalDouble.empty();
double base = w.inputTokens() / 1e6 * r.inputPerM()
+ w.outputTokens() / 1e6 * r.outputPerM();
return OptionalDouble.of(base * w.requests() * (w.batch() ? BATCH_DISCOUNT : 1.0));
}
/** Output tokens per USD: 1e6 / outputPerM, or empty when unpriced. */
public static OptionalDouble tokensPerDollar(Model m) {
return m.outputPerM() == null ? OptionalDouble.empty()
: OptionalDouble.of(1e6 / m.outputPerM());
}
public static void main(String[] args) {
List<Model> models = List.of(
new Model("sonnet-class", 3.0, 15.0),
new Model("budget-class", 0.5, 2.0),
new Model("preview", null, null)); // unpriced model
CostInput work = new CostInput(12_000, 3_000, 250, true);
Comparator<Model> cheapestFirst = (a, b) -> {
OptionalDouble ca = costFor(new CostRates(a.inputPerM(), a.outputPerM()), work);
OptionalDouble cb = costFor(new CostRates(b.inputPerM(), b.outputPerM()), work);
if (ca.isPresent() && cb.isPresent()) {
int byCost = Double.compare(ca.getAsDouble(), cb.getAsDouble());
return byCost != 0 ? byCost : a.id().compareTo(b.id());
}
if (ca.isPresent() != cb.isPresent()) return ca.isPresent() ? -1 : 1;
return a.id().compareTo(b.id());
};
models.stream().sorted(cheapestFirst).forEach(m -> {
OptionalDouble cost = costFor(new CostRates(m.inputPerM(), m.outputPerM()), work);
if (cost.isPresent())
System.out.printf("%-14s $%.4f (%.0f output tokens/USD)%n",
m.id(), cost.getAsDouble(), tokensPerDollar(m).orElse(0));
else
System.out.printf("%-14s unpriced%n", m.id());
});
}
}
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 →