Reduce LLM Costs with Semantic Caching using Redis Vector Store and HuggingFace
Needs changesWhat this workflow does
This AI workflow has 19 nodes and runs manually, when you click “Execute workflow” (plus 1 other trigger). Along the way it connects OpenAI Chat Model, Redis Chat Memory, Redis Vector Store and Embeddings Hugging Face Inference.
How it runs
Trigger: Manually, when you click “Execute workflow” (plus 1 other trigger).
- Initialize Redis store (Redis Vector Store): stores documents in the Redis vector store, with embeddings: Use Huggingface for embeddings; document loader: Process sample data
- Check for similar prompts (Redis Vector Store): searches the Redis vector store, with embeddings: Embeddings Hugging Face Inference
- Analyze results from store (Code): runs custom JavaScript code
- Is this a cache hit? (If): checks a condition and branches into true / false
- Respond to Chat (from semantic cache) (Chat): uses Chat
- LLM Agent (AI Agent): runs an AI agent, with model: OpenAI Chat Model; memory: Redis Chat Memory
- Store entry in cache (Redis Vector Store): stores documents in the Redis vector store, with embeddings: Embeddings Hugging Face Inference; document loader: Add response as metadata
- Respond to Chat (from LLM) (Chat): uses Chat
Services it uses
Before you upgrade to n8n 3.0
1 thing in this workflow is removed in n8n 3.0. Fix it before you upgrade:
- AI Agent v1 (old agent modes) in LLM Agent (v1.8): replace with Latest AI Agent node (Tools Agent). For SQL Agent, use a Postgres or MySQL tool with a recent AI Agent.
| Removed in 3.0 | Replace with |
|---|---|
| AI Agent v1 (old agent modes) LLM Agent (v1.8) | Latest AI Agent node (Tools Agent). For SQL Agent, use a Postgres or MySQL tool with a recent AI Agent. |
Nodes used (12)
- OpenAI Chat Model
@n8n/n8n-nodes-langchain.lmChatOpenAi - Redis Chat Memory
@n8n/n8n-nodes-langchain.memoryRedisChat - Chat Trigger
@n8n/n8n-nodes-langchain.chatTrigger - Code
n8n-nodes-base.code - Redis Vector Store
@n8n/n8n-nodes-langchain.vectorStoreRedis - Chat
@n8n/n8n-nodes-langchain.chat - AI Agent
@n8n/n8n-nodes-langchain.agent - Default Data Loader
@n8n/n8n-nodes-langchain.documentDefaultDataLoader - Recursive Character Text Splitter
@n8n/n8n-nodes-langchain.textSplitterRecursiveCharacterTextSplitter - If
n8n-nodes-base.if - Embeddings Hugging Face Inference
@n8n/n8n-nodes-langchain.embeddingsHuggingFaceInference - Manual Trigger
n8n-nodes-base.manualTrigger
From the author
Stop Paying for the Same Answer Twice Your LLM is answering the same questions over and over. "What's the weather?" "How's the weather today?" "Tell me about the weather." Same answer, three API calls, triple the cost. This workflow fixes that. What Does It…