* feat: add standalone Rust text pricing crate * feat(rust): harden standalone cost calculator --------- Co-authored-by: Yujong Lee <yujong@berri.ai> |
||
|---|---|---|
| .. | ||
| benches | ||
| examples | ||
| src | ||
| tests | ||
| Cargo.toml | ||
| README.md | ||
litellm-cost
This crate calculates text token charges from rates and usage supplied by its caller. It is standalone and has no Python bridge or proxy integration
Call compile(&pricing) once for an immutable plan, then plan.calculate(&request) for each supported request. calculate(&pricing, &request) compiles on each call. A successful result exposes pre-multiplier component costs, selected rates, the multiplier, and derived input(), output(), and total() values
The caller states whether prompt_tokens includes cache tokens. Threshold selection uses total input tokens for either convention and selects one rate for the whole request. Thresholds are sorted when compiled, and duplicate thresholds or tier overrides fail deterministically. Fast selects priority rates; unknown tiers use standard rates
Rate::Missing, Rate::Null, and Rate::Value(0.0) remain distinct. Missing cache rates fall back to the selected input rate, and an absent one-hour write rate falls back to the selected write rate. Missing input or output rates return typed errors, including for zero usage. Python's sparse-entry behavior remains outside this native contract
The supported off-peak shape is one non-wrapping UTC daily window. The caller supplies the applicable regional multiplier after provider-specific selection. Negative or non-finite rates, ambiguous rules, inconsistent cache counts, incomplete write splits, invalid windows and overflow return errors. Callers must decline unsupported inputs before native execution if their public contract accepts those shapes
This crate does not select models, read catalogs, fetch provider prices, normalize multimodal usage, process provider-reported costs, or calculate non-token charges. It does not change proxy behavior. The reference fixture was generated by tests/generate_python_reference.py against the Python implementation at the commit recorded in tests/python_reference.tsv, using synthetic rates and fixed usage