Skip to content

Conversation

@elliottlawson
Copy link
Contributor

@elliottlawson elliottlawson commented Jul 10, 2025

Summary

  • Fixes issue where tool_result_cache_type was being applied to each tool result message instead of only the last one
  • Moves caching logic from message creation to final mapping stage
  • Ensures only the last tool result across all messages gets cached

Changes

  • Remove cache application in Text::handleToolCalls() and Stream::addMessagesToRequest()
  • Add logic to MessageMap::map() to identify and cache only the last tool result message
  • Add comprehensive test coverage for the caching behavior

Test plan

  • Added new test file ToolResultCachingTest.php with 3 test cases

@elliottlawson elliottlawson force-pushed the fix/tool-result-caching-accumulation branch from 5039197 to f48bae1 Compare July 10, 2025 02:38
@elliottlawson elliottlawson changed the title Fix tool result cache accumulation across multiple tool calls fix(anthropic): resolve issue with tool result caching Jul 10, 2025
@elliottlawson elliottlawson force-pushed the fix/tool-result-caching-accumulation branch 2 times, most recently from 4bb94bb to c169f83 Compare July 10, 2025 02:42
@elliottlawson elliottlawson marked this pull request as draft July 10, 2025 02:52
Apply tool_result_cache_type only to the last tool result message across all messages instead of applying it to each tool result during creation. This prevents cache accumulation when there are multiple tool call rounds.
@elliottlawson elliottlawson force-pushed the fix/tool-result-caching-accumulation branch from c169f83 to 5084b26 Compare July 10, 2025 02:53
@kinsta
Copy link

kinsta bot commented Jul 13, 2025

Preview deployments for prism ⚡️

Status Branch preview Commit preview
❌ Failed to deploy N/A N/A

Commit: 5f53879d1f18be74cfe96e92390adb9aef8bd02b

Deployment ID: 485cd4cf-5586-4b8a-a1fb-eaae2154b561

Static site name: prism-97nz9

@sixlive
Copy link
Contributor

sixlive commented Jul 13, 2025

@ChrisB-TL can you take a look at this?

@elliottlawson elliottlawson marked this pull request as ready for review July 16, 2025 02:17
@elliottlawson
Copy link
Contributor Author

I've been running this patch for the past week with pretty good results. But it wouldn't hurt for someone to do a once-over.

@sixlive
Copy link
Contributor

sixlive commented Jul 26, 2025

@ChrisB-TL can you take a look at this?

@ChrisB-TL
Copy link
Contributor

Will this not result in missing the last cache on subsequent calls, as the breakpoint is no longer there? I.e. you'll miss the cache until the last call, which will undermine the point of it?

Anthropic does have a limit of four cache break points, so the current implementation will break eventually for chains with multiple tool calls.

@ChrisB-TL
Copy link
Contributor

ChrisB-TL commented Aug 28, 2025

Will this not result in missing the last cache on subsequent calls, as the breakpoint is no longer there? I.e. you'll miss the cache until the last call, which will undermine the point of it?

Anthropic does have a limit of four cache break points, so the current implementation will break eventually for chains with multiple tool calls.

Actually, possibly not. From the docs:

If any of these previous positions match cached content from earlier requests, the system uses the longest matching prefix
This means you don’t need multiple breakpoints just to enable caching - one at the end is sufficient

Would you be able to test out with real requests to confirm, and add a test that uses recorded fixtures?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants