feat: add opt-in ToolCache for caching tool invocation results in ToolInvoker and Agent#11824
Open
Kunal-Somani wants to merge 2 commits into
Open
feat: add opt-in ToolCache for caching tool invocation results in ToolInvoker and Agent#11824Kunal-Somani wants to merge 2 commits into
Kunal-Somani wants to merge 2 commits into
Conversation
…lInvoker and Agent
|
@Kunal-Somani is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issues
Proposed Changes: Adds an opt-in
ToolCachesoToolInvokerandAgentcan avoid re-invoking the same tool call multiple times within an agent loop (e.g. re-fetching a document already fetched a few steps earlier).haystack/tools/tool_cache.py:ToolCache,ToolCacheBackend(ABC),InMemoryToolCache(default backend), andToolCacheStats(hits/misses/calls_saved).Toolgains acacheable: bool = Falsefield — caching is opt-in per tool so write-effecting tools never accidentally serve a stale cached result for a side-effecting call.ComponentToolinherits and serializes this field too.ToolInvokeraccepts an optionaltool_cacheparam. Cache lookups happen against the original LLM-provided arguments (not state-injected/streaming-callback-injected ones) so identical logical calls hit the cache consistently. Cache hits skip invocation entirely while keeping the existing ThreadPoolExecutor/future-based concurrent execution path unchanged.Agentaccepts the sametool_cacheparam and passes it straight through to its internalToolInvoker. When configured,run()/run_async()include atool_cache_statskey in their output dict with the run's hit/miss/calls_saved counts.tool_cacheis intentionally excluded fromto_dict()/from_dict()on bothToolInvokerandAgent— cache backends are runtime objects, not serializable configuration.How did you test it?
test/tools/test_tool_cache.py(15 tests):ToolCacheStats,InMemoryToolCacheget/set/clear,make_cache_keydeterminism,ToolCachehit/miss/TTL-expiry/scope-validation.TestToolInvokerCachingclass intest_tool_invoker.py(3 tests): cacheable tool's second identical call is served from cache (function invoked once, not twice); non-cacheable tool always invokes; different arguments produce cache misses.test_agent.py:tool_cache_statsappears inAgent.run()output when a cache is configured.to_dict()/serialization test fixtures across 9 files to include the newcacheable: Falsefield on serializedToolobjects (test_tool.py,test_component_tool.py,test_agent.py,test_agent_breakpoints.py,test_agent_hitl.py, and the Azure/HuggingFace/OpenAI chat generator tests) — pure fixture updates, no behavior changes in those files.PYTHONPATH="" hatch run test:unit test— 5307 passed, 4 skipped, 0 failed.hatch run fmtandhatch run test:typesclean.Notes for the reviewer
Not a breaking change —
cacheabledefaults toFalseandtool_cachedefaults toNoneon bothToolInvokerandAgent, so existing behavior is unchanged unless a cache is explicitly configured.Checklist
fix:,feat:,build:,chore:,ci:,docs:,style:,refactor:,perf:,test:and added!in case the PR includes breaking changes.