Skip to content

Commit 4fafe37

Browse files
committed
Fix: AgentCore prompt cache support via environment variable
Load SUPPORTED_CACHE_FIELDS from environment variable to maintain single source of truth in TypeScript (packages/common/model.ts). This follows the same pattern as MCP_SERVERS configuration: - CDK injects SUPPORTED_CACHE_FIELDS via environment variable - Python runtime reads from environment - Claude models: cache_prompt + cache_tools enabled - Nova models: cache_prompt only (no cache_tools) - Other models: no caching (safe default) Changes: - generic-agent-core.ts: Pass SUPPORTED_CACHE_FIELDS as env var - config.py: Load from env var with standard Python style - agent.py: Conditional BedrockModel initialization Fixes: ValidationException when using Nova models with AgentCore
1 parent ffa9ce9 commit 4fafe37

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
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
};

0 commit comments

Comments
 (0)