Skip to content
NLEN
Illustration: Routing and managing retrieval traffic through the gateway

Retrieval traffic through the gateway: routing, rate limiting, and budgeting embeddings calls

By Ivo Donker — compiled with AI assistance (Claude & Gemini)

In most production environments working with Large Language Models, the generative call receives all the infrastructural attention. Organizations set up proxy layers for load balancing, monitor token latencies, and build failover mechanisms around endpoints like Claude, GPT-4, or local open-weight models. In the process, the retrieval infrastructure often remains an architectural blind spot. Embeddings calls for vector searches, document ingestion, and cross-encoder reranking services are dispatched directly from application servers to external providers without centralized routing, throughput control, or budgeting.

In practice, this leads to severe operational issues. When a background job vectorizes millions of document chunks simultaneously, the provider's global rate limit is exhausted within seconds, immediately causing interactive user searches to fail with HTTP 429 errors. Furthermore, there is a total lack of visibility into the true costs per tenant or search query. Structurally routing retrieval traffic through the same central gateway as chat and completion traffic creates a controlled, scalable, and auditable AI pipeline. Those wishing to explore the conceptual foundations of vector representations can first read about how semantic search works with an embeddings API to see how raw text is mathematically transformed into dense vectors.

The anatomy of retrieval traffic in production

Retrieval traffic is fundamentally different in character from generative LLM traffic. Whereas generative calls are characterized by relatively low frequencies, long-lived streaming connections, and asymmetric payload sizes (a short prompt yields a long generated output), retrieval traffic exhibits three distinct sub-patterns, each with its own throughput and latency requirements:

Traffic Type Payload & Batching Latency Tolerance Failure Impact
Runtime Query Vectorization Single string (10–100 tokens) Critical (< 80 ms) User experiences immediate latency or a failed search query
Bulk Ingestion & Indexing Large arrays (batches of 64–2048 chunks) Asynchronous / high (> minutes) Indexing is delayed, no immediate user impact
Cross-Encoder Reranking Query + N candidate documents (20–100 pairs) Highly sensitive (< 150 ms) Suboptimal context selection or necessary fallback to lexical BM25

When all these streams flow unfiltered over the same API key and the same endpoint, bulk background processes compete directly with synchronous user interactions. A central gateway must be able to identify these streams based on headers, payload structure, or tenant tokens, and route them along separate routing and rate-limiting paths. The need for a robust proxy layer becomes evident as soon as teams decide to set up a self-hosted LLM gateway for failover and configuration management, because a central gateway is the only place where policies can be reliably enforced across all API streams.

Architecture: The combined gateway layer

A gateway that orchestrates both generative LLM calls and retrieval calls sits between the application layer (RAG orchestrators, search microservices, ingestion workers) and upstream providers (such as OpenAI, Cohere, Voyage AI, or locally hosted TEI instances based on Hugging Face Text Embeddings Inference). The incoming request sequentially passes through authentication, payload validation, cache lookup, rate limiting, and provider dispatch.

The primary architectural bottleneck in retrieval lies in payload inspection. A generic LLM call typically contains a structured messagesarray. An embeddings call contains an inputfield that can vary from a single 20-character string to a JSON array containing thousands of text chunks of 500 tokens each. The gateway must be able to quickly estimate the token count of these arrays without JSON deserialization and tokenization creating a CPU bottleneck for total network latency.

// Inkomend verzoek naar de centrale gateway
POST /v1/embeddings
Host: gateway.intern
Authorization: Bearer sec_tenant_98234
X-Traffic-Class: interactive-search

{
  "model": "text-embedding-3-small",
  "input": "Hoe configureer ik mutual TLS op een interne proxy?",
  "dimensions": 512
}

// Proxy-reactie na interne verificatie, rate check en upstream routering
HTTP/1.1 200 OK
Content-Type: application/json
X-Gateway-Latency-MS: 28
X-Gateway-Cost-EUR: 0.0000004
X-Tenant-Remaining-Budget-EUR: 142.85

Routing strategies and the dimensionality pitfall

With generative models, dynamic fallback is relatively forgiving: when Claude 3.5 Sonnet is temporarily unavailable, a gateway can automatically route the request to GPT-4o or a local open-weight model with minimal prompt translation. With embedding models, this dynamic flexibility is fundamentally impossible due to vector compatibility.

Each embedding model projects text into a unique mathematical vector space with a specific number of dimensions (such as 384, 768, 1024, or 1536 dimensions) and its own semantic topology. Vectors generated by text-embedding-3-small can under no circumstances be compared with vectors from Cohere's embed-multilingual-v3.0 or a local BGE model. Setting up a dynamic fallback to a different model type renders semantic distance measurements via cosine similarity in the vector database completely useless, resulting in corrupted search results.

Routing within the gateway for embeddings therefore follows strict rules:

For deeper insight into when a combined RAG pipeline requires an additional ranking layer on top of vector similarity, the overview on which retrieval model you choose between embeddings and rerankers on hub.llmnet.nl a clear conceptual delineation.

Rate limiting and priority queues for embeddings

Because bulk document ingestion requires hundreds of thousands of embeddings at once, indexing jobs can consume an API account's entire tokens-per-minute (TPM) budget within seconds. Without gateway intervention, this immediately blocks all interactive search queries from regular users.

The most effective solution is implementing a tiered token bucket algorithm with separate priority queues. The gateway reserves a fixed percentage of the allowed TPM quota for interactive traffic (class interactive-search), while background jobs (class batch-ingestion) are routed through a leaky bucket that automatically throttles as the upstream rate limit approaches. Anyone looking to implement the mathematical mechanism behind this throughput control can read the details in the article on the token bucket algorithm in an LLM gateway.

Priority Class Reserved Quota Max Allowed Latency / Wait Time Behavior on Quota Depletion
Interactive Queries 70% of global TPM 50 ms Immediate failover to secondary region/key; never buffer
Background Ingestion 30% of global TPM + remaining capacity 300,000 ms Automatically pause and drain gradually via queue

Caching strategies for embeddings and reranking

In production environments, identical search queries recur frequently: helpdesk applications, document search engines, and internal chatbots receive dozens of synonymous or identical questions every day. Caching at the gateway layer prevents unnecessary API calls and slashes search latency from ~60 ms to less than 2 ms.

The gateway can apply two forms of caching for retrieval traffic:

Semantic caching of embeddings (reusing a vector if an earlier query is semantically similar) is generally a costly fallacy: to determine the semantic similarity of an incoming query, that query must already be vectorized. Consequently, the external API call has already been made, completely erasing the cost benefit.

Cost accounting and per-tenant attribution

Embeddings appear inexpensive on a per-request basis (often fractions of a cent per thousand tokens), but during continuous enterprise data synchronization, volumes easily escalate to hundreds of millions of tokens per month. When multiple tenants or internal departments share the same infrastructure, direct cost attribution is essential to prevent surprise invoices and unfair cost distribution.

Here, the gateway serves as a central metering station. Every incoming request is logged with its associated tenant ID, the number of processed tokens, the model used, and the current cost price. This allows a virtual balance to be tracked in real time and hard budget limits to be enforced. For the specific setup of this accounting logic, we refer to the article on Allocating API Costs per End User in a SaaS Product, where the integration between gateway meters and tenant balances is detailed step by step.

# Pseudocode: Gateway metering middleware voor embeddings
def handle_embeddings_request(request):
    tenant_id = extract_tenant(request)
    token_count = fast_estimate_tokens(request.payload["input"])
    unit_price = get_model_price(request.payload["model"])
    estimated_cost = token_count * unit_price

    # 1. Pre-flight budgetcontrole
    if not budget_service.has_sufficient_funds(tenant_id, estimated_cost):
        return http_error(402, "Payment Required: Retrieval quota exceeded")

    # 2. Exacte hash cache controle
    cache_key = generate_cache_key(request.payload)
    if cached_vector := cache.get(cache_key):
        metrics.record_cache_hit(tenant_id)
        return json_response(cached_vector, headers={"X-Cache": "HIT"})

    # 3. Uitvoeren via circuit breaker en rate limiter
    response, latency = upstream_client.post_with_retry(request.payload)

    # 4. Exacte afboeking op tenant-saldo
    actual_tokens = response["usage"]["total_tokens"]
    actual_cost = actual_tokens * unit_price
    budget_service.deduct_funds(tenant_id, actual_cost)
    
    # 5. Resultaat wegschrijven naar cache
    cache.set(cache_key, response["data"], ttl_seconds=86400)
    
    return json_response(response, headers={"X-Cache": "MISS"})

Failure Modes, Circuit Breakers, and Degradation Strategies

When an external embeddings provider experiences elevated error rates or extreme network latencies (> 2000 ms), it must not bring down the application's entire search system. A gateway must have an active circuit breaker that recognizes failure conditions and intervenes immediately.

The standard mitigation strategy for retrieval traffic relies on four consecutive phases:

  1. Detection: The gateway tracks the percentage of HTTP 5xx and 429 error responses over a 30-second rolling window. If this exceeds the 5% threshold, the circuit breaker trips to the state Open.
  2. Fast-Fail: Incoming batch ingestion jobs are paused immediately with a standardized status code to prevent further overloading upstream servers.
  3. Graceful Degradation for Search Queries: For interactive search queries, the gateway sends a signal header to the backend application (for example, X-Retrieval-Fallback: lexical-only). Based on this, the application immediately falls back to traditional full-text search methods (such as BM25 or Elasticsearch lexical search), ensuring the end user still receives relevant results without requiring semantic vectorization.
  4. Half-Open Recovery: After a 60-second cooldown period, the gateway routes 1% of interactive queries through to verify whether the upstream provider has recovered, before reopening the entire queue.

The underlying principles and architectural patterns for managing such outages are covered in detail in the guide on Designing Graceful Degradation for LLM Outages.

Measurement Methods, Monitoring, and SLI/SLO Definition

To maintain reliable retrieval traffic in production, generic HTTP success rates are not enough. A gateway must collect specific Service Level Indicators (SLIs) that provide insight into the actual performance of the search infrastructure:

By exporting these metrics in real time to Prometheus or OpenTelemetry, anomalies such as creeping API price increases or degrading upstream network connections can be detected within minutes.

Edge cases and infrastructural constraints

When routing retrieval traffic through a central proxy, specific edge cases arise that differ from standard API processing:

Operational summary and implementation path

Centralizing retrieval traffic within an LLM gateway transforms vectorization from a fragile side activity into a mature, manageable component of the AI architecture. By establishing strict separation between interactive queries and heavy background ingestion, sudden outages and unexpected cost spikes are structurally eliminated.

The recommended implementation order starts with rerouting all embeddings endpoints to the central proxy and setting up the exact hash cache. Next, priority queues are configured to protect interactive search traffic, followed by activating per-tenant budget monitoring and circuit breakers. With these layers in place, the retrieval pipeline is resilient to peak loads and ready for large-scale production deployment.