Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions hindsight-api-slim/hindsight_api/engine/providers/codex_llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
"""

import asyncio
import functools
import hashlib
import json
import logging
import time
import uuid
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -155,6 +156,20 @@ def _auth_file(self) -> Path:
def _auth_file(self, v: Path) -> None:
self._auth_manager._auth_file = v

@functools.cached_property
def _prompt_cache_key(self) -> str:
"""Stable ``prompt_cache_key`` for OpenAI/Codex prompt-cache routing.

OpenAI uses ``prompt_cache_key`` as an explicit hint for routing
requests to a cached-prefix backend, so it must stay constant across
calls that share the same system instructions + tools prefix. A fresh
``uuid4`` per request defeats that routing and forces a 100% cache miss.
Derive it once per provider instance from (account, model) so it is
stable across requests but still distinct per account/model.
"""
seed = f"{self.account_id}:{self.model}"
return hashlib.sha256(seed.encode("utf-8")).hexdigest()[:32]

# ------------------------------------------------------------------
# Forwarding methods (keep surface area for tests / subclasses)
# ------------------------------------------------------------------
Expand Down Expand Up @@ -388,7 +403,7 @@ async def call(
"store": False, # Codex uses stateless mode
"stream": True, # SSE streaming
"include": ["reasoning.encrypted_content"],
"prompt_cache_key": str(uuid.uuid4()),
"prompt_cache_key": self._prompt_cache_key,
}

headers = {
Expand Down Expand Up @@ -711,7 +726,7 @@ async def call_with_tools(
"store": False,
"stream": True,
"include": ["reasoning.encrypted_content"],
"prompt_cache_key": str(uuid.uuid4()),
"prompt_cache_key": self._prompt_cache_key,
}

headers = {
Expand Down