Skip to content

Conversation

raviraj-441
Copy link

Description

Fixes issue #4387 where @tool(cache_results=True) does not work as expected when tool_hooks is not set.

Root Cause

The cache key computation was inconsistent between cache lookup and cache storage:

  • Cache lookup used empty entrypoint_args that start empty
  • Cache storage used entrypoint_args after mutation with arguments.update()
  • This caused different cache keys for read vs write operations, resulting in cache misses

Solution

  • Added _get_cache_key_from_resolved_args() method for deterministic cache key generation
  • Added _build_resolved_args() helper to build canonical arguments before mutations
  • Modified execute() and aexecute() to compute cache key once from resolved args and reuse for both lookup and storage
  • Cache key computation now filters framework arguments and sorts for consistency

Changes Made

  1. Function.py changes:

    • Added _get_cache_key_from_resolved_args() method
    • Added FunctionCall._build_resolved_args() helper method
    • Modified cache key computation in execute() and aexecute() methods
  2. Test coverage:

    • Added test_tool_cache_without_hooks() - verifies caching with no tool_hooks
    • Added test_tool_cache_with_empty_hooks() - verifies caching with empty tool_hooks
    • Added test_tool_cache_kwargs_order_independence() - verifies order independence

Testing

  • ✅ All new tests pass
  • ✅ Existing test suite passes (37/38 tests pass, 1 unrelated Windows path test fails)
  • ✅ Manual testing confirms cache hits on second identical calls

Impact

  • Fixes caching behavior for @tool(cache_results=True) when tool_hooks is not provided
  • Maintains backward compatibility when tool_hooks is present
  • Improves cache key consistency regardless of argument order

…ool_hooks (agno-agi#4387)

- Fixed inconsistent cache key computation in @tool decorator where cache
  lookup used empty entrypoint_args but cache storage used mutated args
- Added _get_cache_key_from_resolved_args() method for deterministic
  cache key generation with sorted arguments and framework arg filtering
- Added _build_resolved_args() helper to build canonical arguments
  before any mutations
- Modified execute() and aexecute() to compute cache_key once from
  resolved_args and reuse for both lookup and storage
- Added comprehensive tests for cache functionality without/with
  tool_hooks and argument order independence
- Ensures @tool(cache_results=True) works correctly regardless of
  tool_hooks configuration

Fixes agno-agi#4387
@raviraj-441 raviraj-441 requested a review from a team as a code owner September 1, 2025 12:21
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.

1 participant