Persistent Anthropic cost tracking to disk

Cost survives service restarts via /var/log/mc_anthropic_cost.json.
Saves after every API call. POS receipt prints at each $1 milestone.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Claude Code
2026-03-18 22:29:50 -04:00
parent 9ce2013814
commit 616aab7cf4
+32 -1
View File
@@ -2017,7 +2017,23 @@ def _llm_call(model: str, system: str, user: str, config: dict,
# --- Anthropic API cost tracking ---
_anthropic_cost_lock = threading.Lock()
_anthropic_total_cost = 0.0
_anthropic_cost_file = "/var/log/mc_anthropic_cost.json"
def _load_anthropic_cost():
try:
with open(_anthropic_cost_file) as f:
return json.load(f).get("total_cost", 0.0)
except:
return 0.0
_anthropic_total_cost = _load_anthropic_cost()
def _save_anthropic_cost():
try:
with open(_anthropic_cost_file, "w") as f:
json.dump({"total_cost": _anthropic_total_cost, "updated": time.strftime("%Y-%m-%dT%H:%M:%SZ")}, f)
except:
pass
def _get_anthropic_cost():
with _anthropic_cost_lock:
@@ -2072,6 +2088,7 @@ def _anthropic_call(model: str, system: str, user: str, config: dict,
with _anthropic_cost_lock:
prev_dollar = int(_anthropic_total_cost)
_anthropic_total_cost += cost
_save_anthropic_cost()
curr_dollar = int(_anthropic_total_cost)
if curr_dollar > prev_dollar:
log.info(f"Anthropic cost milestone: ${_anthropic_total_cost:.4f} / ${budget:.2f}")
@@ -3209,6 +3226,20 @@ def execute_response(response, context, config, praying_player=None):
)
time.sleep(0.2)
# Flash title on the praying player's screen
if praying_player and (commands or message):
try:
rcon(
f'title {praying_player} times 5 40 15',
config["rcon_host"], config["rcon_port"], config["rcon_password"]
)
rcon(
f'title {praying_player} title {{"text":"Your prayers have been answered!","color":"gold","bold":true}}',
config["rcon_host"], config["rcon_port"], config["rcon_password"]
)
except Exception:
pass
fallback = praying_player or (context["online_players"][0] if context["online_players"] else "")
max_cmds = config.get("max_commands_per_response", 6)