What I originally wanted was an answer cache for a language model. If someone asks the same question and the previous answer is still good, return it directly and spare the model another generation. I intended to build it myself but first wanted to see whether anyone had already built something similar.
I did not yet know “LLM gateway” was an established term. In my experience a layer between an application and a model that returns cache hits, calls the model on misses, and manages logs and costs sounded like some kind of gateway. I searched for “LLM gateway” and “AI gateway” and discovered that this product category actually existed. Higress and Apache APISIX were already working on it.123
An AI gateway is a common entry point for applications calling models. Suppose a summarizer and a Q&A application share DeepSeek. Following one request reveals what the gateway does.
| Following a Q&A request | What does a conventional API gateway already handle? | What else must be decided for models? |
|---|---|---|
| Access and authentication | Check gateway credentials, identify callers and verify service access. | Which models may the summarizer and Q&A app use? Who keeps the upstream DeepSeek key?45 |
| Security and content filtering | Inspect paths, headers and bodies and allow or reject by rule. | Which conversation content may go to an external model? Can the generated answer go to the user unchanged?63 |
| Answer cache | Look up a still-valid response by key and return it directly on a hit. | Can a rephrased question reuse an answer? Are both users authorized to see it?72 |
| Model proxying and routing | Select upstreams, distribute traffic and fail over. | Does the backup model support capabilities the application needs? Who translates request formats when providers change?3 |
| Rate limits, quotas and costs | Count by caller and time window and reject requests over the limit. | Output token counts arrive only after generation. When should quota be debited? How are cache hits charged?48 |
| Logs and observability | Record caller, status and duration. | Which model and how many tokens were used? How long until the user saw the first character?43 |
Take a request from the Q&A application. A route first matches conditions such as the path, determining which plugins will process it. If forwarding is required, the upstream is the backend that actually receives the request. Load balancing can distribute traffic among multiple instances. In aneasystone’s example, a route matches GET /web1 and sends it to the upstream web1:80.9 With DeepSeek, ai-proxy sends the request in the provider’s format. Usage returned in the response can then feed accounting.1011123
Suppose the summarizer and Q&A application each receive 100,000 tokens. When Q&A returns a cached answer, should it spend quota? If the summarizer uses up its allocation first, can Q&A keep running?
What gets filtered before input leaves and before an answer reaches the user?
Suppose someone pastes an email with a phone number in its signature into the summarizer. If only the body needs summarizing, replace the number with a placeholder before sending processed text to DeepSeek. If the entire document is prohibited from leaving, reject it at the entry point. Zhao Weiji’s Higress tutorial gives a tightly scoped example using Request Block to check sensitive URL fields. To filter text pasted into a chat box, inspect messages in the request body. APISIX 3.12’s ai-prompt-guard can check the last message or the whole conversation against allow or deny rules. Patterns can recognize specified text. The application must still verify document ownership and the current user’s right to read it.133
Another checkpoint is the returning answer. Higress’s content safety tutorial configures checkRequest and checkResponse separately, sending input and output to a content safety service. Accounting differs depending on the checkpoint. Blocking input before forwarding incurs no DeepSeek generation usage. Blocking output after the model answered still incurs generation costs. Recording the two outcomes separately explains why some requests that gave the user no answer still cost money.6
Must DeepSeek run again when two people ask the same question?
Suppose Q&A just answered “How do I reset my company email password?” and the gateway stored the shareable answer. If a second person asks the same thing, is authorized to see it, and the instructions remain current, the gateway can return the old answer and skip DeepSeek entirely. This is an exact match. If they ask “What if I forgot my work email password?”, the text differs. A semantic cache represents the question as numbers encoding its meaning and searches for sufficiently similar past questions. Higress’s March 2025 tutorial demonstrated a text embedding service and a vector database for this. A hit saves answer generation, although the embedding service can still cost money.2
“Has my email reset request been approved?” sounds similar but requires this person’s live status. Even if two people ask exactly the same question, Alice’s approval result must not go to Bob. Here I would cache only public, stable instructions and let the application query personal status. Include the applicable policy version when caching instructions, and invalidate old answers when procedures change. Check authorization and input rules before cache access, and write to the shared cache only after an answer has passed output checks and is suitable for reuse.
A gateway answer-cache hit and a “cache hit” on a DeepSeek bill are different. For a gateway hit, record “gateway returned cached answer, zero DeepSeek generation usage.” Do not charge the usage attached to the old answer again. For a provider hit, the request has already reached DeepSeek. The model reuses a repeated input prefix but still generates a new answer. Account for cached input, remaining input and new output using the usage reported for this call.82
DeepSeek reports how much was used. Who gets charged?
Tokens measure text processed by the model. The number depends on how the model tokenizes a passage. DeepSeek’s documentation notes “每一次实际处理 token 数量以模型返回为准” (“use the count returned by the model for the actual tokens processed on each call”). Suppose the summarizer sends a long passage and usage reports prompt_tokens of 800 and completion_tokens of 200. Then total_tokens is 1,000. We know how much this call used. Who used it?14
If both applications go through my backend, the answer is simple. Authenticate the app, send the DeepSeek request, then record a row with request ID, app, time, model, 800 input and 200 output tokens. Aggregate by app to split the account. If both apps call DeepSeek directly with the same key, the aggregate for that key alone cannot reconstruct their shares. Per-user accounting also requires associating the user identity with the call at the point where the application knows it.
Converting usage to yuan requires two more figures. DeepSeek reports cached and uncached input tokens separately because their prices differ. Two calls with 800 input and 200 output tokens each can cost different amounts. A token quota can use the total. To reconcile costs, retain the categories and apply the prices for the model at the time of the call.8
What turns month-end accounting into a stop when quota runs out?
Return to the summarizer request. If 1,000 tokens remained and the response used precisely that amount, something must check the summarizer’s balance before forwarding the next request. Q&A looks up its own balance at the same entry point and can keep calling. With one backend the check can live there. Independent services can instead call one AI gateway using individual gateway credentials. Only the gateway holds the DeepSeek key. Authentication, forwarding and accounting then share an entry point.4
Choose a unit before setting a rule for the summarizer. The APISIX introductory tutorial’s limit-count counts requests in a time window. One long article and one sentence each count as one. With token-based ai-rate-limiting, the two can differ by orders of magnitude.1516 Shazi Keke discusses request frequency separately from spending in application-side cost control. Alibaba Cloud’s gateway rate-limit docs likewise distinguish request counts and token consumption.1718
Even with tokens as the unit, distinguish rate from total quota. At most 10,000 tokens per application per minute limits a burst of traffic. 100,000 tokens in total for the summarizer limits the remaining balance. A well-behaved summarizer can still exhaust its total after a few hours. Before choosing a plugin, decide whether the user should retry in a minute or wait for an administrator to add quota.4
Timing matters too. If 100 tokens remain, two concurrent requests may both see them before the first returns. Checking before forwarding and debiting actual usage afterward blocks new calls after exhaustion. For a strict ceiling, work out the maximum charge from input and maximum allowed output, reserve it before admission, and let other calls use only the unreserved remainder. Settle actual usage when the call finishes. When a plugin says it “supports rate limiting,” I first check when it counts tokens.16
Which Higress and APISIX settings matter for summarization and Q&A?
Higress set out to put traffic distribution, microservice governance and security at one entry point. It reuses Istio’s configuration distribution mechanism and Envoy handles the actual requests. Both technologies are common in service meshes, infrastructure that manages service-to-service communication, but an application can deploy Higress independently as just its ingress gateway. Higress was open-sourced in 2022. By 2024 its v1.4 release notes listed AI gateway capabilities explicitly.192021
APISIX began as a dynamically configurable general-purpose API gateway and entered the Apache Incubator in 2019. OpenResty, based on NGINX and Lua, handles traffic: once a request matches a route, APISIX runs the plugins configured for it. It can run independently or use an Ingress Controller to translate Kubernetes ingress resources into APISIX configuration. By version 3.12 in April 2025, one gateway could rate-limit ordinary routes by request count, use ai-proxy to forward a DeepSeek route and count its tokens with ai-rate-limiting. The established routing mechanism remained, with new plugins for model-specific work.229233
A natural question in Zhao Weiji’s Higress article was how an organization can manage upstream keys centrally and still see each member’s usage.13 The API7 team listed answer caching under “Intelligent Routing” in its feature overview, exactly my starting point. However, the introduction explicitly covered “当前及未来几个版本” (“the current and next several versions”).24 To determine whether differently worded questions could reuse an answer, I needed to inspect the APISIX 3.12.0 plugin and its actual cache key.257
| What I wanted to verify | Higress | Apache APISIX 3.12.0 |
|---|---|---|
| Where are input and output filtered? | Content safety can check requests and responses separately. Verify which data is submitted and what may be sent to the review service.6 | ai-prompt-guard checks prompt messages before forwarding. ai-aws-content-moderation can have AWS Comprehend check request text. Both concern input and do not establish output review.3 |
| Can Q&A directly reuse a previous answer? | The tutorial covered exact and semantic caches. The latter requires embeddings and similar-question retrieval.2 | General-purpose proxy-cache reuses HTTP responses by key. In 3.12 the default key uses host and request URI, and default methods are GET and HEAD. Chat typically POSTs different questions to one URL. Enabling POST alone can conflate them. Exact reuse requires distinguishing request content and visibility in the key. Semantic matching needs additional retrieval logic.7 |
| What can be recorded? | Create a separate consumer for each application so the gateway can recognize callers by independent credentials. The official example uses x-api-key and can display token usage by consumer.4 | ai-proxy can proxy DeepSeek and put model and token usage in access logs. Per-app accounting also requires associating records with authenticated callers.3 |
| Does Q&A retain a balance after the summarizer runs out? | Set total quotas separately for both consumers. Administrators can query, reset or add to each balance. Time-window rate limits are configured separately.4 | ai-rate-limiting defines windows for input, output or total tokens. This version directly limits overall use or specified model instances. Two apps using the same route and instance share a counter with that rule alone.16 |
| What if requests alternate between two gateways? | The official quota example stores each consumer’s balance in Redis. Gateways deducting from the same balance need the same store and quota key.4 | This plugin uses local counters in 3.12.0. Two gateways have separate counters. A shared application balance across nodes needs additional accounting.16 |
If I write my own answer cache, can a gateway plugin answer the user directly? In Yang Ming’s Higress experiment, enabling mockEnable made a plugin call SendHttpResponse on request headers. The client immediately received hello world. That response was hard-coded. For Q&A, the plugin must read the question from the body and look up a cache scoped to the knowledge base and version the user may access. Return the saved answer on a hit and call the model only on a miss.26
On APISIX you can also choose where the plugin runs. APISIX can load WebAssembly (Wasm) plugins inside the gateway, or send the request to a separate Plugin Runner process where an external plugin processes it and returns a result. The latter adds a process to maintain.27 For more on how Go can run Wasm plugins, see the article on the Go WebAssembly runtime wazero.28
I would verify quota isolation and caching with two requests. First exhaust the summarizer’s quota and ask Q&A a question absent from the cache. If upstream still generates an answer, Q&A can use its own allocation. Ask the same question again under the same conditions, then compare gateway cache logs and upstream call counts to establish whether the second request actually saved a generation.
When is a gateway plugin enough, and when does an AI gateway merit a separate product?
Back to my original answer cache: with just one Q&A application I would first implement it in the application backend. It already knows which documents the current user can see and when the email manual changes versions. Moving the cache to a gateway requires sending that information with requests or having the gateway look it up separately. For one application that integration cost may not pay off.
Once summarization and Q&A share a gateway, I would first look at what existing plugins save me. To issue separate keys, give each app 100,000 tokens and reuse common answers, I would try Higress consumer quotas and Q&A caching. If APISIX is already in use and the requirement is only to limit a model endpoint’s token consumption per minute, the 3.12 time-window rule suffices.2416
My test for a standalone product is this: would a team with no need to manage ordinary web APIs have a reason to use it on its own? Suppose separate teams own Q&A and summarization. The Q&A lead wants to specify: “Reuse public instructions for the same version. Always look up personal approval status. Log reused answers without charging generation quota.” At month’s end they also want to see which calls cost money and why. A product that lets them express those requirements in terms of apps, models, cache scope and budgets has independent value. If every team must still assemble routes, cache keys and counters, the gateway can forward traffic, but each team has to implement that business rule itself.
The product can be offered separately while sharing the underlying proxy. Envoy AI Gateway, released by Bloomberg and Tetrate in February 2025, is one example: it provides model access and token rate-limit configuration while building on Envoy Gateway.29 For how shared ingress forwards requests to Kubernetes services, see the comparison of open-source cloud-native gateways.
As an aside, AI gateways reminded me of Langfuse. Its focus is asynchronous application observability, but recording individual model calls and showing where tokens went are real areas of overlap.30
References
Footnotes
-
Higress contributors. (n.d.). Higress [source repository]. GitHub. Project homepage. ↩
-
子釉. (2025, March 3). Semantic Caching. Higress. Scenario and Configure the vector cache. ↩ ↩2 ↩3 ↩4 ↩5 ↩6
-
Lin, Y. (2025, April 8). Introducing APISIX AI Gateway. Apache APISIX. Model proxying and logging with
ai-proxy; Prompt Processing and Content Moderation. ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9 -
子釉. (2025, March 3). Token Rate Limiting. Higress. Configure consumers, Token quota configuration, and the same page’s rate-limit, quota-operation and observability examples. The tutorial demonstrates Qwen and lists DeepSeek as an integrated provider. ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9
-
中北萌新程序员. (2025, January 10). Higress in One Article. CSDN. Microservice examples for Higress route configuration and key authentication. ↩
-
子釉. (2025, March 3). Content Safety. Higress. Configure the content safety service and the request and response check switches in the example on that page. ↩ ↩2 ↩3
-
Apache APISIX contributors. (2025, April 1). proxy-cache (3.12.0). GitHub. Default cache key and request methods. ↩ ↩2 ↩3
-
DeepSeek. (2024, August 2). DeepSeek API introduces Context Caching on Disk, cutting prices by an order of magnitude. DeepSeek API Docs. Monitoring Cache Hits explains
prompt_cache_hit_tokensandprompt_cache_miss_tokens. ↩ ↩2 ↩3 -
aneasystone. (2023, February 5). Notes on Using APISIX. aneasystone’s blog. The
/web1forwarding example in “Creating Routes with the Admin API”. ↩ ↩2 -
Apache APISIX. (n.d.). Configure Routes. Apache APISIX documentation. Creating routes and verifying forwarding. ↩
-
Apache APISIX. (n.d.). Load Balancing. Apache APISIX documentation. Configuring multiple upstream nodes. ↩
-
Apache APISIX. (n.d.). ai-proxy. Apache APISIX documentation. Model proxy plugin. ↩
-
赵伟基(兆维). (2023, July 23). Using a Gateway to Reduce Model API Costs and Improve Compliance. Alibaba Cloud Native,CNBlogs. How can Higress reduce model usage costs? The out-of-the-box per-user log reports in the article use the commercial edition. ↩ ↩2
-
DeepSeek. (n.d.). Calculating Token Usage (introduction, paragraph 3, on differences between model tokenizers). DeepSeek API Docs. Usage and the response
usagefield. ↩ -
Apache APISIX. (n.d.). Rate Limiting. Apache APISIX documentation. Enabling
limit-counton a route. ↩ -
Apache APISIX contributors. (2025, April 1). ai-rate-limiting.lua (3.12.0). GitHub. Counter targets, time windows and local storage; Pre-admission check and Deducting actual usage. ↩ ↩2 ↩3 ↩4 ↩5
-
沙子可可. (n.d.). Learning LLM Development, Chapter 18: Model API Cost Control and Request Rate Limits. CSDN. Request limiting and cost monitoring in section II, Core Strategies. ↩
-
Alibaba Cloud. (n.d.). Rate Limiting. Alibaba Cloud Help Center. AI gateway rate-limit policies. ↩
-
gengleilei. (2022, November 4). Alibaba Open-Sources Its Cloud-Native Gateway. Higress Blog. Project origins and positioning at open source release. ↩
-
澄潭. (2023, September 28). Higress at KubeCon China 2023. Higress Blog. Envoy data plane and Istio configuration distribution. ↩
-
张添翼, 王晨. (2024, July 18). Higress v1.4 Adds AI Gateway Capabilities and Enhances Cloud-Native Support. Higress Blog. v1.4 release notes. ↩
-
Apache Software Foundation. (n.d.). Apache APISIX project incubation status. Apache Incubator. Project description and incubation record. ↩
-
Layzer. (2023, May 15). Getting Started with APISIX. CNBlogs. Examples covering installation, routing and authentication. See also Apache APISIX. (n.d.). Overview. How the Ingress Controller turns Kubernetes resources into gateway configuration. ↩
-
API7 technical team. (2025, February 24). Overview of APISIX AI Gateway: LLM Proxying, Token Rate Limits and Security. Zhihu, API Gateway Selection. Opening paragraph and “Intelligent Routing: Dynamic Traffic Management for AI APIs”. ↩
-
Apache APISIX contributors. (n.d.). apache/apisix [source repository]. GitHub. Project homepage. ↩
-
阳明. (2024, April 22). Could Higress Be the Best Cloud-Native Gateway Today? 51CTO (source: k8s Technology Circle). Custom plugins:
mockEnableexample and response verification. ↩ -
Apache APISIX. (n.d.). Wasm and External Plugins. Apache APISIX documentation. For a source build on ARM Ubuntu, see Apache APISIX. (2022, January 11). Building Apache APISIX on ARM Ubuntu (Install OpenResty). For OpenResty prebuilt packages and generic source installation steps, see liwang2019. (2011, June 21). Installation. OpenResty.org. Updated August 12, 2026. For contributing changes, see Apache APISIX. (n.d.). How to Contribute. ↩
-
Introducing Wazero, Tetrate’s Open-Source WebAssembly Runtime Written in Go. (n.d.). Zhihu column. ↩
-
Tetrate & Bloomberg. (2025, February 25). Tetrate and Bloomberg release open source Envoy AI Gateway, built on CNCF’s Envoy Gateway project. Tetrate. Joint release announcement. ↩
-
Rawert, C., & Klingen, M. (2024, September 23). Should you use an LLM proxy to build your application? Langfuse. Is Langfuse an LLM Proxy? ↩