langchain-ai/langchain · error · ValueError

No global cache was configured. Use `set_llm_cache`.to set a

Error message

No global cache was configured. Use `set_llm_cache`.to set a global cache if you want to use a global cache.Otherwise either pass a cache object or set cache to False/None

What it means

`ValueError` from `get_cache` in `langchain_core.language_models.llms`: the caller passed `cache=True`, which means "use the global cache", but `get_llm_cache()` returned `None` — no global LLM cache was ever installed. The function resolves the cache argument into a concrete `BaseCache` or fails loudly.

Source

Thrown at libs/core/langchain_core/language_models/llms.py:150

    )


def _resolve_cache(*, cache: BaseCache | bool | None) -> BaseCache | None:
    """Resolve the cache."""
    llm_cache: BaseCache | None
    if isinstance(cache, BaseCache):
        llm_cache = cache
    elif cache is None:
        llm_cache = get_llm_cache()
    elif cache is True:
        llm_cache = get_llm_cache()
        if llm_cache is None:
            msg = (
                "No global cache was configured. Use `set_llm_cache`."
                "to set a global cache if you want to use a global cache."
                "Otherwise either pass a cache object or set cache to False/None"
            )
            raise ValueError(msg)
    elif cache is False:
        llm_cache = None
    else:
        msg = f"Unsupported cache value {cache}"  # type: ignore[unreachable]
        raise ValueError(msg)
    return llm_cache


def get_prompts(
    params: dict[str, Any],
    prompts: list[str],
    cache: BaseCache | bool | None = None,  # noqa: FBT001
) -> tuple[dict[int, list[Generation]], str, list[int], list[str]]:
    """Get prompts that are already cached.

    Args:
        params: Dictionary of parameters.
        prompts: List of prompts.

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Install a global cache at startup: `from langchain_core.globals import set_llm_cache; set_llm_cache(InMemoryCache())`.
  2. Or pass a cache instance directly (`cache=SQLiteCache(".cache.db")`) instead of `True`.
  3. Or use `cache=False` to opt out explicitly.
  4. Verify with `get_llm_cache() is not None` before enabling caching in library code.

Example fix

# before
llm.generate(["hello"], cache=True)  # ValueError

# after
from langchain_core.globals import set_llm_cache
from langchain_core.caches import InMemoryCache
set_llm_cache(InMemoryCache())
llm.generate(["hello"], cache=True)
Defensive patterns

Strategy: validation

Validate before calling

from langchain_core.globals import get_llm_cache
if cache is True and get_llm_cache() is None:
    raise ValueError("call set_llm_cache before using cache=True")

Try / catch

try:
    result = llm.generate(prompts, cache=True)
except ValueError as e:
    if "No global cache was configured" in str(e):
        from langchain_core.globals import set_llm_cache
        from langchain_core.caches import InMemoryCache
        set_llm_cache(InMemoryCache())
        result = llm.generate(prompts, cache=True)
    else:
        raise

Prevention

When it happens

Trigger: Calling `llm.generate(prompts, cache=True)` (or `get_prompts(..., cache=True)`) without a prior `set_llm_cache(InMemoryCache())` / `set_llm_cache(SQLiteCache(path))` in the same process.

Common situations: Tutorials that say "set `cache=True` to speed things up" without mentioning `set_llm_cache`; long-lived servers where the cache was set in a different process; notebook kernel restarts wiping global state.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/472b3d0894acce74. Report an issue: GitHub.