fix(providers): support Bedrock Anthropic cacheRetention defaults/pass-through (#22303) (thanks @snese)

This commit is contained in:
Peter Steinberger
2026-02-23 18:17:50 +00:00
parent ca5c0bc02b
commit be6f0b8c84
5 changed files with 111 additions and 7 deletions

View File

@@ -187,6 +187,37 @@ describe("applyExtraParamsToAgent", () => {
expect(calls[0]?.cacheRetention).toBeUndefined();
});
it("passes through explicit cacheRetention for Anthropic Bedrock models", () => {
const { calls, agent } = createOptionsCaptureAgent();
const cfg = {
agents: {
defaults: {
models: {
"amazon-bedrock/us.anthropic.claude-opus-4-6-v1": {
params: {
cacheRetention: "long",
},
},
},
},
},
};
applyExtraParamsToAgent(agent, cfg, "amazon-bedrock", "us.anthropic.claude-opus-4-6-v1");
const model = {
api: "openai-completions",
provider: "amazon-bedrock",
id: "us.anthropic.claude-opus-4-6-v1",
} as Model<"openai-completions">;
const context: Context = { messages: [] };
void agent.streamFn?.(model, context, {});
expect(calls).toHaveLength(1);
expect(calls[0]?.cacheRetention).toBe("long");
});
it("adds Anthropic 1M beta header when context1m is enabled for Opus/Sonnet", () => {
const { calls, agent } = createOptionsCaptureAgent();
const cfg = buildAnthropicModelConfig("anthropic/claude-opus-4-6", { context1m: true });

View File

@@ -43,16 +43,25 @@ type CacheRetentionStreamOptions = Partial<SimpleStreamOptions> & {
*
* Mapping: "5m" → "short", "1h" → "long"
*
* Only applies to Anthropic provider (OpenRouter uses openai-completions API
* with hardcoded cache_control, not the cacheRetention stream option).
* Applies to:
* - direct Anthropic provider
* - Anthropic Claude models on Bedrock when cache retention is explicitly configured
*
* Defaults to "short" for Anthropic provider when not explicitly configured.
* OpenRouter uses openai-completions API with hardcoded cache_control instead
* of the cacheRetention stream option.
*
* Defaults to "short" for direct Anthropic when not explicitly configured.
*/
function resolveCacheRetention(
extraParams: Record<string, unknown> | undefined,
provider: string,
): CacheRetention | undefined {
if (provider !== "anthropic") {
const isAnthropicDirect = provider === "anthropic";
const hasBedrockOverride =
extraParams?.cacheRetention !== undefined || extraParams?.cacheControlTtl !== undefined;
const isAnthropicBedrock = provider === "amazon-bedrock" && hasBedrockOverride;
if (!isAnthropicDirect && !isAnthropicBedrock) {
return undefined;
}
@@ -71,7 +80,13 @@ function resolveCacheRetention(
return "long";
}
// Default to "short" for Anthropic when not explicitly configured
// Default to "short" only for direct Anthropic when not explicitly configured.
// Bedrock retains upstream provider defaults unless explicitly set.
if (!isAnthropicDirect) {
return undefined;
}
// Default to "short" for direct Anthropic when not explicitly configured
return "short";
}