New to Kestra?
Use blueprints to kickstart your first workflows.
Cache LLM answers in Redis with Kestra to cut OpenAI costs. Normalizes questions into keys and serves instant responses on cache hits.
Cut OpenAI API spend and response latency by putting a Redis caching layer in front of your LLM. This blueprint normalizes each incoming question into a deterministic cache key, looks it up in Redis with io.kestra.plugin.redis.string.Get, and serves the stored answer instantly when it exists. Only on a true cache miss does it call the model, then writes the fresh answer back to Redis for next time. It solves the classic FAQ problem where users ask the same thing in slightly different wording and you pay for the same completion again and again.
generate_cache_key task (io.kestra.plugin.scripts.python.Script) normalizes the question input (lowercase, strip punctuation, collapse whitespace) and MD5-hashes it with a cache_key_prefix, so semantically equivalent phrasings map to one key. It returns cache_key and normalized_question via Kestra.outputs.check_cache (io.kestra.plugin.redis.string.Get) reads that key from Redis with failedOnMissing: false, so a miss returns null instead of erroring.route_request (io.kestra.plugin.core.flow.If) branches on whether the cached value is present and non-empty.serve_from_cache logs the stored answer with no model call. On a miss, call_llm (io.kestra.plugin.openai.ChatCompletion, gpt-4o) generates an answer, store_in_cache (io.kestra.plugin.redis.string.Set) persists it, and log_llm_response logs the result.cache_key_prefixRedis is a cache, not a scheduler: it cannot trigger on events, retry a failed model call, or give you visibility into why a question hit or missed. Kestra wraps the whole pattern in declarative YAML, adds event triggers and retries, branches with io.kestra.plugin.core.flow.If, and records execution lineage across the Python, Redis, and OpenAI tasks so the cache layer is observable and reproducible.
gpt-4oREDIS_URL: connection URL for your Redis instanceOPENAI_API_KEY: OpenAI API key for the chat completion callREDIS_URL and OPENAI_API_KEY secrets to your namespace.question, or supply your own and a cache_key_prefix.store_in_cache so cached answers expire and refresh.cache_key_prefix for multi-tenant isolation.