OpenAI-compatible AI gateway: cache, budgets, rate limits
An OpenAI-compatible AI gateway with a semantic cache in front: how Crowkis handles a request, enforces budgets and rate limits, and where it stops.
An OpenAI-compatible AI gateway is an HTTP proxy that accepts the same POST /v1/chat/completions request your client already sends, then adds caching, budgets and rate limits before it forwards to the provider. The Crowkis gateway puts a semantic cache in front of the upstream: a request that matches an earlier one in meaning is answered from cache, with no provider call.
In plain words. Change one base URL. Repeated questions stop reaching the provider, each tenant gets a spending wall, and your application code stays as it is.
What does the gateway do with a request?
The gateway is an opt-in HTTP endpoint on the Crowkis server. Your client sends its usual chat completion request to Crowkis instead of to the provider, and four things can happen:
- Hit. A semantically matching prior request is served from cache. There is no upstream call and no token cost, and the response carries the header
x-crowkis-cache: hit. - Miss. The request is forwarded to the upstream, the response is cached, and it is returned to the client.
- Streaming. A request with
stream:trueis proxied transparently. It is not cached yet. - Blocked. If the tenant is past its budget circuit breaker, the upstream call is refused with a 402.
flowchart LR
A[Client] --> B[Crowkis gateway]
B -->|semantic match| C["Cached answer, header x-crowkis-cache: hit"]
B -->|miss| D{"Tenant within budget?"}
D -->|yes| E[Upstream provider]
E --> F[Cache the answer and return it]
D -->|past circuit breaker| G["402, no upstream call"]The cache in front is the same engine the Crowkis commands use, so a match is by meaning and structure, not by identical bytes. Why that needs more than a similarity score is covered in a cache hit is not a correct answer.
How do you turn the gateway on?
It is off by default. A default Crowkis build makes no outbound calls, and that stays true until you set three variables:
CROWKIS_GATEWAY=1
CROWKIS_GATEWAY_KEY=<your key>
CROWKIS_GATEWAY_UPSTREAM=https://api.openai.com| Variable | What it does |
|---|---|
CROWKIS_GATEWAY | 1 turns the gateway on |
CROWKIS_GATEWAY_KEY | the gateway key. Required |
CROWKIS_GATEWAY_UPSTREAM | the provider to forward to. Defaults to https://api.openai.com |
CROWKIS_GATEWAY_CACHE_TTL | how long gateway answers stay cached |
CROWKIS_GATEWAY_RPM, CROWKIS_GATEWAY_TPM | rate limits per virtual key |
Then point your client's base URL at Crowkis. Nothing else in the application changes: no SDK swap and no cache calls in your code. The upstream can be any provider that speaks the OpenAI chat completions format. Running the server itself is covered in the five-minute deploy.
What does the cache save, and what does it not cache?
A hit is a request that never reaches the provider, so you do not pay for it. That saves cost, and the saving grows with how repetitive your traffic is. A support bot that hears the same questions all day gains more than a tool whose every prompt is new. The mechanics are in how to cut LLM API costs.
Three limits on that statement:
- Streaming responses are not cached. They pass through untouched. If most of your traffic streams, the gateway cache will do little for you today.
- Dollar figures are estimates. The dashboard and the metrics API report
tokens_savedanddollars_saved_total. Cache entries do not yet store the provider's real token counts, so a hit is priced by an estimator, at one flat internal rate for every model. Do not reconcile a provider invoice against it, and do not read it per model. - A match must be safe, not just close. A semantic cache can serve the wrong answer if it reuses on similarity alone. Crowkis runs structural, confidence and freshness checks before it reuses.
Semantic matching is the difference from an exact cache. Cloudflare's AI Gateway documentation, for comparison, says its caching applies only to identical requests and that semantic search for caching is planned (Cloudflare AI Gateway caching). An identical-request cache misses the moment a user rewords the question.
How do budgets and the circuit breaker work?
A cache lowers the spend you meant to make. A budget caps the spend you did not, such as an agent stuck in a loop at night. CBUDGET sets a budget per tenant, and the gateway enforces it.
CBUDGET GET|SET|RESET|ALERTS <tenant> [DAILY usd] [MONTHLY usd] [COST per-1k] [ALERT pct] [CIRCUIT pct]
CBUDGET SET acme DAILY 50 MONTHLY 1000 COST 0.01 ALERT 80 CIRCUIT 100
CBUDGET GET acme
CBUDGET ALERTS acmeDAILYandMONTHLYare the limits in US dollars.COSTis the price per thousand tokens used to turn tokens into dollars.ALERTis a share of the budget. Past it, the gateway switches to cost-aware routing.CIRCUITis the circuit breaker. Past it, upstream calls get a hard 402 block.
The name comes from the circuit breaker pattern: stop calling a dependency once a condition trips, instead of letting the failure, here the bill, keep growing. Two details help in practice. The block is on upstream calls, which is where the money goes. And 402 is unusual. RFC 9110 lists 402 Payment Required as reserved for future use, so client libraries may not treat it specially. Handle it explicitly and do not retry it.
The cache and the budget work together on a runaway loop. A looping agent mostly repeats near-identical questions, so the cache absorbs many of them at no cost, and the wall stops what is left. Per-tenant budget state is in the budgets field of the metrics snapshot. For where agent spend comes from, see agent unit economics.
How do rate limits work?
There are two layers, and they sit in different places.
| Control | How you set it | Scope | Where it is enforced |
|---|---|---|---|
| Gateway rate limits | CROWKIS_GATEWAY_RPM, CROWKIS_GATEWAY_TPM | per virtual key | at the gateway |
| Tenant rate limits | CKEYLIMIT SET tenant RPM n TPM n | per tenant | on the CGET and CSET fast path |
| Budget and circuit breaker | CBUDGET SET | per tenant | at the gateway, on upstream calls |
CKEYLIMIT SET|GET|DEL tenant [RPM n] [TPM n]RPM is requests per minute and TPM is tokens per minute, the same two units OpenAI uses for its own limits (rate limits guide). A limit in front of the provider is useful for a different reason than the provider's own limit: it is per tenant. One customer's runaway integration hits its own ceiling and the others keep working.
CKEYLIMIT is the one paid command in the reference. It is Enterprise only and is compiled out of the free binary, where it answers with a pointer to the upgrade and never fails silently. CBUDGET is free.
What about retries, routing and fallback?
The gateway also does the work a raw provider call leaves to you:
- Retries. Automatic retry with exponential backoff and jitter, honouring the
Retry-Afterheader. This matches OpenAI's own advice for a 429: wait at least as long asRetry-Aftersays, and otherwise back off exponentially with a random delay (rate limits guide). The 429 status itself is defined in RFC 6585. - Routing. Multi-provider routing with weighted load balancing.
- Fallback. Failover across providers, decided by error class.
Read these with the status in mind. The feature list marks the gateway as an MVP that is still in progress, and the command reference still names multi-provider routing, fallback and cost routing as next steps. Test the behaviour on the version you run before you depend on it for availability.
How do you see what the gateway is doing?
- Per response: the
x-crowkis-cache: hitheader tells the caller the answer came from cache. - Snapshot:
GET /api/metricsreturns one JSON object. Itsglobalsection hastotal_requests,total_hits,total_misses,hit_rate_pct,tokens_savedanddollars_saved_total. There is one row per tenant, and abudgetssection. - Live:
GET /api/metrics/stream?every=2pushes that JSON as server-sent events. A node serves at most 64 streams at once and answers 429 beyond that. - Prometheus:
GET /metricsis a scrape target, and gateway metrics also appear on the dashboard.
All of these are served by the management server, on the port one above the cache port. Send your admin key in the X-Crowkis-Admin-Key header. A key that belongs to one tenant sees only that tenant's rows.
When should you not use the gateway?
- You need the cache decision in your code. The gateway decides for you. If you want to read the confidence behind a hit and apply your own rule, call
CGETwithWITHCONFIDENCEyourself. The commands are in the Crowkis command reference. - Your traffic is mostly streaming. It is proxied, not cached.
- You need endpoints other than chat completions.
POST /v1/chat/completionsis the documented route. - You need proven tail latency under heavy writes. A semantic lookup embeds the request first. The published cold figure is about 250 semantic reads per second on a development laptop, and a sustained write and search soak test missed its own stall budget. Measure on your hardware.
- You want the assistant itself to decide. For Claude Code and other MCP clients, the MCP server exposes the cache and memory as tools instead.
Common questions
Do I have to change my application code?
No. The gateway accepts the OpenAI chat completions request as it is. You change the base URL your client points at, and the request shape, the response shape and your SDK stay the same.
Does the gateway send data anywhere by default?
No. It is off by default, and Crowkis makes no outbound calls until you set CROWKIS_GATEWAY=1, a gateway key and an upstream. Once it is on, misses are forwarded to the upstream you configured, and to nothing else.
How do I know whether a response came from cache?
Check for the x-crowkis-cache: hit response header. For totals, read total_hits and hit_rate_pct from /api/metrics, or scrape /metrics with Prometheus.
What happens when a tenant runs out of budget?
Past the alert share, the gateway switches to cost-aware routing. Past the circuit breaker share, upstream calls for that tenant are blocked with a 402. CBUDGET GET shows the current state, and CBUDGET SET and CBUDGET RESET are how you change it.