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
NanoGPT cache-write usage and billed cost are lost in session accounting #48478
The nano-gpt integration loses cache-write usage and recomputes an estimated cost even when NanoGPT returns the settled USD charge. The OpenAI-compatible SDK does not map usage.cache_creation_input_tokens into its normalized usage, and OpenCode does not consume x_nanogpt_pricing.
This can display zero cache writes and a cost that differs from the charge when cache-write rates, long-context pricing, or discounts apply. Cache writes should remain distinct from cache reads. A valid reported USD cost should take precedence over the estimate, including an explicit zero; otherwise the existing estimate should remain.
OpenCode version
Reported in desktop v1.18.30; reproduced through the SDK and session accounting tests on dev commit 193de13.
Steps to reproduce
Use the nano-gpt provider with @ai-sdk/openai-compatible. Feed its SDK a synthetic completion with:
Observe session accounting: the cache-write count is lost and the cost uses catalog rates. Expected: 100 ordinary input tokens, 100 cache-read tokens, 800 cache-write tokens, and cost $0.0123. The same issue affects streaming usage frames.
Local regression tests and a fix are in #48477. No live inference is required for the reproduction.
#37745 overlaps with the missing cache-write count. #35974 concerns DeepSeek’s prompt_cache_miss_tokens, whereas this report concerns NanoGPT’s explicit usage.cache_creation_input_tokens. #34022 concerns missing cache reads; our reproduction preserves reads but loses writes.
The additional issue here is that OpenCode ignores NanoGPT’s settled USD charge in x_nanogpt_pricing and recomputes an estimate from catalog rates. Fixing cache-write mapping alone would leave that discrepancy unresolved.
#48477 includes a proposed fix and regression tests for both aspects, including streaming. Happy to consolidate the cache-write portion if preferred, while retaining coverage of the reported-cost handling.
Thanks for the detailed report. I can reproduce this on the latest dev (1e7ba46).
I replayed your usage payload as a streamed chat-completions response through OpenCode's OpenAI-compatible protocol (packages/ai), with the provider set to nano-gpt. The parsed usage came back as:
input 1000, cache read 100: correct
cache write: missing, expected 800
non-cached input: 900, expected 100, so the cache-write tokens are counted as ordinary input
cost: missing, so session accounting falls back to the catalog estimate instead of the reported $0.0123
The cause is in mapUsage in packages/ai/src/protocols/openai-chat.ts. It reads cache writes only from prompt_tokens_details.cache_write_tokens and cache_created_input_tokens, never from the top-level cache_creation_input_tokens. Nothing reads x_nanogpt_pricing.
The test below reproduces it from packages/ai/test/. I only tested the streaming path. A related gap is the TODO for #35765 in packages/core/src/session/usage.ts, which is about using Copilot's reported billed amount in the same way.
Activity
github-actions commentedon Sep 11, 2026
This issue might be a duplicate of existing issues. Please check:
The NanoGPT-specific
x_nanogpt_pricingcost override aspect may be unique to this report, but the cache-write token loss overlaps with the above.huggix commentedon Sep 14, 2026
Thanks—checked all three.
#37745 overlaps with the missing cache-write count. #35974 concerns DeepSeek’s
prompt_cache_miss_tokens, whereas this report concerns NanoGPT’s explicitusage.cache_creation_input_tokens. #34022 concerns missing cache reads; our reproduction preserves reads but loses writes.The additional issue here is that OpenCode ignores NanoGPT’s settled USD charge in
x_nanogpt_pricingand recomputes an estimate from catalog rates. Fixing cache-write mapping alone would leave that discrepancy unresolved.#48477 includes a proposed fix and regression tests for both aspects, including streaming. Happy to consolidate the cache-write portion if preferred, while retaining coverage of the reported-cost handling.
opencodejr commentedon Oct 28, 2026
Thanks for the detailed report. I can reproduce this on the latest
dev(1e7ba46).I replayed your usage payload as a streamed chat-completions response through OpenCode's OpenAI-compatible protocol (
packages/ai), with the provider set tonano-gpt. The parsed usage came back as:The cause is in
mapUsageinpackages/ai/src/protocols/openai-chat.ts. It reads cache writes only fromprompt_tokens_details.cache_write_tokensandcache_created_input_tokens, never from the top-levelcache_creation_input_tokens. Nothing readsx_nanogpt_pricing.The test below reproduces it from
packages/ai/test/. I only tested the streaming path. A related gap is the TODO for #35765 inpackages/core/src/session/usage.ts, which is about using Copilot's reported billed amount in the same way.Reproduction script (nanogpt-repro.test.ts)
Run it:
bun nanogpt-repro.test.ts