keepalive: bill the TTS warm ping to its own team (agent:audio-keepalive)

The ping ran on the master key, so every row logged team_id '' and ~$0.19/day
of real Tinfoil spend resolved to no alias: invisible on the panel, unseen by
the digest, and the unexplained half of the spend-guard delta.

audio_keepalive.py now reads the team key from /app/data/.keepalive_key
(model-scoped to tinfoil/voxtral-tts, team budget $10/30d) and falls back to
the master key, warning once, if the file is gone. README documents the key
and how to recreate it.
This commit is contained in:
inference-admin committed 2026-11-28 13:20:30 +00:00
1 parent 41b8560f46
commit 2e1e35d7fb
3 files changed
+69 -24

No files matched your search

+42 -12
View File
@@ -4,31 +4,61 @@ so voice mode doesn't pay the ~24s cold-start after idle.
Runs INSIDE the gateway container, invoked by the keep-alive loop in
start.sh (every 15 min; the Cloudron scheduler task was removed
2026-11-09 so the loop is the only warm path). Reads the master key from
env or /app/data/.master_key. TTS-only (STT pings cost
2026-11-09 so the loop is the only warm path). TTS-only (STT pings cost
$0.01/request each; TTS $0.002 — see docs, keep-alive economics). Silent on
success; prints + exits non-zero on failure so keepalive.log captures it.
BILLING: the ping uses the keepalive team's own key (/app/data/.keepalive_key,
team agent:audio-keepalive, model-scoped to tinfoil/voxtral-tts). Anything sent
with the master key writes SpendLogs rows with team_id '' — no alias, so no
panel, digest or guard can attribute the spend. The master key stays as a
fallback for warmth if the team key is missing, and that fallback warns once.
"""
import json, os, urllib.request, urllib.error, sys
PORT = os.environ.get("PORT", "4000")
def master_key():
"""The gateway's start.sh generates the master key into /app/data/.master_key
and exports LITELLM_MASTER_KEY for the litellm process — scheduler tasks run
in a separate process tree, so read the key file directly."""
key = os.environ.get("LITELLM_MASTER_KEY", "")
if key:
return key
def _read(path):
try:
with open("/app/data/.master_key") as f:
with open(path) as f:
return f.read().strip()
except OSError:
return ""
KEY = master_key()
def warn_once(msg):
"""keepalive.log must only grow on failures; a standing config warning would
repeat every 15 min. Note it once per container generation instead."""
marker = "/app/data/.keepalive_warned"
if os.path.exists(marker):
return
print(msg, file=sys.stderr)
try:
open(marker, "w").close()
except OSError:
pass
def master_key():
"""The gateway's start.sh generates the master key into /app/data/.master_key
and exports LITELLM_MASTER_KEY for the litellm process — scheduler tasks run
in a separate process tree, so read the key file directly."""
return os.environ.get("LITELLM_MASTER_KEY", "") or _read("/app/data/.master_key")
def keepalive_key():
"""The team key, so warm pings bill to agent:audio-keepalive and show on the
co-op's own-spend line. Master key is the fallback (unattributed rows)."""
key = os.environ.get("LITELLM_KEEPALIVE_KEY", "") or _read("/app/data/.keepalive_key")
if key:
return key
warn_once("keepalive: /app/data/.keepalive_key missing - falling back to the "
"master key; these pings will have no team and bill unattributed")
return master_key()
KEY = keepalive_key()
if not KEY:
print("keepalive: no master key (env or /app/data/.master_key)", file=sys.stderr)
print("keepalive: no key (keepalive file, env or master key)", file=sys.stderr)
sys.exit(1)
fail = 0