fix: restore Anthropic prompt caching - #1
Open
luisherranz wants to merge 1 commit into
Open
Conversation
|
this is highly needed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Important
AI disclosure: All changes in this PR, including the implementation, tests, commit message, and this PR description, were generated entirely by GPT-5.6 Sol. I, Luis Herranz, have no idea what was changed or why. The technical explanation below is also AI-generated, so please treat the contribution as untrusted code requiring careful maintainer review.
Summary
This PR restores Anthropic prompt caching for OAuth requests routed through the plugin's custom AI SDK provider.
It adds a bounded default cache policy when the caller did not supply one, preserves explicit cache policies and TTLs, and ensures transient session-warming requests refresh history that subsequent real prompts can reuse.
Problem
The plugin replaces OpenCode's native Anthropic route with:
aisdk:file://.../dist/provider.jsThat sends requests through OpenCode's generic AI SDK adapter rather than its native Anthropic protocol.
In this path:
transformBodyreceived requests withoutcache_controlmarkers and did not add any.This was particularly expensive with session warming enabled because each warm request resent the complete context every four minutes without refreshing a reusable cache entry.
Solution
When the input has no existing prompt-cache policy,
transformBodynow adds at most four ephemeral cache boundaries after completing the OAuth rewrites:The reusable history boundary is important for warming. A warm request has a transient user suffix that will not appear in the next real prompt. Caching only that tail would refresh an entry the next prompt could not reuse. The additional history boundary lets warming refresh the shared conversation prefix independently of its temporary suffix.
The implementation also:
ttl: "1h".nullcache policies as absent.Testing
make allpasses:Regression coverage includes:
Manual OAuth verification against Anthropic also succeeded:
Checklist
make allpasses locally