MCPcopy Create free account
hub / github.com/CommonstackAI/UncommonRoute / _stable_prompt_cache_key

Function _stable_prompt_cache_key

uncommon_route/cache_support.py:447–470  ·  view source on GitHub ↗
(
    body: dict[str, Any],
    *,
    model: str,
    session_id: str | None,
    step_type: str,
)

Source from the content-addressed store, hash-verified

445
446
447def _stable_prompt_cache_key(
448 body: dict[str, Any],
449 *,
450 model: str,
451 session_id: str | None,
452 step_type: str,
453) -> str:
454 seed_parts = [model, session_id or "stateless", step_type]
455 tools = body.get("tools") or body.get("customTools") or []
456 if tools:
457 try:
458 seed_parts.append(json.dumps(tools, sort_keys=True, ensure_ascii=False, separators=(",", ":")))
459 except TypeError:
460 seed_parts.append(str(tools))
461 system_parts: list[str] = []
462 for msg in body.get("messages") or []:
463 if not isinstance(msg, dict):
464 continue
465 if msg.get("role") == "system":
466 system_parts.append(_message_text(msg))
467 if system_parts:
468 seed_parts.append("\n".join(system_parts)[:4096])
469 digest = hashlib.sha256("\n".join(seed_parts).encode("utf-8")).hexdigest()[:24]
470 return f"ur:{digest}"
471
472
473def _is_openai_cache_retention_model(model: str) -> bool:

Callers 1

apply_openai_cache_hintsFunction · 0.85

Calls 3

_message_textFunction · 0.70
getMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected