Skip to content

Conversation

@KeremTurgutlu
Copy link
Contributor

@KeremTurgutlu KeremTurgutlu commented Oct 22, 2025

This PR introduces cache_idxs argument which will let users to provide custom cache idx locations to insert Anthropic cache control breakpoints.

If a cache idx corresponds to a tool call block or a tool result then it will be skipped and shifted to the closest previous valid message.

By default the last message will be cached if cache=True and no cache_idxs is provided.

For example, to cache both the sysp and the last message a user can set cache_idxs=[0,-1] assuming sp is also given.

res = []
for idx in cache_idxs:
try:
while msgs[idx].get('tool_calls', []) or msgs[idx]['role'] == 'tool': idx -= 1
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some cache_idxs can end up being shifted back to the same idx. For example if last 4 messages are tool blocks / results and user sets cahce_idxs=[-4,-3,-2,-1] then it will be cache_idxs=[-5,-5,-5,-5].

We can keep shifting them until all idxs are unique (if possible otherwise collision is unavoidable) but this might complicate the logic a bit?

@KeremTurgutlu KeremTurgutlu added the enhancement New feature or request label Oct 22, 2025
@KeremTurgutlu KeremTurgutlu self-assigned this Oct 22, 2025
@KeremTurgutlu KeremTurgutlu changed the title add custom cache idxs Custom anthropic cache_idxs Oct 22, 2025
@KeremTurgutlu KeremTurgutlu marked this pull request as ready for review October 22, 2025 13:01
@KeremTurgutlu KeremTurgutlu requested a review from jph00 October 22, 2025 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant