{
  "name": "Reduce LLM Costs with Semantic Caching using Redis Vector Store and HuggingFace",
  "nodes": [
    {
      "id": "44708aff-a859-4d15-923f-b33f38000db7",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1744,
        -400
      ],
      "parameters": {
        "width": 580,
        "height": 1216,
        "content": "## Try it out!\n\n### This workflow implements an intelligent semantic caching system that reduces LLM costs and improves response times by caching similar queries.\n\n### How It Works\n\n**Chat Trigger → V"
      }
    },
    {
      "id": "7c408b00-c33a-4953-94c6-007cde96a416",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1024,
        672
      ],
      "parameters": {
        "width": 736,
        "height": 136,
        "content": "### Tuning the Cache\nAdjust the `distanceThreshold` in the `Analyze results from store` node to control cache sensitivity:\n- **Lower threshold** (e.g., 0.2): More strict matching, fewer false positive"
      }
    },
    {
      "id": "485fe302-f0d6-4889-812e-549244cf5ffe",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -112,
        496
      ]
    },
    {
      "id": "08cc2596-a105-45ce-9645-158607387623",
      "name": "Redis Chat Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryRedisChat",
      "position": [
        64,
        496
      ]
    },
    {
      "id": "50407adf-e53c-4a6c-a32f-0bd2a62425b3",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -1088,
        288
      ]
    },
    {
      "id": "acc2d01d-4694-47fc-88f5-f79c2ca47d36",
      "name": "Analyze results from store",
      "type": "n8n-nodes-base.code",
      "position": [
        -528,
        288
      ]
    },
    {
      "id": "9a49690b-fab4-4759-a633-9f7dfcef2be7",
      "name": "Check for similar prompts",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreRedis",
      "position": [
        -864,
        288
      ]
    },
    {
      "id": "70b60272-9e7f-4d06-9d3c-2da8d5c83ee5",
      "name": "Respond to Chat (from semantic cache)",
      "type": "@n8n/n8n-nodes-langchain.chat",
      "position": [
        -32,
        -32
      ]
    },
    {
      "id": "00e5c3d4-eada-4caf-bf5e-a3b49d73196e",
      "name": "Respond to Chat (from LLM)",
      "type": "@n8n/n8n-nodes-langchain.chat",
      "position": [
        768,
        288
      ]
    },
    {
      "id": "f7919340-de57-41ca-a2b6-d41528ed6c34",
      "name": "LLM Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -80,
        288
      ]
    },
    {
      "id": "ad7dfa5e-4687-4cce-be6f-694779b7ff83",
      "name": "Store entry in cache",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreRedis",
      "position": [
        304,
        288
      ]
    },
    {
      "id": "e60f4189-4d99-4f95-98d5-2485a80509a0",
      "name": "Add response as metadata",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        400,
        496
      ]
    },
    {
      "id": "2546c015-da8f-4481-bb61-87113f82e206",
      "name": "Recursive Character Text Splitter",
      "type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
      "position": [
        496,
        672
      ]
    },
    {
      "id": "16887dcb-ccba-44a7-a64e-eb99db4256bc",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        288,
        -32
      ],
      "parameters": {
        "width": 576,
        "height": 152,
        "content": "### Embedding model\nObviously using your own model to calculate the embeddings would not only increase performance but may also drastically reduce the costs.\n\nEven with the existing popular models tho"
      }
    },
    {
      "id": "d7cf0fd0-c042-45b5-8a24-29288c92cea4",
      "name": "Is this a cache hit?",
      "type": "n8n-nodes-base.if",
      "position": [
        -304,
        288
      ]
    },
    {
      "id": "96820700-4963-492f-a91f-b039d6efba03",
      "name": "Embeddings HuggingFace Inference",
      "type": "@n8n/n8n-nodes-langchain.embeddingsHuggingFaceInference",
      "position": [
        -864,
        480
      ]
    },
    {
      "id": "e5b313d3-ce98-4188-a593-890f5ae09b6b",
      "name": "Embeddings HuggingFace Inference1",
      "type": "@n8n/n8n-nodes-langchain.embeddingsHuggingFaceInference",
      "position": [
        256,
        496
      ]
    },
    {
      "id": "56925ea7-b773-486b-91af-c26837844e94",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1104,
        -400
      ],
      "parameters": {
        "width": 884,
        "height": 640,
        "content": "\n## Initialize the index\n_(manually creating the index can be done using the [langchain library](https://docs.langchain.com/oss/javascript/integrations/vectorstores/redis) )_\n\nIn case your Redis deplo"
      }
    },
    {
      "id": "b47e75f4-9b9e-43ca-b100-95a88728dc83",
      "name": "When clicking ‘Execute workflow’",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -864,
        -128
      ]
    },
    {
      "id": "e4595eb8-ad5e-47be-b718-3897fcd8d120",
      "name": "Initialize Redis store",
      "type": "@n8n/n8n-nodes-langchain.vectorStoreRedis",
      "position": [
        -512,
        -272
      ]
    },
    {
      "id": "7988808f-8d22-40c5-afe4-2e26d0d676f5",
      "name": "Process sample data",
      "type": "@n8n/n8n-nodes-langchain.documentDefaultDataLoader",
      "position": [
        -528,
        -64
      ]
    },
    {
      "id": "66828a6a-00ec-4377-b5bc-c25808050b34",
      "name": "Use Huggingface for embeddings",
      "type": "@n8n/n8n-nodes-langchain.embeddingsHuggingFaceInference",
      "position": [
        -720,
        80
      ]
    },
    {
      "id": "5e69ae73-70e5-463d-8e90-df327211d47f",
      "name": "Recursive Character Text Splitter1",
      "type": "@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter",
      "position": [
        -528,
        80
      ]
    }
  ],
  "connections": {
    "LLM Agent": {
      "main": [
        [
          {
            "node": "Store entry in cache",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "LLM Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Redis Chat Memory": {
      "ai_memory": [
        [
          {
            "node": "LLM Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Process sample data": {
      "ai_document": [
        [
          {
            "node": "Initialize Redis store",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Is this a cache hit?": {
      "main": [
        [
          {
            "node": "Respond to Chat (from semantic cache)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "LLM Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Store entry in cache": {
      "main": [
        [
          {
            "node": "Respond to Chat (from LLM)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add response as metadata": {
      "ai_document": [
        [
          {
            "node": "Store entry in cache",
            "type": "ai_document",
            "index": 0
          }
        ]
      ]
    },
    "Check for similar prompts": {
      "main": [
        [
          {
            "node": "Analyze results from store",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Analyze results from store": {
      "main": [
        [
          {
            "node": "Is this a cache hit?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "Check for similar prompts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Use Huggingface for embeddings": {
      "ai_embedding": [
        [
          {
            "node": "Initialize Redis store",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings HuggingFace Inference": {
      "ai_embedding": [
        [
          {
            "node": "Check for similar prompts",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Embeddings HuggingFace Inference1": {
      "ai_embedding": [
        [
          {
            "node": "Store entry in cache",
            "type": "ai_embedding",
            "index": 0
          }
        ]
      ]
    },
    "Recursive Character Text Splitter": {
      "ai_textSplitter": [
        [
          {
            "node": "Add response as metadata",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "Recursive Character Text Splitter1": {
      "ai_textSplitter": [
        [
          {
            "node": "Process sample data",
            "type": "ai_textSplitter",
            "index": 0
          }
        ]
      ]
    },
    "When clicking ‘Execute workflow’": {
      "main": [
        [
          {
            "node": "Initialize Redis store",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}