Files
litellm/audio_keepalive.py
T
inference-admin 24dbd313cf keepalive: TTS warm ping every 30 min (measured, halves warm cost)
Idle sweep 2026-11-28 (5/10/15/20/30/45/60 min gaps through the live gateway): warm latency 0.38-0.99s through 45 minutes, no cold-start cliff, so the 15-minute ping was paying $5.76/mo for warmth already present. 30 min halves it to ~$2.88/mo and stays inside the proven-warm window; the 60-min trial was contaminated by member voice traffic at 34 min true idle, so 30 is the conservative bound. Comments in start.sh and README record why, so the interval is not 'tidied' back to 15.
2026-11-28 16:56:33 +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 30 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 30 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)