"""Offline token-charge scenarios. USD rates checked 2026-10-08. No API, network, or external packages. Excludes tool fees/taxes/regional pricing. Uses total prompt length (fresh + cached + written) for the Haiku price band. Batch mode here is intentionally limited to uncached input/output scenarios. """ import argparse from decimal import Decimal as D MILLION = D(1000000) def cost(fresh=0, output=0, read=0, write5=0, write1=0, batch=False): counts = (fresh, output, read, write5, write1) if any(not isinstance(n, int) or isinstance(n, bool) or n < 0 for n in counts): raise ValueError("Token counts must be nonnegative integers") if batch and (read or write5 or write1): raise ValueError("This calculator's batch scenario supports uncached input/output only") prompt = fresh + read + write5 + write1 if prompt > 1000000: raise ValueError("Prompt exceeds stated context capacity; this is not a request-validity checker") multiplier = D(5) if prompt > 100000 else D(1) # Cache creation tokens replace ordinary input billing; do not double count. rates = (D('.10'), D('.50'), D('.01'), D('.125'), D('.20')) amount = sum(D(n) * rate for n, rate in zip(counts, rates)) * multiplier / MILLION return amount * (D('.5') if batch else D(1)) def competitor_cost(model, fresh, output): if fresh < 0 or output < 0: raise ValueError("Negative token count") if model == 'luna': ir, outr = (D('.20'), D('.75')) if fresh > 272000 else (D('.10'), D('.50')) elif model == 'deepseek-offpeak': ir, outr = D('.15'), D('.60') else: raise ValueError("Unsupported model") return (fresh * ir + output * outr) / MILLION def self_test(): scenarios = [ (dict(fresh=10000, output=2000), '0.002'), (dict(fresh=100000, output=2000), '0.011'), (dict(fresh=100001, output=2000), '0.0550005'), (dict(fresh=10000, read=80000, output=2000), '0.0028'), (dict(fresh=10000, write5=80000, output=2000), '0.012'), (dict(fresh=10000, write1=80000, output=2000), '0.018'), (dict(fresh=10000, output=2000, batch=True), '0.001'), (dict(fresh=150000, output=10000), '0.100'), (dict(fresh=400000, output=10000), '0.225'), (dict(fresh=1, read=100000, output=2000), '0.0100005'), ] for args, expected in scenarios: assert cost(**args) == D(expected), (args, expected) assert competitor_cost('luna', 150000, 10000) == D('.020') assert competitor_cost('luna', 400000, 10000) == D('.0875') assert competitor_cost('deepseek-offpeak', 150000, 10000) == D('.0285') assert competitor_cost('deepseek-offpeak', 400000, 10000) == D('.066') assert cost(fresh=10000, write5=80000, output=2000) + 9*cost(fresh=10000,read=80000,output=2000) == D('.0372') for args in [dict(fresh=-1), dict(batch=True, read=1), dict(fresh=1000001)]: try: cost(**args) except ValueError: pass else: raise AssertionError(args) print('18 arithmetic, boundary, amortization, and invalid-input checks passed.') if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) for name in ['input','output','read','write5','write1']: parser.add_argument('--'+name, type=int, default=0) parser.add_argument('--batch', action='store_true') parser.add_argument('--self-test', action='store_true') args = parser.parse_args() if args.self_test: self_test() else: try: charge = cost(args.input,args.output,args.read,args.write5,args.write1,args.batch) except ValueError as error: parser.error(str(error)) print(format(charge, 'f') + ' USD (estimated token charge)')