Skip to content

Commit 8298e9f

Browse files
authored
Fix: Non-Claude models cannot be used in AgentCore (#1347)
Co-authored-by: keykbd <keykbd@users.noreply.github.com>
1 parent 0db8ab7 commit 8298e9f

File tree

4 files changed

+60
-11
lines changed

4 files changed

+60
-11
lines changed

packages/cdk/lambda-python/generic-agent-core-runtime/src/agent.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from strands import Agent as StrandsAgent
1010
from strands.models import BedrockModel
1111

12-
from .config import extract_model_info, get_max_iterations, get_system_prompt
12+
from .config import extract_model_info, get_max_iterations, get_system_prompt, supports_prompt_cache, supports_tools_cache
1313
from .tools import ToolManager
1414
from .types import Message, ModelInfo
1515
from .utils import (
@@ -81,12 +81,21 @@ async def process_request_streaming(
8181

8282
# Create boto3 session and Bedrock model
8383
session = boto3.Session(region_name=region)
84-
bedrock_model = BedrockModel(
85-
model_id=model_id,
86-
boto_session=session,
87-
cache_prompt="default",
88-
cache_tools="default",
89-
)
84+
85+
# Configure caching based on model support (loaded from environment variable)
86+
bedrock_model_params = {
87+
"model_id": model_id,
88+
"boto_session": session,
89+
}
90+
91+
# Only enable caching for officially supported models
92+
if supports_prompt_cache(model_id):
93+
bedrock_model_params["cache_prompt"] = "default"
94+
95+
if supports_tools_cache(model_id):
96+
bedrock_model_params["cache_tools"] = "default"
97+
98+
bedrock_model = BedrockModel(**bedrock_model_params)
9099

91100
# Process messages and prompt using utility functions
92101
processed_messages = process_messages(messages)

packages/cdk/lambda-python/generic-agent-core-runtime/src/config.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Configuration and environment setup for the agent core runtime."""
22

3+
import json
34
import logging
45
import os
6+
import re
57
from typing import Any
68

79
# Configure root logger
@@ -82,3 +84,34 @@ def get_max_iterations() -> int:
8284
except ValueError:
8385
logger.warning(f"Invalid MAX_ITERATIONS value. Defaulting to {DEFAULT_MAX_ITERATIONS}.")
8486
return DEFAULT_MAX_ITERATIONS
87+
88+
89+
# CRI (Cross-Region Inference) prefix pattern
90+
CRI_PREFIX_PATTERN = re.compile(r"^(global|us|eu|apac|jp)\.")
91+
92+
# Prompt caching configuration
93+
# Based on: https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html
94+
# Load from environment variable (injected by CDK from TypeScript definition)
95+
_supported_cache_fields_env = os.environ.get("SUPPORTED_CACHE_FIELDS")
96+
if _supported_cache_fields_env:
97+
SUPPORTED_CACHE_FIELDS: dict[str, list[str]] = json.loads(_supported_cache_fields_env)
98+
else:
99+
# Fallback if environment variable is not set (should not happen in production)
100+
logger.warning("SUPPORTED_CACHE_FIELDS not found in environment, using empty fallback")
101+
SUPPORTED_CACHE_FIELDS: dict[str, list[str]] = {}
102+
103+
104+
def get_supported_cache_fields(model_id: str) -> list[str]:
105+
"""Get supported cache fields for a model (removes CRI prefix before lookup)"""
106+
base_model_id = CRI_PREFIX_PATTERN.sub("", model_id)
107+
return SUPPORTED_CACHE_FIELDS.get(base_model_id, [])
108+
109+
110+
def supports_prompt_cache(model_id: str) -> bool:
111+
"""Check if a model supports prompt caching (system or messages)"""
112+
return len(get_supported_cache_fields(model_id)) > 0
113+
114+
115+
def supports_tools_cache(model_id: str) -> bool:
116+
"""Check if a model supports tools caching"""
117+
return "tools" in get_supported_cache_fields(model_id)

packages/cdk/lib/construct/generic-agent-core.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
import { BucketInfo } from 'generative-ai-use-cases';
2121
import * as path from 'path';
2222
import { loadMCPConfig } from '../utils/mcp-config-loader';
23+
import { SUPPORTED_CACHE_FIELDS } from '@generative-ai-use-cases/common';
2324

2425
export interface AgentCoreRuntimeConfig {
2526
name: string;
@@ -102,6 +103,7 @@ export class GenericAgentCore extends Construct {
102103
environmentVariables: {
103104
FILE_BUCKET: bucketName,
104105
MCP_SERVERS: JSON.stringify(genericMcpServers),
106+
SUPPORTED_CACHE_FIELDS: JSON.stringify(SUPPORTED_CACHE_FIELDS),
105107
},
106108
},
107109
agentBuilder: {
@@ -115,6 +117,7 @@ export class GenericAgentCore extends Construct {
115117
environmentVariables: {
116118
FILE_BUCKET: bucketName,
117119
MCP_SERVERS: JSON.stringify(agentBuilderMcpServers),
120+
SUPPORTED_CACHE_FIELDS: JSON.stringify(SUPPORTED_CACHE_FIELDS),
118121
},
119122
},
120123
};

packages/cdk/test/__snapshots__/generative-ai-use-cases.test.ts.snap

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4526,7 +4526,7 @@ exports[`GenerativeAiUseCases matches the snapshot (closed network mode) 4`] = `
45264526
"AgentRuntimeArtifact": {
45274527
"ContainerConfiguration": {
45284528
"ContainerUri": {
4529-
"Fn::Sub": "123456890123.dkr.ecr.us-east-1.\${AWS::URLSuffix}/cdk-hnb659fds-container-assets-123456890123-us-east-1:8c5a41bdaf8957310132e22107e4c5d5573580cf47ebe99a8c257159a5ba076e",
4529+
"Fn::Sub": "123456890123.dkr.ecr.us-east-1.\${AWS::URLSuffix}/cdk-hnb659fds-container-assets-123456890123-us-east-1:69eb434ee242daa7224dd9fb0f5cf9142a71fe81dac805352a11509e5ecff870",
45304530
},
45314531
},
45324532
},
@@ -4536,6 +4536,7 @@ exports[`GenerativeAiUseCases matches the snapshot (closed network mode) 4`] = `
45364536
"Ref": "GenericAgentCoreAgentCoreFileBucket0430DA42",
45374537
},
45384538
"MCP_SERVERS": "{"time":{"command":"uvx","args":["mcp-server-time"],"metadata":{"category":"Utility","description":"Provides current time and date functionality"}},"aws-knowledge-mcp-server":{"command":"npx","args":["mcp-remote","https://knowledge-mcp.global.api.aws"],"metadata":{"category":"AWS","description":"AWS Knowledge Base MCP server for enterprise knowledge access"}},"awslabs.aws-documentation-mcp-server":{"command":"uvx","args":["awslabs.aws-documentation-mcp-server@latest"],"metadata":{"category":"AWS","description":"Access AWS documentation and guides"}},"awslabs.cdk-mcp-server":{"command":"uvx","args":["awslabs.cdk-mcp-server@latest"],"metadata":{"category":"AWS","description":"AWS CDK code generation and assistance"}},"awslabs.aws-diagram-mcp-server":{"command":"uvx","args":["awslabs.aws-diagram-mcp-server@latest"],"metadata":{"category":"AWS","description":"Generate AWS architecture diagrams"}},"awslabs.nova-canvas-mcp-server":{"command":"uvx","args":["awslabs.nova-canvas-mcp-server@latest"],"env":{"AWS_REGION":"us-east-1"},"metadata":{"category":"AI/ML","description":"Amazon Nova Canvas image generation"}},"tavily-search":{"command":"npx","args":["-y","mcp-remote","https://mcp.tavily.com/mcp/?tavilyApiKey=<key>"],"metadata":{"category":"Search","description":"Web search and research capabilities powered by Tavily"}}}",
4539+
"SUPPORTED_CACHE_FIELDS": "{"anthropic.claude-sonnet-4-5-20250929-v1:0":["messages","system","tools"],"anthropic.claude-haiku-4-5-20251001-v1:0":["messages","system","tools"],"anthropic.claude-opus-4-1-20250805-v1:0":["messages","system","tools"],"anthropic.claude-opus-4-20250514-v1:0":["messages","system","tools"],"anthropic.claude-sonnet-4-20250514-v1:0":["messages","system","tools"],"anthropic.claude-3-7-sonnet-20250219-v1:0":["messages","system","tools"],"anthropic.claude-3-5-haiku-20241022-v1:0":["messages","system","tools"],"amazon.nova-premier-v1:0":["messages","system"],"amazon.nova-pro-v1:0":["messages","system"],"amazon.nova-lite-v1:0":["messages","system"],"amazon.nova-micro-v1:0":["messages","system"]}",
45394540
},
45404541
"NetworkConfiguration": {
45414542
"NetworkMode": "PUBLIC",
@@ -4794,7 +4795,7 @@ exports[`GenerativeAiUseCases matches the snapshot (closed network mode) 4`] = `
47944795
"AgentRuntimeArtifact": {
47954796
"ContainerConfiguration": {
47964797
"ContainerUri": {
4797-
"Fn::Sub": "123456890123.dkr.ecr.us-east-1.\${AWS::URLSuffix}/cdk-hnb659fds-container-assets-123456890123-us-east-1:8c5a41bdaf8957310132e22107e4c5d5573580cf47ebe99a8c257159a5ba076e",
4798+
"Fn::Sub": "123456890123.dkr.ecr.us-east-1.\${AWS::URLSuffix}/cdk-hnb659fds-container-assets-123456890123-us-east-1:69eb434ee242daa7224dd9fb0f5cf9142a71fe81dac805352a11509e5ecff870",
47984799
},
47994800
},
48004801
},
@@ -4804,6 +4805,7 @@ exports[`GenerativeAiUseCases matches the snapshot (closed network mode) 4`] = `
48044805
"Ref": "GenericAgentCoreAgentCoreFileBucket0430DA42",
48054806
},
48064807
"MCP_SERVERS": "{"time":{"command":"uvx","args":["mcp-server-time"],"metadata":{"category":"Utility","description":"Provides current time and date functionality"}},"aws-knowledge-mcp-server":{"command":"npx","args":["mcp-remote","https://knowledge-mcp.global.api.aws"],"metadata":{"category":"AWS","description":"AWS Knowledge Base MCP server for enterprise knowledge access"}},"awslabs.aws-documentation-mcp-server":{"command":"uvx","args":["awslabs.aws-documentation-mcp-server@latest"],"metadata":{"category":"AWS","description":"Access AWS documentation and guides"}},"awslabs.cdk-mcp-server":{"command":"uvx","args":["awslabs.cdk-mcp-server@latest"],"metadata":{"category":"AWS","description":"AWS CDK code generation and assistance"}},"awslabs.aws-diagram-mcp-server":{"command":"uvx","args":["awslabs.aws-diagram-mcp-server@latest"],"metadata":{"category":"AWS","description":"Generate AWS architecture diagrams"}},"awslabs.nova-canvas-mcp-server":{"command":"uvx","args":["awslabs.nova-canvas-mcp-server@latest"],"env":{"AWS_REGION":"us-east-1"},"metadata":{"category":"AI/ML","description":"Amazon Nova Canvas image generation"}},"tavily-search":{"command":"npx","args":["-y","mcp-remote","https://mcp.tavily.com/mcp/?tavilyApiKey=<key>"],"metadata":{"category":"Search","description":"Web search and research capabilities powered by Tavily"}}}",
4808+
"SUPPORTED_CACHE_FIELDS": "{"anthropic.claude-sonnet-4-5-20250929-v1:0":["messages","system","tools"],"anthropic.claude-haiku-4-5-20251001-v1:0":["messages","system","tools"],"anthropic.claude-opus-4-1-20250805-v1:0":["messages","system","tools"],"anthropic.claude-opus-4-20250514-v1:0":["messages","system","tools"],"anthropic.claude-sonnet-4-20250514-v1:0":["messages","system","tools"],"anthropic.claude-3-7-sonnet-20250219-v1:0":["messages","system","tools"],"anthropic.claude-3-5-haiku-20241022-v1:0":["messages","system","tools"],"amazon.nova-premier-v1:0":["messages","system"],"amazon.nova-pro-v1:0":["messages","system"],"amazon.nova-lite-v1:0":["messages","system"],"amazon.nova-micro-v1:0":["messages","system"]}",
48074809
},
48084810
"NetworkConfiguration": {
48094811
"NetworkMode": "PUBLIC",
@@ -26484,7 +26486,7 @@ exports[`GenerativeAiUseCases matches the snapshot 4`] = `
2648426486
"AgentRuntimeArtifact": {
2648526487
"ContainerConfiguration": {
2648626488
"ContainerUri": {
26487-
"Fn::Sub": "123456890123.dkr.ecr.us-east-1.\${AWS::URLSuffix}/cdk-hnb659fds-container-assets-123456890123-us-east-1:8c5a41bdaf8957310132e22107e4c5d5573580cf47ebe99a8c257159a5ba076e",
26489+
"Fn::Sub": "123456890123.dkr.ecr.us-east-1.\${AWS::URLSuffix}/cdk-hnb659fds-container-assets-123456890123-us-east-1:69eb434ee242daa7224dd9fb0f5cf9142a71fe81dac805352a11509e5ecff870",
2648826490
},
2648926491
},
2649026492
},
@@ -26494,6 +26496,7 @@ exports[`GenerativeAiUseCases matches the snapshot 4`] = `
2649426496
"Ref": "GenericAgentCoreAgentCoreFileBucket0430DA42",
2649526497
},
2649626498
"MCP_SERVERS": "{"time":{"command":"uvx","args":["mcp-server-time"],"metadata":{"category":"Utility","description":"Provides current time and date functionality"}},"aws-knowledge-mcp-server":{"command":"npx","args":["mcp-remote","https://knowledge-mcp.global.api.aws"],"metadata":{"category":"AWS","description":"AWS Knowledge Base MCP server for enterprise knowledge access"}},"awslabs.aws-documentation-mcp-server":{"command":"uvx","args":["awslabs.aws-documentation-mcp-server@latest"],"metadata":{"category":"AWS","description":"Access AWS documentation and guides"}},"awslabs.cdk-mcp-server":{"command":"uvx","args":["awslabs.cdk-mcp-server@latest"],"metadata":{"category":"AWS","description":"AWS CDK code generation and assistance"}},"awslabs.aws-diagram-mcp-server":{"command":"uvx","args":["awslabs.aws-diagram-mcp-server@latest"],"metadata":{"category":"AWS","description":"Generate AWS architecture diagrams"}},"awslabs.nova-canvas-mcp-server":{"command":"uvx","args":["awslabs.nova-canvas-mcp-server@latest"],"env":{"AWS_REGION":"us-east-1"},"metadata":{"category":"AI/ML","description":"Amazon Nova Canvas image generation"}},"tavily-search":{"command":"npx","args":["-y","mcp-remote","https://mcp.tavily.com/mcp/?tavilyApiKey=<key>"],"metadata":{"category":"Search","description":"Web search and research capabilities powered by Tavily"}}}",
26499+
"SUPPORTED_CACHE_FIELDS": "{"anthropic.claude-sonnet-4-5-20250929-v1:0":["messages","system","tools"],"anthropic.claude-haiku-4-5-20251001-v1:0":["messages","system","tools"],"anthropic.claude-opus-4-1-20250805-v1:0":["messages","system","tools"],"anthropic.claude-opus-4-20250514-v1:0":["messages","system","tools"],"anthropic.claude-sonnet-4-20250514-v1:0":["messages","system","tools"],"anthropic.claude-3-7-sonnet-20250219-v1:0":["messages","system","tools"],"anthropic.claude-3-5-haiku-20241022-v1:0":["messages","system","tools"],"amazon.nova-premier-v1:0":["messages","system"],"amazon.nova-pro-v1:0":["messages","system"],"amazon.nova-lite-v1:0":["messages","system"],"amazon.nova-micro-v1:0":["messages","system"]}",
2649726500
},
2649826501
"NetworkConfiguration": {
2649926502
"NetworkMode": "PUBLIC",
@@ -26752,7 +26755,7 @@ exports[`GenerativeAiUseCases matches the snapshot 4`] = `
2675226755
"AgentRuntimeArtifact": {
2675326756
"ContainerConfiguration": {
2675426757
"ContainerUri": {
26755-
"Fn::Sub": "123456890123.dkr.ecr.us-east-1.\${AWS::URLSuffix}/cdk-hnb659fds-container-assets-123456890123-us-east-1:8c5a41bdaf8957310132e22107e4c5d5573580cf47ebe99a8c257159a5ba076e",
26758+
"Fn::Sub": "123456890123.dkr.ecr.us-east-1.\${AWS::URLSuffix}/cdk-hnb659fds-container-assets-123456890123-us-east-1:69eb434ee242daa7224dd9fb0f5cf9142a71fe81dac805352a11509e5ecff870",
2675626759
},
2675726760
},
2675826761
},
@@ -26762,6 +26765,7 @@ exports[`GenerativeAiUseCases matches the snapshot 4`] = `
2676226765
"Ref": "GenericAgentCoreAgentCoreFileBucket0430DA42",
2676326766
},
2676426767
"MCP_SERVERS": "{"time":{"command":"uvx","args":["mcp-server-time"],"metadata":{"category":"Utility","description":"Provides current time and date functionality"}},"aws-knowledge-mcp-server":{"command":"npx","args":["mcp-remote","https://knowledge-mcp.global.api.aws"],"metadata":{"category":"AWS","description":"AWS Knowledge Base MCP server for enterprise knowledge access"}},"awslabs.aws-documentation-mcp-server":{"command":"uvx","args":["awslabs.aws-documentation-mcp-server@latest"],"metadata":{"category":"AWS","description":"Access AWS documentation and guides"}},"awslabs.cdk-mcp-server":{"command":"uvx","args":["awslabs.cdk-mcp-server@latest"],"metadata":{"category":"AWS","description":"AWS CDK code generation and assistance"}},"awslabs.aws-diagram-mcp-server":{"command":"uvx","args":["awslabs.aws-diagram-mcp-server@latest"],"metadata":{"category":"AWS","description":"Generate AWS architecture diagrams"}},"awslabs.nova-canvas-mcp-server":{"command":"uvx","args":["awslabs.nova-canvas-mcp-server@latest"],"env":{"AWS_REGION":"us-east-1"},"metadata":{"category":"AI/ML","description":"Amazon Nova Canvas image generation"}},"tavily-search":{"command":"npx","args":["-y","mcp-remote","https://mcp.tavily.com/mcp/?tavilyApiKey=<key>"],"metadata":{"category":"Search","description":"Web search and research capabilities powered by Tavily"}}}",
26768+
"SUPPORTED_CACHE_FIELDS": "{"anthropic.claude-sonnet-4-5-20250929-v1:0":["messages","system","tools"],"anthropic.claude-haiku-4-5-20251001-v1:0":["messages","system","tools"],"anthropic.claude-opus-4-1-20250805-v1:0":["messages","system","tools"],"anthropic.claude-opus-4-20250514-v1:0":["messages","system","tools"],"anthropic.claude-sonnet-4-20250514-v1:0":["messages","system","tools"],"anthropic.claude-3-7-sonnet-20250219-v1:0":["messages","system","tools"],"anthropic.claude-3-5-haiku-20241022-v1:0":["messages","system","tools"],"amazon.nova-premier-v1:0":["messages","system"],"amazon.nova-pro-v1:0":["messages","system"],"amazon.nova-lite-v1:0":["messages","system"],"amazon.nova-micro-v1:0":["messages","system"]}",
2676526769
},
2676626770
"NetworkConfiguration": {
2676726771
"NetworkMode": "PUBLIC",

0 commit comments

Comments
 (0)