Skip to content

dspy.configure_cache

dspy.configure_cache(enable_disk_cache: Optional[bool] = True, enable_memory_cache: Optional[bool] = True, disk_cache_dir: Optional[str] = DISK_CACHE_DIR, disk_size_limit_bytes: Optional[int] = DISK_CACHE_LIMIT, memory_max_entries: Optional[int] = 1000000, enable_litellm_cache: bool = False)

Configure the cache for DSPy.

Parameters:

Name Type Description Default
enable_disk_cache Optional[bool]

Whether to enable on-disk cache.

True
enable_memory_cache Optional[bool]

Whether to enable in-memory cache.

True
disk_cache_dir Optional[str]

The directory to store the on-disk cache.

DISK_CACHE_DIR
disk_size_limit_bytes Optional[int]

The size limit of the on-disk cache.

DISK_CACHE_LIMIT
memory_max_entries Optional[int]

The maximum number of entries in the in-memory cache.

1000000
enable_litellm_cache bool

Whether to enable LiteLLM cache.

False
Source code in dspy/clients/__init__.py
def configure_cache(
    enable_disk_cache: Optional[bool] = True,
    enable_memory_cache: Optional[bool] = True,
    disk_cache_dir: Optional[str] = DISK_CACHE_DIR,
    disk_size_limit_bytes: Optional[int] = DISK_CACHE_LIMIT,
    memory_max_entries: Optional[int] = 1000000,
    enable_litellm_cache: bool = False,
):
    """Configure the cache for DSPy.

    Args:
        enable_disk_cache: Whether to enable on-disk cache.
        enable_memory_cache: Whether to enable in-memory cache.
        disk_cache_dir: The directory to store the on-disk cache.
        disk_size_limit_bytes: The size limit of the on-disk cache.
        memory_max_entries: The maximum number of entries in the in-memory cache.
        enable_litellm_cache: Whether to enable LiteLLM cache.
    """
    if enable_disk_cache and enable_litellm_cache:
        raise ValueError(
            "Cannot enable both LiteLLM and DSPy on-disk cache, please set at most one of `enable_disk_cache` or "
            "`enable_litellm_cache` to True."
        )

    if enable_litellm_cache:
        try:
            litellm.cache = LitellmCache(disk_cache_dir=DISK_CACHE_DIR, type="disk")

            if litellm.cache.cache.disk_cache.size_limit != DISK_CACHE_LIMIT:
                litellm.cache.cache.disk_cache.reset("size_limit", DISK_CACHE_LIMIT)
        except Exception as e:
            # It's possible that users don't have the write permissions to the cache directory.
            # In that case, we'll just disable the cache.
            logger.warning("Failed to initialize LiteLLM cache: %s", e)
            litellm.cache = None
    else:
        litellm.cache = None

    import dspy

    dspy.cache = Cache(
        enable_disk_cache,
        enable_memory_cache,
        disk_cache_dir,
        disk_size_limit_bytes,
        memory_max_entries,
    )