opencode icon indicating copy to clipboard operation
opencode copied to clipboard

Claude Agent: Opus 5.5 shows 3.9x lower cache reuse than native Claude Code

Open christian-taillon opened this issue 1 month ago • 4 comments

Description

Observed Behavior

Tokscale reports significantly worse prompt-cache efficiency using Opus 5.5 through the new Claude Agent provider.

Native Claude Code

  • CacheX: 63.3x
  • Estimated cost/1M tokens: $0.39

OpenCode (Claude Agent)

  • CacheX: 16.2x
  • Estimated cost/1M tokens: $0.78

Difference: 3.9x lower cache reuse and 2x higher estimated cost in OpenCode.

CacheX = cache_read / (input + cache_write).

These are aggregate development-session measurements, not a controlled A/B test.

Potential areas to investigate:

  • SDK session resume and prompt-cache invalidation
  • Changes to system prompts or tool definitions
  • Cache TTL and process lifecycle
  • Context compaction behavior

Related:Anthropic #99833, OpenCode #61020.

Could we compare per-request cache-read/write statistics between OpenCode's Claude Agent provider and native Claude Code to isolate the difference?

Plugins

No response

OpenCode version

No response

Steps to reproduce

No response

Screenshot

No response

Operating System

No response

Terminal

No response

christian-taillon avatar Nov 02 '26 19:11 christian-taillon

Thanks for the report and the numbers! Since they come from different sessions, it's hard to tell whether OpenCode breaks the prompt cache or whether the workloads were just different. Could you share:

  1. Versions: opencode --version and claude --version, plus your OS and how you installed OpenCode.
  2. Setup: which plugins and MCP servers you have enabled, and whether you used subagents.
  3. Compaction: whether the OpenCode sessions were long enough to compact. With Claude Agent, compaction can start at about 170K tokens (see #61020), and each compaction has to write the cache again.
  4. A small matched comparison, if possible: the same short task in a fresh session in both tools, with the per-request input / cache_write / cache_read numbers. Even 5–10 turns would help a lot. For OpenCode, the per-message token usage in the session (e.g. from opencode export <sessionID>) is enough.

You can reply here with the details or edit the issue. If you edit it, please also leave a comment saying you've updated it so we know you're done.

Related issues, ordered from most to least likely to be relevant:

  • #61020: Claude Agent models fall back to a 200K context window, so compaction starts early and the cache has to be rebuilt more often
  • #61248: on claude-agent, text that plugins add to the system prompt never reaches Claude, so OpenCode's system prompt differs from Claude Code's
  • #51109: feature request to make the prompt-cache TTL configurable

opencodejr[bot] avatar Nov 02 '26 19:11 opencodejr[bot]

Hi @christian-taillon @opencodejr,

We recently investigated the exact wire-level token prefix divergence between OpenCode and native Claude Code and built an open-source, zero-dependency profiler (agent-cache-lens) to isolate the root causes behind this 3.9x CacheX drop.

Here is the architectural breakdown and per-turn findings isolating why OpenCode's prompt cache prefix invalidates:

1. Primary Root Causes Identified

  1. Premature Context Compaction (#61020):

    • Claude Code models support 1M context windows, but OpenCode's stored catalog frequently falls back to 200,000.
    • With RESERVE_SHARE = 0.15, compaction triggers prematurely near 170k tokens instead of ~850k tokens, truncating history and forcing a complete cache rewrite (paying full write price on the entire context).
  2. System Instruction Prefix Invalidation via Plugin Hooks (#61248):

    • Any plugin appending dynamic metadata (e.g. dynamic timestamps, working directories, active Git branches) to event.system shifts the byte-for-byte SHA-256 hash at offset 0.
    • Because Anthropic Prompt Caching hashes the strictly serialized prefix prior to ephemeral breakpoints, modifying the system prompt destroys cache retention for all subsequent turns.
  3. Tool Parameter Dictionary Key Drift:

    • Native Claude Code guarantees deterministic recursive key sorting for all declared MCP / built-in tools. In frameworks where JSON serialization doesn't enforce strict key canonicalization, server reconnections invalidate the prompt cache prefix hash.

2. Side-by-Side Controlled A/B Profile

We ran a controlled 3-turn trace comparing identical tool-use workloads:

Metric Native Claude Code OpenCode (Claude Agent) Delta / Gap
CacheX (cache_read / write+input) 1.82x 0.0x 100% Cache Miss in Target
Cache Hit Rate 64.5% 0.0% -64.5%
Cache Read Tokens 17,420 0 -
Cache Write Tokens 9,300 26,820 +17,520
Total Incurred Cost $0.0611 $0.1745 +185% cost inflation

3. Tooling for Verification

If anyone wants to isolate where their own session diverges, you can pipe your session export through agent-cache-lens:

# Pinpoint exact byte/character where the prefix hash broke
agent-cache-lens analyze <opencode_session_export.json>

# Or run the live proxy sniffer to see turn-by-turn cache retention in real-time:
agent-cache-lens proxy --port 8080 --upstream https://api.anthropic.com

Hope this forensic data helps isolate the fix for #61282!

Prognosticate-X avatar Nov 03 '26 05:11 Prognosticate-X

Closing this for now because we haven't heard back with the details we asked for. Without the OpenCode and Claude Code versions, your setup, and a matched per-request comparison of input / cache_write / cache_read, we can't tell whether OpenCode is breaking the prompt cache or the sessions just had different workloads.

If you can share those details, please reply here or open a new issue and we'll take another look. Early compaction on Claude Agent may account for part of the gap; that's tracked in #61020. Thanks for the report!

opencodejr[bot] avatar Nov 05 '26 19:11 opencodejr[bot]

Following up as the issue author with the controlled measurements requested when this was closed.

Environment

  • OpenCode 2.3.2, native binary. Claude Code 2.1.330, native.
  • Fedora Linux 44, x86_64.
  • Model opus / claude-agent/opus, resolved to claude-opus-5-5 on every measured request. Effort low on both.
  • Same Claude.ai subscription login through the installed claude binary. No API key and no proxy.
  • Context window reported for this model: 1,000,000. No compaction. Peak context in these trials was about 39k tokens.

Method

Three paired trials, order alternated. Each trial is a fresh session, then 8 identical prompts in one persistent session, on identical copies of a deterministic fixture. Usage is deduped by assistant message id from the Claude session JSONL. OpenCode step_finish tokens matched those rows exactly. Result totals were not added on top. Every cache write was the 1-hour tier. Estimated dollars use Opus 5.5 list rates and are not subscription spend.

Baseline results

Trial Claude Code CacheX OpenCode CacheX CC est. API OC est. API
1 23.60 17.71 $0.112 $0.291
2 23.66 17.78 $0.112 $0.286
3 23.08 17.75 $0.116 $0.288
Mean 23.45 17.75 $0.113 $0.288

Uncached input was 44 tokens in every trial. Mean cache-read share was 95.9% for Claude Code and 94.7% for OpenCode. Each follow-up request reread the previous request's prefix (within 4 tokens). There were no full misses, no shape-hash changes, and no mid-trial process restart.

Is the original gap reproducible?

No. The unmatched 63.3x versus 16.2x figures are not reproduced. The matched ratio here is 1.32x CacheX and about 2.5x estimated API cost. Claude Code's controlled CacheX was about 23, not 63. OpenCode's 17.7 is near the old 16.2 figure, but the per-request records show the prefix being reused.

The cost difference has a measured cause. OpenCode's grep returned about 51,000 characters for CACHE_EPOCH; that suffix was written once (18,100 tokens at the 1-hour rate) and then read back. Claude Code's grep result for the same prompt was under about 1,200 bytes. That is a tool-result difference, not a lost prompt prefix.

A user-config pair was only partly comparable. The running OpenCode service could not resolve claude and failed before a model call. An isolated server loading a copy of the user config, still pinned to low effort, scored CacheX 12.35 versus 12.87 for Claude Code with user settings enabled. Follow-up prefix reuse still held. I am not treating that as the live service.

Related

  • #61020: not observed for claude-opus-5-5 in these runs. Some older catalog ids still showed 200k in the live catalog; they were not used.
  • #63871: not exercised. There was no cross-model catch-up.
  • #61248: not isolated. User plugins were present only in the copied-config run, together with the default agent prompt.

I am not asking to reopen this for prefix invalidation. The remaining cost gap is explained by the grep result size above.

Reproduction, in short: one persistent claude -p --input-format stream-json --output-format stream-json --verbose --model opus --effort low session, and one isolated opencode serve with opencode run --format json --model claude-agent/opus#low continued by --session. Deduplicate the session JSONL by assistant message id and do not sum result.usage. Raw transcripts stay local because they contain tool output and local paths. I can provide a sanitized per-request table if useful.

christian-taillon avatar Nov 21 '26 04:11 christian-taillon