{"record":{"id":"472b3d0894acce74","repo":"langchain-ai/langchain","slug":"no-global-cache-was-configured-use-set-llm-cache","errorCode":null,"errorMessage":"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","messagePattern":"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","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/language_models/llms.py","lineNumber":150,"sourceCode":"    )\n\n\ndef _resolve_cache(*, cache: BaseCache | bool | None) -> BaseCache | None:\n    \"\"\"Resolve the cache.\"\"\"\n    llm_cache: BaseCache | None\n    if isinstance(cache, BaseCache):\n        llm_cache = cache\n    elif cache is None:\n        llm_cache = get_llm_cache()\n    elif cache is True:\n        llm_cache = get_llm_cache()\n        if llm_cache is None:\n            msg = (\n                \"No global cache was configured. Use `set_llm_cache`.\"\n                \"to set a global cache if you want to use a global cache.\"\n                \"Otherwise either pass a cache object or set cache to False/None\"\n            )\n            raise ValueError(msg)\n    elif cache is False:\n        llm_cache = None\n    else:\n        msg = f\"Unsupported cache value {cache}\"  # type: ignore[unreachable]\n        raise ValueError(msg)\n    return llm_cache\n\n\ndef get_prompts(\n    params: dict[str, Any],\n    prompts: list[str],\n    cache: BaseCache | bool | None = None,  # noqa: FBT001\n) -> tuple[dict[int, list[Generation]], str, list[int], list[str]]:\n    \"\"\"Get prompts that are already cached.\n\n    Args:\n        params: Dictionary of parameters.\n        prompts: List of prompts.","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/language_models/llms.py#L132-L168","documentation":"`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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Install a global cache at startup: `from langchain_core.globals import set_llm_cache; set_llm_cache(InMemoryCache())`.","Or pass a cache instance directly (`cache=SQLiteCache(\".cache.db\")`) instead of `True`.","Or use `cache=False` to opt out explicitly.","Verify with `get_llm_cache() is not None` before enabling caching in library code."],"exampleFix":"# before\nllm.generate([\"hello\"], cache=True)  # ValueError\n\n# after\nfrom langchain_core.globals import set_llm_cache\nfrom langchain_core.caches import InMemoryCache\nset_llm_cache(InMemoryCache())\nllm.generate([\"hello\"], cache=True)","handlingStrategy":"validation","validationCode":"from langchain_core.globals import get_llm_cache\nif cache is True and get_llm_cache() is None:\n    raise ValueError(\"call set_llm_cache before using cache=True\")","typeGuard":null,"tryCatchPattern":"try:\n    result = llm.generate(prompts, cache=True)\nexcept ValueError as e:\n    if \"No global cache was configured\" in str(e):\n        from langchain_core.globals import set_llm_cache\n        from langchain_core.caches import InMemoryCache\n        set_llm_cache(InMemoryCache())\n        result = llm.generate(prompts, cache=True)\n    else:\n        raise","preventionTips":["Call `set_llm_cache` during application/notebook startup when caching is enabled.","Pass an explicit `BaseCache` instance for library code shared across processes.","Health-check `get_llm_cache() is not None` when caching is a hard requirement."],"tags":["cache","configuration","llm"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}