Skip to main content

Answer repeated prompts from a cache: ai-cache, exact and by meaning

ai-cache on an LLM Route: the exact layer in Redis answers a repeated prompt in 60 ms instead of 1.5 s, with status headers; the semantic layer configured only.

By Efsun Balcı AI Gateway

What you will have at the end

ai-cache on the Route chat-api: a prompt sent twice is answered the second time from Redis, with X-AI-Cache-Status: HIT, in 60 to 140 ms at the client instead of a second and a half, and the model is not called. The same question with one word changed is a miss, because the exact layer matches bytes. The semantic layer, which matches by meaning, needs an embedding provider and a Redis with the search module; the lab has neither, so this article shows its form and says it was not run. Every hit is a record in Traffic Explorer with gateway as the Upstream node, and the save is audited with the Redis password and the model key masked.

Prerequisites

  • A Route to a model. chat-api serves POST /ai/v1/chat with ai-proxy to a cloud model, ai-prompt-guard and ai-prompt-decorator as in Prompt rules at the gateway, without the template, and a single token limit of 300 a minute for the Route.
  • The cloud model is a real one on a real account, called with the lab’s own key. The key is masked in every screenshot and in the audit log, and no part of it is printed here.
  • A Redis the gateway nodes can reach. The lab’s is redis:6379 with a password; Redis is the only store the Plugin offers.
  • Traffic logging turned on under Traffic Explorer › Settings, and a role that may manage Routes. The screenshots below were taken as nora.becker.

The exact layer

Open the Route, Plugins, Add plugin, ai-cache. Under Basics, Layers has two checkboxes, exact and semantic; the help says exact is always needed and semantic adds a similarity match, and the note says which prompts can reach the semantic layer at all: only a plain-text chat prompt; an image, an audio block or a tool call keeps it in the exact layer. Under Exact layer, TTL is how long an answer stays, 600 seconds here. Policy is redis, and under it the Redis host, port, database, username and password, stored on the engine and shown only to roles that manage the Route, with the TLS switches beside them.

The ai-cache form on chat-api: Layers with exact ticked and semantic clear, the note about which prompts reach the semantic layer, Exact layer TTL 600 s, Policy redis with the sentence that Redis is the only store, and Redis host One layer, ten minutes, one Redis. This install runs one gateway node, so whether a second node serves the first one’s entries from the shared Redis was not measured.

Further down, the cache key: Include consumer serves a signed-in Consumer only what was cached for them; Include variables adds request variables to the key; Share across routes is off, so every Route keeps a cache of its own. Cache status headers puts X-AI-Cache-Status on every answer and, on a hit, X-AI-Cache-Age. Bypass rules name a header and a value that send a request to the model and never cache it. Fail mode says what a request that did not pass through an AI proxy meets here.

Select Add plugin.

What a client sees

The same prompt four times, with a change in the middle:

### call 1, the prompt the first time
HTTP/1.1 200 OK  X-AI-Cache-Status: MISS  · 1.57 s
### call 2, the same prompt, byte for byte
HTTP/1.1 200 OK  X-AI-Cache-Status: HIT  X-AI-Cache-Age: 0  · 0.14 s
### call 3, "return policy" instead of "returns policy"
HTTP/1.1 200 OK  X-AI-Cache-Status: MISS  · 1.45 s
### call 4, the first prompt again
HTTP/1.1 200 OK  X-AI-Cache-Status: HIT  X-AI-Cache-Age: 2  · 0.06 s

The second and fourth answers are the first one, word for word, served from Redis; the third went to the model because one letter differed. That is what exact means, and it is the layer every installation gets. A support assistant whose users ask the same ten questions gets most of its hits here.

What Traffic Explorer shows

A hit is a record whose Upstream node is gateway: the gateway answered, and no provider was asked. A miss carries the provider’s address and its latency.

Traffic Explorer filtered to the path /ai/v1/chat: the four newest of 29 records, two 200s answered by api.anthropic.com:443 in 1.37 and 1.50 s and two 200s answered by gateway in 17 and 49 ms Two provider calls, two answers from the cache.

The semantic layer, configured and not run

Tick semantic under Layers and the Semantic layer block opens: Embedding with OpenAI or Azure OpenAI, their API key, Model, Dimensions, an Endpoint for a provider of your own, and Vector search in Redis with an Index prefix; the note under it is the one that decides whether it works: the Redis needs the RediSearch module, because without it every semantic lookup misses and only the log says so. Similarity threshold and Top K set how close a stored prompt must be, and the Message window switches say which messages of the conversation are embedded.

The Semantic layer section of the ai-cache form: the note that it is used only when Layers includes semantic, Embedding with Embed with OpenAI chosen, and the empty API key, Model and Dimensions fields of the OpenAI block The form as far as the lab can take it. No embedding provider and no search module are reachable from here, so the layer stayed unticked and nothing below was measured.

What the semantic layer would add, from the form alone: a prompt close enough to a stored one in meaning is served the stored answer, with X-AI-Cache-Similarity on the answer instead of the age. Whether “return policy” and “returns policy” land within the threshold is a measurement this article could not make.

What the audit log keeps

Adding the Plugin is a Route update with the ai-cache block added on the after side and the Redis password in it as ••••, next to the ai-proxy block whose provider key is masked on both sides. The note under the diff names the masked value that changed, plugins.ai-cache.redis_password.

The Change section of the audit record that added ai-cache to chat-api: on the after side the new ai-cache block with exact ttl 600, redis_host redis, redis_port 6379 and redis_password as four dots; on both sides the ai-proxy block with its x-api-key as four dots and the ai-prompt-decorator block Two secrets on one Route, both masked. The TTL and the host are there to read.

What to do next

Written by
Efsun Balcı
Engineering intern, G8
Was this helpful?
Next step

See G8 in a demo

We’ll show you the console with APIs and AI traffic like yours, and answer the questions these articles leave open.

  1. 01Demo
  2. 02Proof of conceptOn Docker for a quick start, or on the infrastructure you plan to run G8 on.
  3. 03LicenseWe prepare a quote for your edition and nodes, and you receive a signed license file.