Class BedrockSystemMessage
java.lang.Object
dev.langchain4j.model.bedrock.BedrockSystemMessage
- All Implemented Interfaces:
ChatMessage
AWS Bedrock-specific system message supporting granular cache points.
Unlike the core SystemMessage which contains a single text block,
this class supports multiple content blocks, each with an optional cache point.
This enables caching static portions (instructions, examples) while keeping
dynamic portions (user context) uncached.
Example usage:
BedrockSystemMessage systemMessage = BedrockSystemMessage.builder()
.addText("You are an AI assistant.")
.addTextWithCachePoint("Here are 50 examples:\n" + examples) // Cached
.addText("Current user: " + userName) // Not cached
.build();
AWS Bedrock Caching Requirements:
- Minimum tokens: ~1,024 tokens required for caching to activate
- Cache TTL: 5-minute default, resets on each cache hit
- Supported models: Only Claude 3.x and Amazon Nova models
- Maximum cache points: AWS limits to 4 cache points per request (across all messages including system, user, and tool definitions)
Important limitations:
- Serialization: This message type is NOT compatible with standard
ChatMessageSerializer. If usingChatMemorywith persistence, convert toSystemMessagefirst usingtoSystemMessage()(cache points will be lost). - ChatMemory window:
MessageWindowChatMemoryandTokenWindowChatMemorywill NOT recognize this as a system message for window management (they useinstanceof SystemMessage). - Static helpers:
SystemMessage.findFirst(),findAll(),findLast()will NOT findBedrockSystemMessageinstances. - Type checking:
type()returnsChatMessageType.SYSTEM, but this class does NOT extendSystemMessage. Always useinstanceofchecks rather thantype()checks. WARNING: Code usingmessage.type() == ChatMessageType.SYSTEMwill match this class, but code usinginstanceof SystemMessagewill NOT. The codebase has many places usinginstanceof SystemMessagethat will silently ignoreBedrockSystemMessageinstances.
Thread Safety: Instances of this class are immutable and thread-safe.
The BedrockSystemMessage.Builder is NOT thread-safe and should not be shared between threads.
- Since:
- 1.11.0
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intMaximum number of cache points allowed by AWS Bedrock per request.static final intMaximum number of content blocks per message. -
Method Summary
Modifier and TypeMethodDescriptionstatic BedrockSystemMessage.Builderbuilder()Creates a new builder.intReturns the number of cache points in this message.contents()Returns an unmodifiable list of content blocks.booleanstatic BedrockSystemMessagefrom(SystemMessage systemMessage) Converts a core SystemMessage to BedrockSystemMessage (no cache point).static BedrockSystemMessageCreates a simple message with single text content (no cache point).static BedrockSystemMessagefrom(List<BedrockSystemContent> contents) Creates a message from list of content blocks.booleanReturns true if any content block has a cache point marker.inthashCode()booleanReturns true if this message contains exactly one text content block.Returns text from single content block.text()Returns combined text from all text content blocks, joined by double newlines.Creates a new builder initialized with this message's contents.toString()Converts to core SystemMessage (loses cache point information).type()ReturnsChatMessageType.SYSTEM.
-
Field Details
-
MAX_CONTENT_BLOCKS
public static final int MAX_CONTENT_BLOCKSMaximum number of content blocks per message.- See Also:
-
MAX_CACHE_POINTS
public static final int MAX_CACHE_POINTSMaximum number of cache points allowed by AWS Bedrock per request.- See Also:
-
-
Method Details
-
contents
Returns an unmodifiable list of content blocks.- Returns:
- the content blocks
-
type
ReturnsChatMessageType.SYSTEM.Note: While this returns
SYSTEM, this class does NOT extendSystemMessage. Always useinstanceofchecks for type safety.- Specified by:
typein interfaceChatMessage- Returns:
- ChatMessageType.SYSTEM
-
text
Returns combined text from all text content blocks, joined by double newlines. Useful for logging or conversion to core SystemMessage.- Returns:
- combined text from all blocks
-
hasSingleText
public boolean hasSingleText()Returns true if this message contains exactly one text content block.- Returns:
- true if single text block
-
singleText
Returns text from single content block.- Returns:
- the single text content
- Throws:
IllegalStateException- if message has multiple content blocks
-
toSystemMessage
Converts to core SystemMessage (loses cache point information). Use this for ChatMemory persistence where serialization is required.- Returns:
- equivalent SystemMessage (without cache points)
-
toBuilder
Creates a new builder initialized with this message's contents.- Returns:
- new builder with current contents
-
builder
-
from
Creates a simple message with single text content (no cache point).- Parameters:
text- the text content- Returns:
- new BedrockSystemMessage
-
from
Creates a message from list of content blocks.- Parameters:
contents- the content blocks- Returns:
- new BedrockSystemMessage
-
from
Converts a core SystemMessage to BedrockSystemMessage (no cache point).- Parameters:
systemMessage- the core system message- Returns:
- new BedrockSystemMessage
-
equals
-
hashCode
-
hasCachePoints
public boolean hasCachePoints()Returns true if any content block has a cache point marker.- Returns:
- true if this message contains any cache points
-
cachePointCount
public int cachePointCount()Returns the number of cache points in this message.- Returns:
- the cache point count
-
toString
-