Files
litellm/audio_keepalive.py
T
inference-admin 2e1e35d7fb 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.
2026-11-28 13:20:30 +00:00

82 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""Audio keep-alive: ping the Tinfoil TEE audio models through localhost LiteLLM
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). 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 _read(path):
try:
with open(path) as f:
return f.read().strip()
except OSError:
return ""
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 key (keepalive file, env or master key)", file=sys.stderr)
sys.exit(1)
fail = 0
# TTS warm (voxtral-tts) — $0.002/request (Tinfoil request-priced)
req = urllib.request.Request(
f"http://127.0.0.1:{PORT}/v1/audio/speech",
data=json.dumps({"model": "tinfoil/voxtral-tts", "input": "ok",
"voice": "casual_female"}).encode(),
method="POST",
headers={"Authorization": f"Bearer {KEY}", "Content-Type": "application/json"})
try:
urllib.request.urlopen(req, timeout=30)
except Exception as e:
print(f"keepalive: TTS ping failed: {e}", file=sys.stderr)
fail = 1
# STT warm disabled 2026-10-11: $0.01/STT request × 6/hr ≈ $52/mo. Revisit only
# with a cheaper warm strategy.
sys.exit(fail)