Action Cache

Cache expensive action results like AI API calls in Convex with configurable TTL to reduce latency and API costs.

Installation

npm install @convex-dev/action-cache

About Action Cache

Sometimes your app needs to fetch information from a third-party API that is slow or costs money. Caching can help! This is a Convex component that can cache the results of expensive functions and set an optional TTL. Expired entries are cleaned up via a cron job once a day. The cache key is the ActionCache's name (defaults to function name) and the arguments to the action that generates the cache values.

To invalidate the cache, you can set a new name explicitly and/or clear cached values by name.

Benefits

Use cases

how to cache AI API calls in Convex actions

Action Cache wraps your expensive AI calls with automatic caching based on input parameters. Results are stored with configurable TTL, so identical requests return instantly from cache instead of hitting the AI API again.

reduce OpenAI API costs in Convex backend

Use Action Cache to store OpenAI responses and avoid duplicate API calls for the same inputs. The component automatically handles cache keys and expiration, significantly reducing your AI API usage and costs.

cache third party API responses Convex

Action Cache stores results from external APIs like payment processors or data services. Configure cache duration based on how frequently the external data changes to balance freshness with performance.

Frequently asked questions

How does Action Cache determine when to return cached vs fresh results?

Action Cache uses input parameters as cache keys and respects the TTL you configure. If cached data exists and hasn't expired, it returns immediately. Otherwise, it executes your action and caches the new result for subsequent calls.

Can I manually invalidate cached results before they expire?

Yes, Action Cache provides methods to manually clear specific cache entries or entire cache namespaces. This is useful when you know external data has changed and want to force fresh API calls.

What happens if my cached action throws an error?

Action Cache only stores successful results. If your action throws an error, the error propagates normally and no cache entry is created, ensuring failed operations don't prevent retries.

How much data can Action Cache store?

Action Cache uses Convex's database for storage, so it's limited by your Convex plan's database size. The component is designed for caching API responses and computed results, not large files or datasets.

Links