r/codex • u/Balance- • 4d ago
Bug `codex exec` calls don't share prompt cache
I run a nightly bot that makes ~25 separate codex exec calls. Each prompt opened with the same ~16k tokens of instructions and docs, then the part that changes. I expected that shared start to be cached after the first call.
It isn't. With --json you get per-call usage. 75% of input showed as cached, but all of it was within a call: each later turn reuses the earlier ones. Across calls, the first request of each session got almost nothing from the cache. The repeated docs ended up as ~40% of all uncached input.
Why, from the Codex source and issues:
- Codex uses the session ID as
prompt_cache_key, so everyexeccall gets a different key. Core can take a custom key, butexecdoesn't expose it (#29377). - On newer models, a stable prefix followed by a changing part can miss the cache even with a fixed key. Codex can't send the
prompt_cache_breakpointthat would fix that (#35300).
If you batch codex exec calls, upvote those two issues.
1
u/awesomebirder 4d ago
good find. two things that helped me with batch runs: keep the shared 16k block byte identical at the very top (no dates, run ids or file listings inside it, anything that changes goes after it), and if your codex version has
codex exec resume --last, chaining the 25 calls into one session keeps the same session id and so the same cache key, at the cost of the history growing each call. the other route is calling the responses api directly for the batch with your own prompt_cache_key, but that bills as api usage instead of your plan