From aebdfe0b2eb599ea0de2dff9644aeacfcc9dbf90 Mon Sep 17 00:00:00 2001 From: giulio-leone Date: Fri, 13 Mar 2026 01:21:35 +0100 Subject: [PATCH] fix: recognize Bedrock inference profile ARNs for prompt caching The _cache_strategy property only checked for 'claude' or 'anthropic' substrings in the model_id. This works for system inference profile IDs (e.g. us.anthropic.claude-haiku-4-5-20251001-v1:0) but fails for application inference profile ARNs which have the form: arn:aws:bedrock:::application-inference-profile/ These ARNs don't contain 'claude' or 'anthropic', so automatic caching was silently disabled even when the user explicitly set cache_config=CacheConfig(strategy='auto'). The fix detects ARNs that contain 'inference-profile' and optimistically enables caching. Currently only Anthropic Claude models support prompt caching on Bedrock; for non-caching models the cache point is silently ignored by the Converse API. Closes #1705 --- src/strands/models/bedrock.py | 10 ++++++++++ tests/strands/models/test_bedrock.py | 22 +++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/strands/models/bedrock.py b/src/strands/models/bedrock.py index bab4031ed..bfad100b3 100644 --- a/src/strands/models/bedrock.py +++ b/src/strands/models/bedrock.py @@ -186,6 +186,16 @@ def _cache_strategy(self) -> str | None: model_id = self.config.get("model_id", "").lower() if "claude" in model_id or "anthropic" in model_id: return "anthropic" + + # Application / cross-region inference profile ARNs don't contain the + # foundation model name, so the substring check above misses them. + # When the user explicitly opted into caching via cache_config, we + # optimistically enable caching for inference-profile ARNs. Currently + # only Anthropic Claude models support prompt caching on Bedrock; for + # non-caching models, the cache point is silently ignored by the API. + if model_id.startswith("arn:") and "inference-profile" in model_id: + return "anthropic" + return None @override diff --git a/tests/strands/models/test_bedrock.py b/tests/strands/models/test_bedrock.py index 89c4df70d..41cd83d57 100644 --- a/tests/strands/models/test_bedrock.py +++ b/tests/strands/models/test_bedrock.py @@ -2597,7 +2597,27 @@ def test_cache_strategy_none_for_non_claude(bedrock_client): assert model._cache_strategy is None -def test_inject_cache_point_adds_to_last_user(bedrock_client): +def test_cache_strategy_anthropic_for_inference_profile_arn(bedrock_client): + """Test that _cache_strategy returns 'anthropic' for inference profile ARNs. + + Application inference profile ARNs don't contain 'claude' or 'anthropic', + but should still support caching when the user explicitly opts in. + + Reproduces: https://github.com/strands-agents/sdk-python/issues/1705 + """ + # Application inference profile ARN + model = BedrockModel( + model_id="arn:aws:bedrock:us-east-1:123456789012:application-inference-profile/abc123def456" + ) + assert model._cache_strategy == "anthropic" + + # Cross-region inference profile ARN + model2 = BedrockModel( + model_id="arn:aws:bedrock:us-east-1:123456789012:inference-profile/us.anthropic.claude-sonnet-4-20250514-v1:0" + ) + assert model2._cache_strategy == "anthropic" + + """Test that _inject_cache_point adds cache point to last user message.""" model = BedrockModel( model_id="us.anthropic.claude-sonnet-4-20250514-v1:0", cache_config=CacheConfig(strategy="auto")