Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions crates/goose/src/providers/bedrock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,14 @@ impl BedrockProvider {
let visible_messages: Vec<&Message> =
messages.iter().filter(|m| m.is_agent_visible()).collect();

// Cache the earliest messages (not most recent) because prompt caching
// requires exact prefix matching — caching recent messages would shift
// positions each turn, causing misses.
const MESSAGE_CACHE_BUDGET: usize = 3;
let cache_count = if enable_caching {
visible_messages.len().min(MESSAGE_CACHE_BUDGET)
} else {
0
};
// Place the cache point on the trailing message. Cache entries are keyed by
// the hash of the prefix ending at the breakpoint; on each new turn, the
// lookback walks backward from the new trailing position and finds the
// previous turn's entry, so fresh processing is bounded to the content added
// since the last request. See
// https://platform.claude.com/docs/en/build-with-claude/prompt-caching
let last_idx = visible_messages.len().checked_sub(1);
let cache_last = enable_caching && last_idx.is_some();

let mut request = self
.client
Expand All @@ -251,7 +250,9 @@ impl BedrockProvider {
visible_messages
.iter()
.enumerate()
.map(|(idx, m)| to_bedrock_message_with_caching(m, idx < cache_count))
.map(|(idx, m)| {
to_bedrock_message_with_caching(m, cache_last && Some(idx) == last_idx)
})
.collect::<Result<_>>()?,
));

Expand Down
Loading