anomalyco / anomalyco/opencode

[FEATURE]: Enable OpenAI prompt caching by setting `prompt_cache_key` for GPT-5.6+

Open
#43,689 2 comments 1 reaction 1 assignee View on GitHub

@jlongster is already working on this.

Since Aug 20, 2026.

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

Description

Problem

OpenAI GPT-5.6+ supports prompt caching, but it requires setting prompt_cache_key in API requests for reliable cache matching. Without this, users pay full input token costs on every message, even in long sessions where the context is largely unchanged.

Real-world impact: In a 1 hour window of a 32-hour session using GPT-5.6, I spent ~$700. The session data shows cache_read: null and cache_write: null for all OpenAI messages, meaning zero caching occurred. Switching to Claude mid-session immediately showed cache hits (173M tokens read from cache) because Anthropic's caching works automatically.

OpenAI's Caching Requirements

From OpenAI's docs:

For GPT-5.6, you must set prompt_cache_key to use the more reliable matching for both implicit and explicit caching. At each breakpoint, the service matches the key with the exact prompt prefix. Without a key, requests may still receive automatic cache hits, but they do not use the improved matching.

Key parameters:

  • prompt_cache_key: String identifier (e.g., session ID) - required for reliable caching
  • prompt_cache_options.mode: "implicit" (default) or "explicit"
  • prompt_cache_options.ttl: Cache lifetime (default "30m")

Current State

OpenCode already:

  • ✅ Tracks cache tokens from OpenAI responses (cached_tokens, cache_write_tokens)
  • ✅ Has a cacheKey option in the provider interface
  • ❌ Does NOT pass prompt_cache_key to OpenAI API requests

Proposed Solution

Set prompt_cache_key to the session ID for OpenAI GPT-5.6+ requests:

{
  "model": "gpt-5.6",
  "prompt_cache_key": "ses_abc123...",
  "messages": [...]
}

This would:

  1. Enable reliable prompt caching for long sessions
  2. Dramatically reduce costs (cached tokens are billed at 0.1× the uncached rate)
  3. Reduce latency for repeated context

Configuration Options

Could be exposed as a provider-level or global config option:

{
  "provider": {
    "openai": {
      "promptCache": true  // or "auto" | "off"
    }
  }
}

Or enabled by default for supported models (GPT-5.6+).

Workaround

Currently none. The opencode-cache-injector plugin exists but only works for Claude API proxies, not native OpenAI.

Environment

  • OpenCode version: latest
  • Models affected: GPT-5.6, GPT-5.5, GPT-5.4, GPT-5.2, GPT-5.1, GPT-5, GPT-4.1 (all models supporting prompt_cache_key)

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.