Class OpenAiPromptCacheBreakpoint
java.lang.Object
dev.langchain4j.model.openai.OpenAiPromptCacheBreakpoint
Marks the end of a cacheable prompt prefix for
gpt-5.6 and later, which match a cache entry
exactly at a breakpoint instead of falling back to a shorter unmarked prefix.
Use mark(ChatMessage) to mark a message. Since prompt caching is prefix-based, the
prompt_cache_breakpoint is applied to the last content block of the marked message,
so that everything up to and including that message forms the cached prefix:
SystemMessage systemMessage = OpenAiPromptCacheBreakpoint.mark(SystemMessage.from(SHARED_INSTRUCTIONS));
Marking is also possible by hand, by putting MODE_EXPLICIT under the ATTRIBUTE_KEY
attribute:
SystemMessage systemMessage = SystemMessage.builder()
.text(SHARED_INSTRUCTIONS)
.attributes(Map.of(OpenAiPromptCacheBreakpoint.ATTRIBUTE_KEY,
OpenAiPromptCacheBreakpoint.MODE_EXPLICIT))
.build();
Breakpoints can be placed on a SystemMessage, a UserMessage and a
ToolExecutionResultMessage. AiMessage cannot carry one, because assistant output
blocks are not among the block types OpenAI accepts a breakpoint on.
Each request supports up to four cache writes, one of which is consumed by
OpenAiPromptCacheOptions.MODE_IMPLICIT.
- Since:
- 1.21.0
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringTheChatMessageattribute key under which the breakpoint mode is stored.static final StringThe only breakpoint mode currently accepted by OpenAI. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanWhether the given attributes mark a message as a prompt cache breakpoint.static <T extends ChatMessage>
Tmark(T message) Returns a copy of the given message marked as a prompt cache breakpoint, so that everything up to and including that message forms the cached prefix:
-
Field Details
-
ATTRIBUTE_KEY
TheChatMessageattribute key under which the breakpoint mode is stored. Do not change, it is part of the public API.- See Also:
-
MODE_EXPLICIT
The only breakpoint mode currently accepted by OpenAI.- See Also:
-
-
Method Details
-
mark
Returns a copy of the given message marked as a prompt cache breakpoint, so that everything up to and including that message forms the cached prefix:
The given message is left untouched, and any other attributes it carries are preserved.SystemMessage systemMessage = OpenAiPromptCacheBreakpoint.mark(SystemMessage.from(SHARED_INSTRUCTIONS));- Parameters:
message- aSystemMessage, aUserMessageor aToolExecutionResultMessage.- Returns:
- a marked copy of the given message.
- Throws:
UnsupportedFeatureException- if the message cannot carry a breakpoint, e.g. anAiMessage.- Since:
- 1.21.0
-
isMarked
Whether the given attributes mark a message as a prompt cache breakpoint.- Parameters:
attributes- theChatMessageattributes, may benull.- Returns:
trueif a breakpoint should be emitted for this message.- Throws:
IllegalArgumentException- if the attribute is present but holds an unsupported value. OpenAI answers such a request with an HTTP 400.
-