anomalyco / anomalyco/opencode

NanoGPT cache-write usage and billed cost are lost in session accounting

Open
#48,478 2 comments 0 reactions 1 assignee View on GitHub

@fwang is already working on this.

Since Sep 11, 2026.

Dominant language
TypeScript
Stars
209k
Forks
27.5k
PR merge metrics
PR metrics pending

Description

Description

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:

{
  "usage": {
    "prompt_tokens": 1000,
    "completion_tokens": 20,
    "total_tokens": 1020,
    "cache_creation_input_tokens": 800,
    "prompt_tokens_details": { "cached_tokens": 100 }
  },
  "x_nanogpt_pricing": { "amount": 0.0123, "currency": "USD" }
}

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.