Skip to content

fix(metadata): Some providers use requestbody(body.metadata.user_id) to enable cache features Fixes #8195#11276

Open
tisoz wants to merge 5 commits intoanomalyco:devfrom
tisoz:dev
Open

fix(metadata): Some providers use requestbody(body.metadata.user_id) to enable cache features Fixes #8195#11276
tisoz wants to merge 5 commits intoanomalyco:devfrom
tisoz:dev

Conversation

@tisoz
Copy link

@tisoz tisoz commented Jan 30, 2026

What does this PR do?

Fixes #8195

allow user add a options to enable cache features.

opencode.json

    "provider": {
        "anthropic": {
            "name": "fox.anthropic.claude",
            "options": {
                "baseURL": "https://code.newcli.com/claude/aws/v1",
                "apiKey": "sk-ant-xxxxx",
                "setCacheKey": true,   
                "enableMeta":true    // fix provider cache
            },
.....

How did you verify your code works?

clone the project and

bun run dev

opencode and others added 4 commits January 30, 2026 06:48
…ng sent back as assistant message content (anomalyco#11270)

Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
@github-actions
Copy link
Contributor

Hey! Your PR title Fix Some providers cache futures doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@github-actions
Copy link
Contributor

The following comment was made by an LLM, it may be inaccurate:

No duplicate PRs found

@tisoz tisoz changed the title Fix Some providers cache futures fix(metadata): Some providers use requestbody(body.metadata.user_id) to enable cache futures Jan 30, 2026
@github-actions
Copy link
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@tisoz tisoz changed the title fix(metadata): Some providers use requestbody(body.metadata.user_id) to enable cache futures fix(metadata): Some providers use requestbody(body.metadata.user_id) to enable cache features Jan 30, 2026
@tisoz tisoz changed the title fix(metadata): Some providers use requestbody(body.metadata.user_id) to enable cache features fix(metadata): Some providers use requestbody(body.metadata.user_id) to enable cache features Fixes #8195 Jan 30, 2026
])
if (provider.options?.enableMeta) {
input.model.options.metadata = {
user_id: `user_${Instance.project.id ?? "unknown"}_account__session_${input.sessionID}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the purpose of this combination is to simulate the behavior in the Claude Code CLI, which is why this approach is adopted

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I ran into this issue while using AWS

@tisoz
Copy link
Author

tisoz commented Jan 30, 2026

Change Description:

  1. llm.ts:
    Check if provider.options.enableMeta is enabled, and only then proceed with metadata information passing (due to incomplete context parameters in provider.ts)

  2. The core implementation of this feature requires manually enabling the provider.options.enableMeta configuration item to ensure third-party providers can properly implement the caching functionality for conversations.

  3. The request parameters originate from simulating the native parameters of the Claude Code CLI, so this is configured as an optional setting, defaulting to off. Regarding privacy, there should be no issues.

image

This submission is primarily aimed at reducing tool conversation costs. Previously, without caching features, contextual conversations consumed a large number of tokens, leaving no choice but to adopt this method to maintain compatibility with the model's functionality.

@rekram1-node
Copy link
Collaborator

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?

@tisoz
Copy link
Author

tisoz commented Feb 2, 2026

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?理想情况下所有功能都应在无需额外配置的情况下直接运行,听起来只有 Azure 和 Bedrock 需要这个设置?能否提供相关说明文档?

anthropic api docs:
https://platform.claude.com/docs/en/build-with-claude/prompt-caching

However, it should be noted that the documentation does not specify the exact function of metadata.user_id. I discovered through reverse engineering that claude code adds this parameter in requests.

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

@rekram1-node
Copy link
Collaborator

Oh if ur talking specifically about bedrock, there was a separate issue and has been addressed:
ca5e85d

@tisoz
Copy link
Author

tisoz commented Feb 2, 2026

Oh if ur talking specifically about bedrock, there was a separate issue and has been addressed:哦,如果你特指 Bedrock 的问题,之前确实有个独立的问题,现在已经解决了: ca5e85d

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

image

@rekram1-node
Copy link
Collaborator

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

@tisoz
Copy link
Author

tisoz commented Feb 2, 2026

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这确实解决了 Bedrock 的缓存问题,至少是直接解决了

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:
https://github.com/1034378361/foxcode-cache-proxy
https://github.com/[x7yue/opencode-foxcode-aws-cache](https://github.com/x7yue/opencode-foxcode-aws-cache)

So I think this should be a necessary feature for simulating the Claude CLI.

If the addition of the config.provider.options.enableMeta option is a carefully considered feature

I can consider modifying it into a more general form to allow users to add metadata themselves for reliable iteration of the caching functionality

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants