Decorators

Caching decorators for Arshai agents and workflows.

Functions

arshai.cache.decorators.cache_result(ttl=300, key_prefix='', key_func=None, cache_none=False)[source]

Decorator to cache agent/workflow results.

This decorator provides transparent caching for expensive operations. It works with any object that has a ‘memory’ attribute implementing get/set methods.

Example

class MyAgent(BaseAgent):

@cache_result(ttl=600, key_prefix=”analysis”) async def analyze_data(self, input_data):

# Expensive operation return result

@cache_result(ttl=3600) def process_sync(self, data):

# Sync method also supported return processed_data

Parameters:
  • ttl (int) – Time to live in seconds (default 300)

  • key_prefix (str) – Prefix for cache keys

  • key_func (Optional[Callable]) – Optional function to generate cache key

  • cache_none (bool) – Whether to cache None results (default False)

arshai.cache.decorators.create_key_func(include_args=True, include_kwargs=True, hash_objects=True)[source]

Create a custom key generation function.

Parameters:
  • include_args (bool) – Include positional arguments in key

  • include_kwargs (bool) – Include keyword arguments in key

  • hash_objects (bool) – Hash complex objects (otherwise use repr)

Returns:

Key generation function

Example

@cache_result(key_func=create_key_func(include_kwargs=False)) def my_func(self, x, y=10):

# Only x will be used for cache key, y will be ignored return x * y

arshai.cache.decorators.invalidate_cache(self, pattern=None)[source]

Helper function to invalidate cache entries.

Can be used as a method in classes that use caching.

Parameters:
  • self – Object with memory manager

  • pattern (Optional[str]) – Optional pattern to match keys (prefix)

Example

class MyAgent(BaseAgent):
def clear_analysis_cache(self):

invalidate_cache(self, “analysis:”)