Skip to main content

Overview

Caching is a technique used to temporarily store copies of data or computation results to improve performance by reducing the need to repeatedly fetch or compute the same data from slower or more resource-intensive sources. In the context of AI applications, caching provides several important benefits:
  • 🚀 Performance improvement - Avoid repeating expensive operations like API calls or complex calculations
  • 💰 Cost reduction - Minimize repeated calls to paid services (like external APIs or LLM providers)
  • Latency reduction - Deliver faster responses to users by serving cached results
  • 🔄 Consistency - Ensure consistent responses for identical inputs
BeeAI framework provides a robust caching system with multiple implementations to suit different use cases.

Core concepts

Cache types

BeeAI framework offers several cache implementations out of the box: Each cache type implements the BaseCache interface, making them interchangeable in your code.

Usage patterns

BeeAI framework supports several caching patterns:

Basic usage

Caching function output

The simplest way to use caching is to wrap a function that produces deterministic output:

Using with tools

BeeAI framework’s caching system seamlessly integrates with tools:

Using with LLMs

You can also cache LLM responses to save on API costs:

Cache types

UnconstrainedCache

The simplest cache type with no constraints on size or entry lifetime. Good for development and smaller applications.

SlidingCache

Maintains a maximum number of entries, removing the oldest entries when the limit is reached.

FileCache

Persists cache data to disk, allowing data to survive if application restarts. Use it when caches must survive process restarts or you need to share state between workers. Persisted entries still respect TTL and eviction settings, so design your limits accordingly.

With custom provider

Seed a file-backed cache from another provider when you want to warm the disk cache before first use or promote hot data captured in memory. The example below clones an UnconstrainedCache into the JSON file cache so new processes can reuse it immediately.

NullCache

A special cache that implements the BaseCache interface but performs no caching. Useful for testing or temporarily disabling caching. The reason for implementing is to enable Null object pattern.

Advanced usage

Cache decorator

Create a reusable decorator when you want to keep caching logic close to your functions without wiring cache calls manually.
For more complex caching logic, you can customize the key generation: Use custom key builders to partition cache entries per tenant or time window, and clear the cache in response to deployment events.

CacheFn helper

For more dynamic caching needs, the CacheFn helper provides a functional approach: It is well-suited for API tokens or other resources that return an expiry with each refresh—call update_ttl before returning the value so the cache matches the upstream lifetime.

Creating a custom cache provider

You can create your own cache implementation by extending the BaseCache class:

Examples

Python

Explore reference cache implementations in Python

TypeScript

Explore reference cache implementations in TypeScript