"""Record the OpenRouter spend for the run: account usage after minus before."""
import json, os, sys, urllib.request
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import orlib
HERE = os.path.dirname(os.path.abspath(__file__))
base = json.load(open(os.path.join(HERE, "spend_baseline.json")))["baseline_usage_usd"]
d = json.load(urllib.request.urlopen(urllib.request.Request(
    "https://openrouter.ai/api/v1/credits",
    headers={"Authorization": "Bearer " + orlib.K}), timeout=30))["data"]
spend = round(d["total_usage"] - base, 2)
N = json.load(open(os.path.join(HERE, "numbers.json")))
N["openrouter_spend_usd"] = spend
json.dump(N, open(os.path.join(HERE, "numbers.json"), "w"), indent=1)
print("spend $%.2f" % spend)
