{
  "source_file": "0930",
  "source_platform": "Ghost",
  "author": "Deep Learning Nerds",
  "title_listed": "How to Cache LangChain Responses in Python",
  "keywords": "",
  "domain": "大模型算法",
  "content_type": "技术教程",
  "is_paid_flag": "否",
  "url": "https://www.deeplearningnerds.com/langchain-cache-responses-python/",
  "publish_date": "2026-06",
  "corpus_stage": "",
  "notes": "",
  "id": "21ec983020ee",
  "collected_at": "2026-10-02T12:24:53",
  "title": "How to Cache LangChain Responses in Python",
  "body_markdown": "📘 Introduction\nWhen you build AI apps, some users ask the same question more than once. Your app may also send the same prompt during testing, retries, demos, or repeated workflows. If every identical prompt goes back to the model provider, you can waste time and money.\nIn this tutorial, you will learn how to cache LangChain chat model responses in Python. We will start with a normal\nChatOpenAI\ncall, then add an in-memory cache, and finally use a small SQLite cache file so cached responses can survive between script runs.\n💡\nCaching is useful when the exact same prompt and model settings are used again. It is not the same thing as chat memory, RAG, or provider-side prompt caching.\n💡 Why caching matters\nA cache stores a previous result and reuses it when the same request appears again. In an LLM app, this can make repeated responses faster and reduce unnecessary API calls.\nFor example, imagine a learning app that explains the same concept to many users. If the prompt, model name, and model settings are identical, the app can reuse the cached answer instead of calling the model again.\n✅\nLangChain model caching is best for exact repeats. If the prompt changes by even a small amount, it may count as a new request.\n✅ Prerequisites\nBefore getting started, make sure you have:\n☑️ Python installed\n☑️ Basic Python knowledge\n☑️ An OpenAI API key\n☑️ A terminal or command prompt\n⚙️1️⃣ Create a project folder\nCreate a new project folder for this tutorial:\nmkdir langchain-response-cache\ncd langchain-response-cache\n🐍2️⃣ Create a virtual environment\nCreate and activate a virtual environment:\npython -m venv .venv\nsource .venv/bin/activate\nOn Windows, activate it with:\n.venv\\Scripts\\activate\n📦3️⃣ Install libraries\nInstall the LangChain packages we need. The community package is used later for the SQLite cache example.\npip install langchain-core langchain-openai langchain-community\n🔐4️⃣ Set your API key\nSet your OpenAI API key as an environment variable. Replace the placeholder with your own key and never commit real secrets to a repository.\nexport OPENAI_API_KEY=\"your_api_key_here\"\nOn Windows PowerShell, use:\n$env:OPENAI_API_KEY=\"your_api_key_here\"\n🧪5️⃣ Create a normal LangChain response script\nFirst, create a normal script without caching. This gives us a baseline so the cache behavior is easier to understand.\nCreate a file named\nnormal_response.py\nand add this code:\nimport time\n\nfrom langchain_openai import ChatOpenAI\n\nmodel = ChatOpenAI(model=\"gpt-4.1-mini\", temperature=0)\nquestion = \"Explain LangChain caching in two short sentences.\"\n\nfor run_number in range(1, 3):\n    start = time.perf_counter()\n    response = model.invoke(question)\n    elapsed = time.perf_counter() - start\n\n    print(f\"Run {run_number}: {elapsed:.2f} seconds\")\n    print(response.content)\n    print(\"-\" * 40)\nnormal_response.py\nRun the script:\npython normal_response.py\nBoth runs call the model. The timing will vary, but the second run still needs to wait for a model response because no LangChain cache is active yet.\n🎓\nWant the full Academy version? Next we add LangChain caching, compare the result, persist responses with SQLite, and cover common troubleshooting issues.\nYou can view this post with the tier:\n      Academy Membership\nJoin academy now to read the post and get access to the full library of premium posts for academy members only.\nJoin Academy\nAlready have an account?\nSign In",
  "metadata": {
    "title": "How to Cache LangChain Responses in Python",
    "description": "Learn how to cache LangChain chat model responses in Python with InMemoryCache and SQLiteCache so repeated prompts can return faster and avoid unnecessary model calls.",
    "keywords": null,
    "author": null,
    "og:site_name": "Deep Learning Nerds | The ultimate Learning Platform for AI and Data Science",
    "og:type": "article",
    "og:title": "How to Cache LangChain Responses in Python",
    "og:description": "Learn how to cache LangChain chat model responses in Python with InMemoryCache and SQLiteCache so repeated prompts can return faster and avoid unnecessary model calls.",
    "og:url": "https://www.deeplearningnerds.com/langchain-cache-responses-python/",
    "og:image": "https://storage.ghost.io/c/5b/26/5b26e983-a59f-4f4b-a5f2-bd8c5a0a38e2/content/images/size/w1200/2026/06/How-to-Cache-LangChain-Responses-in-Python.png",
    "og:image:width": "1200",
    "og:image:height": "839",
    "twitter:card": "summary_large_image",
    "twitter:title": "How to Cache LangChain Responses in Python",
    "twitter:description": "Learn how to cache LangChain chat model responses in Python with InMemoryCache and SQLiteCache so repeated prompts can return faster and avoid unnecessary model calls.",
    "twitter:url": "https://www.deeplearningnerds.com/langchain-cache-responses-python/",
    "twitter:image": "https://storage.ghost.io/c/5b/26/5b26e983-a59f-4f4b-a5f2-bd8c5a0a38e2/content/images/size/w1200/2026/06/How-to-Cache-LangChain-Responses-in-Python.png",
    "twitter:label1": "Written by",
    "twitter:data1": "AI Developer",
    "twitter:label2": "Filed under",
    "twitter:data2": "LangChain, Python, OpenAI, Artificial Intelligence",
    "twitter:site": "@DLNerds",
    "article:published_time": "2026-06-05T12:13:30.000Z",
    "article:modified_time": "2026-06-05T12:13:30.000Z",
    "article:tag": "Artificial Intelligence",
    "article:publisher": "https://www.facebook.com/deeplearningnerds/",
    "article:author": "https://www.facebook.com/deeplearningnerds/"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/21ec983020ee/code/body-000.txt",
      "chars": 58,
      "language": "text"
    },
    {
      "file": "attachments/21ec983020ee/code/body-001.txt",
      "chars": 46,
      "language": "text"
    },
    {
      "file": "attachments/21ec983020ee/code/body-002.txt",
      "chars": 22,
      "language": "text"
    },
    {
      "file": "attachments/21ec983020ee/code/body-003.txt",
      "chars": 63,
      "language": "text"
    },
    {
      "file": "attachments/21ec983020ee/code/body-004.txt",
      "chars": 41,
      "language": "text"
    },
    {
      "file": "attachments/21ec983020ee/code/body-005.txt",
      "chars": 39,
      "language": "text"
    },
    {
      "file": "attachments/21ec983020ee/code/body-006.txt",
      "chars": 419,
      "language": "text"
    },
    {
      "file": "attachments/21ec983020ee/code/body-007.txt",
      "chars": 25,
      "language": "text"
    }
  ],
  "images": [],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://www.deeplearningnerds.com/membership/",
      "label": "Join Academy",
      "type": "a"
    },
    {
      "href": "https://www.deeplearningnerds.com/signin/",
      "label": "Sign In",
      "type": "a"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:47:42Z",
  "source_url": "https://www.deeplearningnerds.com/langchain-cache-responses-python/",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 3398,
  "expected_body_images": 0,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/21ec983020ee/pages/source.html",
  "local_html": "attachments/21ec983020ee/pages/article.html",
  "body_base_path": "attachments/21ec983020ee/pages/",
  "body_markdown_path": "attachments/21ec983020ee/pages/article.md"
}