[FEATURE] Token Caching
Feature Description Handle token caching as codex cli does
Token usage: total=421,703 input=400,805 (+ 6,335,488 cached) output=20,898 (reasoning 11,136)
Use Case Lowers the token usage
Proposed Implementation Codex CLI does it, I am not sure how to do it.
Compliance Consideration Please confirm:
- [x] This feature is for personal development use
- [x] This feature does not violate or circumvent OpenAI's Terms of Service
- [x] This feature is not intended for commercial resale or multi-user access
Alternatives Considered Have you considered any alternative solutions or workarounds?
Yes, codex CLI but want to use opencode
Additional Context Add any other context, screenshots, or examples.
Discussed this with Codex providing it Codex CLI, opencode and this plugin
› How can caching be implemented in the plugin?
• To let the plugin benefit from Codex’s prompt caching, you’d need to change its architecture in a few places. Right now it deliberately makes every turn “stateless” ( store: false, IDs
removed, no cache key ). Here’s the work you’d have to do:
———
1. Preserve a stable cache key
- Generate or reuse an identifier per chat (e.g. the OpenCode conversation_id) and send it as prompt_cache_key.
- Keep that ID in the plugin’s in‑memory session state so it stays constant for future turns.
2. Keep the cacheable prefix identical
- Stop stripping all previous message IDs. Preserve a message history in the form Codex expects (same ordering, same content).
- When you need to inject the bridge prompt or filter system messages, do it once and store the result so the prefix is byte‑for‑byte the same on every call.
- Only append the new user/assistant items.
3. Manage “store” and reasoning items
- Consider switching to store: true (the CLI uses this for certain endpoints) or mirror the CLI logic for stateless caching so encrypted reasoning snippets are retained. You need Codex
to see the same prefix as before to get a cache hit.
4. Persist per-session metadata
- Maintain, in the plugin, the conversation transcript with the canonical IDs Codex returns (tools, reasoning, shell outputs, etc.).
- When Codex responds, merge its items back into your stored history (using IDs) so the next request replays exactly what it saw.
5. Handle defaults and fallbacks
- If you lose the cache key or need to branch (fork a conversation), generate a new key so you don’t pollute the old cache.
- On errors (e.g. the backend rejects cached content), drop back to stateless mode, clear the stored prefix, and start a fresh cache key.
———
Implementation outline
1. Introduce a session manager inside the plugin that tracks the ongoing conversation: cache key, stored prefix (instructions + environment), and the latest server items.
2. Update transformRequestBody() to pull the saved prefix, append the new turn’s items, and set prompt_cache_key.
3. After each response, update the stored prefix—ensuring you remove transient items (e.g. in-progress tool calls) and keep final IDs.
4. Provide a strategy for when the user starts a new OpenCode chat or resumes one (carry or generate a new cache key).
5. Instrument telemetry/logging so you can verify when cached_tokens > 0, confirming hits/misses.
6. Add tests that send two consecutive turns and assert the second request’s prefix matches the first and that prompt_cache_key stays constant.
This is a significant change—essentially re-creating the Codex CLI’s conversation manager inside the plugin. But without these steps (stable key, identical prefix, stored IDs), Codex
won’t reuse its cached prompts.
+1 but the scope is out of the this project, this is a plugin for opencode, a proxy is what is needed instead for this type of feature, but if the repo's op finds a way, that would be good as well.
I opened a pull request, it is a bit hacky though, so I left it as a draft and wanted to brainstorm better solutions. It works, but I am all ears on how to make it better. Gotta say though. Limits are WAY higher when token caching is working. It is a dream having codex in opencode, especially with higher limits. https://github.com/numman-ali/opencode-openai-codex-auth/pull/28
I have it working using my forks
https://github.com/shantur/CLIProxyAPI/commit/ace270891e96980974f34e656e522331e36f5327 and https://github.com/shantur/opencode/commit/10a0eb7211b7561968d5fd8c1511be4af8e17992
I have it working using my forks
Dang! I literally was about to open a pull request for the CLIProxyAPI as well! Seems like you beat me to it.
Hey @shantur, I'm using your commit https://github.com/shantur/opencode/commit/10a0eb7211b7561968d5fd8c1511be4af8e17992 on my own fork, thanks for that!
Now how can I validate the caching is working when using it with opencode-openai-codex-auth plugin? Apologies if this question is too dumb 😅 Right now I'm just looking into https://chatgpt.com/codex/settings/usage and comparing usage from memory
Awesome work guys!!
I'll review @Jordan-Jarvis PR and the other implementations
The key is to ensure no breaking changes:
- general chat usage
- all tool usage
- summarise command
- sub agent creation and usage
Also need to verify all of the options mapping per config is respected too
For this level of change, I would normally go for a logical branch (ie FF) where core logical is
I'd also like to snoop the HTTP requests to understand all headers sent back and forth to ensure it works as expected
Might be a day or two before I get the chance to put up a new PR that covers all of this
It's coming tonight boys and girls
Running final validations
Thanks for pushing me on it
Makes such a difference!
Merged and release https://github.com/numman-ali/opencode-openai-codex-auth/pull/32
Make sure to use the updated config json in readme, and run the update command in readme