Skip to content

Commit 016694a

Browse files
sergicalclaude
andcommitted
fix(provider): scope OpenRouter session_id to Anthropic and honor setCacheKey
session_id also pins the resolved model for OpenRouter router models such as Auto Router and Pareto Router, so sending it for every model changed routing semantics well outside the Anthropic caching bug in anomalyco#39009. Restrict it to the branch that needs it. setCacheKey: false is the established opt-out for provider cache and session keys, and the new assignment sat outside that guard. Caching itself stays on when opted out, since cache_control carries no session identity. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent be1476d commit 016694a

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

‎packages/opencode/src/provider/transform.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,12 +1255,13 @@ export function options(input: {
12551255
}
12561256
}
12571257

1258-
if (input.model.api.npm === "@openrouter/ai-sdk-provider") {
1259-
result["session_id"] = input.sessionID
1260-
// `~anthropic/*` are catalog aliases for the same models.
1261-
if (input.model.api.id.replace(/^~/, "").startsWith("anthropic/")) {
1262-
result["cache_control"] = { type: "ephemeral" }
1263-
}
1258+
// `~anthropic/*` are catalog aliases for the same models.
1259+
if (
1260+
input.model.api.npm === "@openrouter/ai-sdk-provider" &&
1261+
input.model.api.id.replace(/^~/, "").startsWith("anthropic/")
1262+
) {
1263+
result["cache_control"] = { type: "ephemeral" }
1264+
if (input.providerOptions?.setCacheKey !== false) result["session_id"] = input.sessionID
12641265
}
12651266

12661267
if (input.model.api.npm === "@ai-sdk/gateway") {

‎packages/opencode/test/provider/transform.test.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ describe("ProviderTransform.options - setCacheKey", () => {
305305
})
306306
}
307307

308-
test("should keep OpenRouter sticky routing but not caching for non-Anthropic models", () => {
308+
test("should leave non-Anthropic OpenRouter models untouched", () => {
309309
const result = ProviderTransform.options({
310310
model: {
311311
...mockModel,
@@ -315,10 +315,24 @@ describe("ProviderTransform.options - setCacheKey", () => {
315315
sessionID,
316316
providerOptions: {},
317317
})
318-
expect(result.session_id).toBe(sessionID)
319318
expect(result.cache_control).toBeUndefined()
319+
expect(result.session_id).toBeUndefined()
320320
expect(result.prompt_cache_key).toBeUndefined()
321321
})
322+
323+
test("should disable the OpenRouter session key but keep caching when opted out", () => {
324+
const result = ProviderTransform.options({
325+
model: {
326+
...mockModel,
327+
providerID: "openrouter",
328+
api: { ...mockModel.api, id: "anthropic/claude-opus-4.8", npm: "@openrouter/ai-sdk-provider" },
329+
},
330+
sessionID,
331+
providerOptions: { setCacheKey: false },
332+
})
333+
expect(result.cache_control).toEqual({ type: "ephemeral" })
334+
expect(result.session_id).toBeUndefined()
335+
})
322336
})
323337

324338
describe("ProviderTransform.options - zai/zhipuai thinking", () => {

0 commit comments

Comments
 (0)