You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DeepSeek provider: prompt_cache_miss_tokens not mapped to cache.write #35974
The DeepSeek API returns two cache fields in every response:
prompt_cache_hit_tokens — tokens served from disk KV cache
prompt_cache_miss_tokens — tokens that missed the cache
opencode correctly maps prompt_cache_hit_tokens → cache.read, but prompt_cache_miss_tokens is never recorded, resulting in cache.write: 0 for every single message.
Steps to reproduce
Use deepseek provider (any model)
Make multiple requests in the same session (multi-turn conversation)
Check stats: opencode stats --models
Check per-message data: opencode db "SELECT json_extract(data, '$.tokens.cache.read'), json_extract(data, '$.tokens.cache.write') FROM message WHERE json_extract(data, '$.role')='assistant'" --format tsv
Expected
cache.write should equal prompt_cache_miss_tokens from the DeepSeek API response, enabling calculation of cache hit rate as hit / (hit + miss).
Actual
cache.write is always 0 for every message. Cache read (hit) works fine.
Impact
Cannot calculate cache hit rate. The opencode stats display shows Cache Write: 0 which is misleading.
This is the same gap as #36749, in the same function, and the two are one patch rather than two.
On V2 (0.0.0-beta-17498) the DeepSeek route goes through the shared OpenAI Chat protocol — openai-compatible-chat.ts reuses OpenAIChat.protocol end to end — and the mapping reads cache
counts only from prompt_tokens_details:
DeepSeek reports prompt_cache_hit_tokens / prompt_cache_miss_tokens at the top level of usage
(per your report — I have no DeepSeek traffic to verify the wire shape myself), so neither key is
matched. #36749 hits the identical wall with a gateway sending cache_creation_input_tokens.
Both keys already survive decoding, because the usage struct carries a rest record:
So the fix is additive inside mapUsage — read the provider's own key names before falling back to
the subtraction. No schema change.
Worth flagging why nobody notices until the bill arrives: prompt_tokens is inclusive, and nonCached
is derived as prompt_tokens - (cached + cacheWrite). With cacheWrite undefined the miss tokens stay
inside nonCached and the totals still reconcile exactly, so cache.write: 0 is not accompanied by
any other symptom. Your requested hit / (hit + miss) ratio is unrecoverable from the stored message
for the same reason — providerMetadata is not persisted, so the raw counts are gone by then.
Thanks for the report! OpenCode 2.0 is now out and development is focused on V2, so we're closing issues filed against OpenCode 1. Please upgrade (opencode upgrade --major, details: https://anoma.ly/notes/opencode-2-0), and if this is still relevant on 2.0, reopen this issue or open a new one with your 2.0 version.
Activity
github-actions commentedon Jul 8, 2026
This issue might be a duplicate of or related to an existing issue. Please check:
tokens_cache_read— similarly touches cache token accounting for DeepSeek/compatible providers, though that issue focuses on cache read tokens rather thancache.write/prompt_cache_miss_tokens.Zaczero commentedon Aug 18, 2026
This is the same gap as #36749, in the same function, and the two are one patch rather than two.
On V2 (
0.0.0-beta-17498) the DeepSeek route goes through the shared OpenAI Chat protocol —openai-compatible-chat.tsreusesOpenAIChat.protocolend to end — and the mapping reads cachecounts only from
prompt_tokens_details:opencode/packages/ai/src/protocols/openai-chat.ts
Lines 582 to 600 in 39be159
DeepSeek reports
prompt_cache_hit_tokens/prompt_cache_miss_tokensat the top level ofusage(per your report — I have no DeepSeek traffic to verify the wire shape myself), so neither key is
matched. #36749 hits the identical wall with a gateway sending
cache_creation_input_tokens.Both keys already survive decoding, because the usage struct carries a rest record:
opencode/packages/ai/src/protocols/openai-chat.ts
Lines 155 to 181 in 39be159
So the fix is additive inside
mapUsage— read the provider's own key names before falling back tothe subtraction. No schema change.
Worth flagging why nobody notices until the bill arrives:
prompt_tokensis inclusive, andnonCachedis derived as
prompt_tokens - (cached + cacheWrite). WithcacheWriteundefined the miss tokens stayinside
nonCachedand the totals still reconcile exactly, socache.write: 0is not accompanied byany other symptom. Your requested
hit / (hit + miss)ratio is unrecoverable from the stored messagefor the same reason —
providerMetadatais not persisted, so the raw counts are gone by then.thdxr commentedon Oct 11, 2026
Thanks for the report! OpenCode 2.0 is now out and development is focused on V2, so we're closing issues filed against OpenCode 1. Please upgrade (
opencode upgrade --major, details: https://anoma.ly/notes/opencode-2-0), and if this is still relevant on 2.0, reopen this issue or open a new one with your 2.0 version.— from 𝕺𝖕𝖊𝖓𝕮𝖔𝖉𝖊