Skip to content

Conversation

@Yuripetusko
Copy link
Contributor

@Yuripetusko Yuripetusko commented Nov 6, 2025

Summary

This PR adds per-tool cache key prefixing to prevent cache collisions between different tools using the same parameters, and improves Redis key organization with clearer hierarchy.

Fixes #91

Problem

Cache keys could clash between different tools when using the same parameters. For example, a weather tool and news tool both receiving { location: "NYC" } would generate the same cache key, causing incorrect cached results.

Key Changes

New Features:

  • Per-tool prefixing: Add toolName option to namespace cache keys per tool
  • Configurable separator: Add keySeparator option (default ":")
  • Clearer naming: cacheKeyContext replaces cacheKey (better reflects it's a suffix)

Breaking Changes:

  • keyPrefixstoreName in RedisCacheStore and CacheBackendConfig
  • Default changed from "ai-tools-cache:" to "ai-tools-cache" (separator added automatically)
  • keyGenerator signature changed from positional to object parameters

Deprecations:

  • cacheKey deprecated in favor of cacheKeyContext (backward compatible)

Migration Examples

keyGenerator

// Before
const keyGenerator = (params: any, context?: any): string => {
  return `custom-${JSON.stringify(params)}`;
};

// After
const keyGenerator = ({ params, context, toolName, keySeparator }: {
  params: any;
  context?: any;
  toolName?: string;
  keySeparator?: string;
}): string => {
  return `custom-${JSON.stringify(params)}`;
};

Usage Example

const weatherTool = createCached(tool({ /* ... */ }), {
  toolName: "weather",
  ttl: 3600
});

const newsTool = createCached(tool({ /* ... */ }), {
  toolName: "news",
  ttl: 1800
});

Keys structure: storeName:toolName:params:context

Note: there are breaking changes, not sure if contributors are suppose to run changeset?

- Add toolName option for per-tool cache key prefixing
- Add keySeparator option (default ':')
- Rename keyPrefix to storeName for clarity
- Rename cacheKey to cacheKeyContext (deprecated cacheKey still works)
- Change keyGenerator to use object parameters
- Add DEFAULT_CACHE_KEY_SEPARATOR and DEFAULT_STORE_NAME constants
- Update all examples to use new API
- Fix type issues in example files

BREAKING CHANGE: keyPrefix renamed to storeName, default changed from 'ai-tools-cache:' to 'ai-tools-cache', keyGenerator signature changed to object parameters

Fixes midday-ai#91
@vercel
Copy link

vercel bot commented Nov 6, 2025

@Yuripetusko is attempting to deploy a commit to the Pontus Abrahamsson's projects Team on Vercel.

A member of the Team first needs to authorize it.

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.

cacheKey could clash for different tools

1 participant