Skip to content

[FEATURE]: Set prompt_cache_ttl for OpenRouter provider #16848

@rndmcnlly

Description

@rndmcnlly

[FEATURE]: Set prompt_cache_ttl for OpenRouter provider

Feature hasn't been suggested before.

  • I have verified this feature I'm about to request hasn't been suggested before.

(Note: #5416 and #5422 cover broader caching config. This is about one missing parameter on an existing code path.)

Describe the enhancement you want to request

We already set prompt_cache_key for OpenRouter in ProviderTransform.options() (packages/opencode/src/provider/transform.ts, line 790), but we don't set a TTL. OpenRouter defaults to 5 minutes.

Without a longer TTL, any idle gap over 5 minutes between prompts triggers a full cache miss and rewrite on the next turn. For Anthropic models this is common in real usage — reviewing output, editing files, thinking, etc. The 5-minute default ends up costing more overall because the cache is constantly being rewritten.

Caution

The fix originally sketched below is broken. The top-level prompt_cache_ttl parameter is silently ignored by OpenRouter — no error, no cache write, nothing. See the conclusive empirical research in #16850, including a 3-way verification script that tested with a 6-minute delay past the default TTL.

The fix is one line next to the existing prompt_cache_key assignment:

// BROKEN — silently ignored by OpenRouter
if (input.model.providerID === "openrouter") {
  result["prompt_cache_key"] = input.sessionID
  result["prompt_cache_ttl"] = 3600 // does nothing
}

The correct documented mechanism is cache_control: { type: "ephemeral", ttl: "1h" } on message content (OpenRouter docs). This is a one-line change in the existing applyCaching function. The fix is implemented and tested in #16850.

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions