llm-cost-cap
Pre-flight USD cost gate for a single LLM call.
Before sending a request, estimate the worst-case cost (input tokens at
the input rate plus the requested max_output_tokens at the output rate)
and reject if it would exceed a configured per-call cap. Catches the
failure mode where an agent burns $20 on one call before the call ever
leaves the process. Zero non-std deps. Built-in price table covers
Anthropic, OpenAI, Gemini, and Bedrock variants.
Install
[]
= "0.1"
Example
use ;
let cap = new;
match cap.check
check() returns the EstimatedCost breakdown on success so you can
log or budget against the exact number that gated the call.
Wrap a call
run() does the check first and only invokes your closure if the
estimate passes.
use CostCap;
let cap = new;
let result = cap.run?;
# Ok::
Custom prices
use ;
use HashMap;
let mut prices = new;
prices.insert;
let cap = with_prices;
Or register one model into an existing cap:
# use ;
let mut cap = new;
cap.add_model;
Features
serde— deriveSerialize/DeserializeonModelPriceandEstimatedCost.
Siblings
claude-cost— cache-aware cost calculator for Anthropic.bedrock-cost— cross-vendor Bedrock pricing.token-budget-pool— concurrent token / USD budget.
License
MIT