fix(metadata): Some providers use requestbody(body.metadata.user_id) to enable cache features Fixes #8195#11276
fix(metadata): Some providers use requestbody(body.metadata.user_id) to enable cache features Fixes #8195#11276tisoz wants to merge 5 commits intoanomalyco:devfrom
Conversation
…ons handling for metadata
…ng sent back as assistant message content (anomalyco#11270) Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
|
Hey! Your PR title Please update it to start with one of:
Where See CONTRIBUTING.md for details. |
|
The following comment was made by an LLM, it may be inaccurate: No duplicate PRs found |
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
packages/opencode/src/session/llm.ts
Outdated
| ]) | ||
| if (provider.options?.enableMeta) { | ||
| input.model.options.metadata = { | ||
| user_id: `user_${Instance.project.id ?? "unknown"}_account__session_${input.sessionID}`, |
There was a problem hiding this comment.
user_session_id would be more clear. or, ideally, let's make user_id just be user_id and concatenate our values at the site of use.
There was a problem hiding this comment.
Currently, the purpose of this combination is to simulate the behavior in the Claude Code CLI, which is why this approach is adopted
There was a problem hiding this comment.
If it's a variable name, it could indeed be optimized to user_session_id to express the meaning more clearly
| // Inject metadata from model.options if enableMeta is set | ||
| if (provider.options?.enableMeta && opts.body && opts.method === "POST") { | ||
| try { | ||
| options.headers["x-opencode-session"] = model.options.metadata.user_id |
There was a problem hiding this comment.
Perhaps "the train has already left the station", but I would prefer not adding opencode specific headers that give away where my traffic is coming from like this. Also, should we be using a metadata
There was a problem hiding this comment.
This addresses the issue where many model providers cannot properly enable caching functionality. I attempted to add parameters like user_id in the metadata.
However, the model providers cannot recognize them correctly, including AWS Claude Code and several third-party provided Claude models.
There was a problem hiding this comment.
Perhaps "the train has already left the station", but I would prefer not adding opencode specific headers that give away where my traffic is coming from like this. Also, should we be using a metadata
I think some API endpoint like AWS bedrock/Azure are alao require this fix to get better prompt cache support
There was a problem hiding this comment.
yeah, I ran into this issue while using AWS
00637c0 to
71e0ba2
Compare
f1ae801 to
08fa7f7
Compare
|
ideally we make everything just work without additional configs, it sounds like azure and bedrock are the ones that require this? Can you point to any docs that show it? |
anthropic api docs: However, it should be noted that the documentation does not specify the exact function of I then enabled this parameter in opencode and tested it on the sonnet 4.5 and opus 4.5 models, which successfully implemented the caching functionality. It might be that AWS has special caching identification methods for these models, but clearly, we all need to enable this feature to reduce our token input and output |
|
Oh if ur talking specifically about bedrock, there was a separate issue and has been addressed: |
I noticed this commit, but it's not based on the same principle as our current cache mechanism identification. I tried simulating these parameters, but the third-party endpoint I'm using didn't successfully enable it
|
Well if you are using a third party endpoint then it is separate, could u provide docs for ur other provider instead? I don't wanna add random headers and body fields unless we can have good reason for it. This did fix caching for bedrock, at least directly |
Currently, because the platform's scale is not that large, only a small number of developers will notice this issue. They offer relatively low-cost endpoint channels, but they lack the capability to provide complete documentation. Therefore, I only have some open-source projects from fellow enthusiasts to address this issue. Next, I will provide some open-source projects for your reference: So I think this should be a necessary feature for simulating the Claude CLI. If the addition of the I can consider modifying it into a more general form to allow users to add metadata themselves for reliable iteration of the caching functionality |


What does this PR do?
Fixes #8195
allow user add a options to enable cache features.
opencode.json
How did you verify your code works?
clone the project and