Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKProvidersAnthropicAWSGoogleMicrosoftOpenAIMoreComponentsLLMsLLMsAI21Aleph AlphaAmazon API GatewayAnyscaleArceeAzure MLAzure OpenAIBaidu QianfanBananaBasetenBeamBedrockBittensorCerebriumAIChatGLMClarifaiCohereC TransformersCTranslate2DatabricksDeepInfraDeepSparseEden AIFireworksForefrontAIGCP Vertex AIGooseAIGPT4AllGradientHugging Face HubHugging Face Local PipelinesHuggingface TextGen InferenceJavelin AI Gateway TutorialJSONFormerKoboldAI APILlama.cppLLM Caching integrationsManifestMinimaxModalMosaicMLNLP CloudOctoAIOllamaOpaquePromptsOpenAIOpenLLMOpenLMPetalsPipelineAIPredibasePrediction GuardPromptLayer OpenAIRELLMReplicateRunhouseSageMakerEndpointStochasticAINebula (Symbl.ai)TextGenTitan TakeoffTogether AITongyi QwenvLLMWriterXorbits Inference (Xinference)YandexGPTChat modelsDocument loadersDocument transformersText embedding modelsVector storesRetrieversToolsAgents and toolkitsMemoryCallbacksChat loadersComponentsLLMsLLM Caching integrationsOn this pageLLM Caching integrationsThis notebook covers how to cache results of individual LLM calls using different caches.from langchain.globals import set_llm_cachefrom langchain.llms import OpenAI# To make the caching really obvious, lets use a slower model.llm = OpenAI(model_name="text-davinci-002", n=2, best_of=2)In Memory Cache‚Äãfrom langchain.cache import InMemoryCacheset_llm_cache(InMemoryCache())# The first time, it is not yet in cache, so it should take longerllm("Tell me a joke")    CPU times: user 52.2 ms, sys: 15.2 ms, total: 67.4 ms    Wall time: 1.19 s    "\n\nWhy couldn't the bicycle stand up by itself? Because it was...two tired!"# The second time it is, so it goes fasterllm("Tell me a joke")    CPU times: user 191 ¬µs, sys: 11 ¬µs, total: 202 ¬µs    Wall time: 205 ¬µs    "\n\nWhy couldn't the bicycle stand up by itself? Because it was...two tired!"SQLite Cache‚Äãrm .langchain.db# We
This notebook covers how to cache results of individual LLM calls using different caches.
This notebook covers how to cache results of individual LLM calls using different caches. ->: Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKProvidersAnthropicAWSGoogleMicrosoftOpenAIMoreComponentsLLMsLLMsAI21Aleph AlphaAmazon API GatewayAnyscaleArceeAzure MLAzure OpenAIBaidu QianfanBananaBasetenBeamBedrockBittensorCerebriumAIChatGLMClarifaiCohereC TransformersCTranslate2DatabricksDeepInfraDeepSparseEden AIFireworksForefrontAIGCP Vertex AIGooseAIGPT4AllGradientHugging Face HubHugging Face Local PipelinesHuggingface TextGen InferenceJavelin AI Gateway TutorialJSONFormerKoboldAI APILlama.cppLLM Caching integrationsManifestMinimaxModalMosaicMLNLP CloudOctoAIOllamaOpaquePromptsOpenAIOpenLLMOpenLMPetalsPipelineAIPredibasePrediction GuardPromptLayer OpenAIRELLMReplicateRunhouseSageMakerEndpointStochasticAINebula (Symbl.ai)TextGenTitan TakeoffTogether AITongyi QwenvLLMWriterXorbits Inference (Xinference)YandexGPTChat modelsDocument loadersDocument transformersText embedding modelsVector storesRetrieversToolsAgents and toolkitsMemoryCallbacksChat loadersComponentsLLMsLLM Caching integrationsOn this pageLLM Caching integrationsThis notebook covers how to cache results of individual LLM calls using different caches.from langchain.globals import set_llm_cachefrom langchain.llms import OpenAI# To make the caching really obvious, lets use a slower model.llm = OpenAI(model_name="text-davinci-002", n=2, best_of=2)In Memory Cache‚Äãfrom langchain.cache import InMemoryCacheset_llm_cache(InMemoryCache())# The first time, it is not yet in cache, so it should take longerllm("Tell me a joke")    CPU times: user 52.2 ms, sys: 15.2 ms, total: 67.4 ms    Wall time: 1.19 s    "\n\nWhy couldn't the bicycle stand up by itself? Because it was...two tired!"# The second time it is, so it goes fasterllm("Tell me a joke")    CPU times: user 191 ¬µs, sys: 11 ¬µs, total: 202 ¬µs    Wall time: 205 ¬µs    "\n\nWhy couldn't the bicycle stand up by itself? Because it was...two tired!"SQLite Cache‚Äãrm .langchain.db# We