Перейти к содержимому

Падение hit-rate prompt-cache

Prompt-cache работает только при бит-в-бит идентичном префиксе. Один лишний пробел, новая дата в системном промпте, новая версия инструмента в MCP-tools — и весь префикс пересчитывается с нуля. Анализируем cached_tokens / total_tokens из ответа:

import os, time, statistics, requests, json
W = "/tmp/cache-hit.json"
def observe(usage):
cached = (usage.cache_read_input_tokens or 0)
total = usage.input_tokens or 1
ratio = cached / total
s = (json.load(open(W)) if os.path.exists(W) else {"r": []})
s["r"] = (s["r"] + [ratio])[-300:]
json.dump(s, open(W, "w"))
if len(s["r"]) >= 100:
old, new = statistics.mean(s["r"][:50]), statistics.mean(s["r"][-50:])
if old > 0.3 and new < old / 2:
push("❄️ prompt-cache hit упал",
f"Было: {int(old*100)}% → стало: {int(new*100)}%\n"
"Проверьте, не вставили ли в системный промпт переменную (дата, request-id).",
priority=7)
def push(t, m, p):
requests.post(f"{os.environ['NOTIFLY_URL']}/message",
params={"token": os.environ["NOTIFLY_TOKEN"]},
json={"title": t, "message": m, "priority": p}, timeout=5)

Топ-3 причины падения hit-rate (по опыту):

  1. в системном промпте появился time.now().isoformat() или request-id;
  2. MCP-сервер выдал инструменты в другом порядке;
  3. история чата начала включать timestamp каждого сообщения.