Skip to content
NLEN
Illustration: Dynamic payload routing based on prompt size

Dynamic payload routing based on prompt size

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

In modern AI architectures, the size of incoming API payloads varies enormously. A single integration might process a brief user prompt of fifty tokens one moment, and receive a forty-thousand-token summarization request with multiple document attachments the next. When an application blindly forwards all these requests to the same homogeneous backend model, substantial inefficiencies arise. Short prompts pay unnecessary overhead on heavy models with slow time-to-first-token, while voluminous payloads blow past the context windows of compact, more affordable models.

Dynamic payload routing resolves this by inspecting the size of the incoming prompt prior to dispatching and routing the request to the most appropriate model or endpoint. This article falls under pillar A2 (Gateway, routing & throughput management). For an overview of the underlying infrastructure and proxy architecture, you can consult the anchor article on self-hosting an LLM gateway to see how proxy layers intercept and distribute incoming traffic.

The architecture of context-driven routing

In context-driven routing, an API gateway acts as an intelligent inspection layer between the client application and upstream model providers. Instead of a static binding between an endpoint and a specific model name, the gateway decodes the incoming HTTP body and performs a deterministic analysis of the payload's token footprint.

The routing process follows four sequential steps within the proxy pipeline:

For a deeper analysis of dispatch algorithms and fallback mechanisms, you can explore the guide on orchestrating and routing multiple models to understand how failover rules interact with payload inspection.

Defining token thresholds and model profiles

To establish an effective routing matrix, we typically categorize incoming requests into three distinct tiers based on prompt size. Each tier serves a specific class of models, offering its own balance between cost, latency, and context capacity.

Tier Token volume Typical use case Model profile
Tier 1: Micro/Standard 0 – 2,048 tokens Classification, short Q&A, entity extraction Small, fast models (low latency, low cost per token)
Tier 2: Medium 2,049 – 16,384 tokens Multilingual chat sessions, document analysis up to 10 pages Mid-tier models with balanced reasoning capabilities
Tier 3: Macro/Extended 16,385 – 128,000+ tokens Complex RAG synthesis, full codebases, books Frontier models with very large context windows and caching

By strictly enforcing this segmentation via gateway rules, we avoid simple 150-token prompts being processed by expensive frontier models designed for massive contexts. Conversely, it prevents requests with tens of thousands of tokens from failing on models with a restricted context limit of 8k or 16k tokens.

Token counting methods at the gateway

A critical technical trade-off in payload routing is the measurement method used to determine prompt volume. The gateway must perform the calculation before opening the HTTP connection to the upstream provider. Two dominant approaches exist:

1. Exact tokenization via local BPE parsers: Here, a local tokenizer (such as Tiktoken or HuggingFace Tokenizers) runs directly inside the gateway process. This provides an exact count, but introduces CPU overhead and memory consumption—especially when requests require different tokenizers (for example, OpenAI cl100k vs. Anthropic vs. Google Gemma tokenizers).

2. Heuristic byte and character counting: For many scenarios, a fast approximation based on string length or byte volume is sufficient (for instance, 1 token ≈ 4 characters for Western languages). This costs virtually zero CPU time, but requires a safety margin of at least fifteen to twenty percent to prevent edge cases from exceeding context limits.

Because token definitions and encodings vary significantly across providers, it is advisable to read the article on normalizing token usage across providers to see how different tokenizers compare in a multi-provider setup.

Failure modes and risks in volume-based routing

No architecture is without risks. Dynamic payload routing based on prompt volume introduces specific failure modes in production that must be proactively mitigated.

Failure Mode 1: Edge-case misclassification (Boundary Thrashing). A 2,045-token prompt is classified as Tier 1. If the tokenizer heuristic is off by 10 tokens, or if the model injects additional instructions at runtime via templating, the request can exceed the hard context limit of the Tier 1 model and cause a 400 ContextWindowExceeded error.

Mitigation: Always maintain a safety buffer of 10% below the maximum context limit of each model profile.

Failure Mode 2: Unintended model quality degradation. A user submits a prompt of only 40 tokens, but requests a mathematical proof or complex logic. Because the volume is low, the system routes the request to a small model that fails the reasoning task.

Mitigation: Always combine prompt size with metadata hints (such as intent tags or requested reasoning tiers) that can be provided by the client.

Failure Mode 3: Latency accumulation due to tokenizer overhead. When a gateway receives massive payloads (e.g., 500 KB of JSON) and performs BPE tokenization synchronously on a single-threaded Node.js event loop, it blocks all other incoming requests.

Mitigation: Perform tokenization asynchronously in a worker thread or use optimized native C/Rust bindings.

Implementation: Routing logic in the gateway

Below is an implementation example of payload routing middleware. This code calculates the size of the incoming payload, applies a safety margin, selects the appropriate upstream endpoint, and handles errors gracefully using a fallback mechanism.

/**
 * Dynamische payload router middleware voor LLM-gateways
 */
import { countTokensFast } from './token-utils.js';
import { forwardRequest } from './http-transport.js';

interface ModelRoute {
  provider: string;
  model: string;
  maxTokens: number;
  timeoutMs: number;
}

const ROUTING_TIERS: Record<string, ModelRoute> = {
  tier1_small: {
    provider: 'fast-inference-engine',
    model: 'small-instruct-v2',
    maxTokens: 2048,
    timeoutMs: 5000
  },
  tier2_medium: {
    provider: 'standard-cloud',
    model: 'general-chat-8b',
    maxTokens: 16384,
    timeoutMs: 15000
  },
  tier3_large: {
    provider: 'frontier-cloud',
    model: 'frontier-large-context',
    maxTokens: 128000,
    timeoutMs: 45000
  }
};

export async function routePayloadByVolume(req: any, res: any) {
  const startTime = Date.now();
  const messages = req.body.messages || [];
  
  // 1. Snelle berekening van totale prompt-tekst
  const fullPromptText = messages.map((m: any) => m.content || '').join('\n');
  const estimatedTokens = countTokensFast(fullPromptText);
  
  // 2. Selecteer tier met 10% veiligheidsbuffer
  let selectedTier = 'tier3_large';
  if (estimatedTokens < (ROUTING_TIERS.tier1_small.maxTokens * 0.9)) {
    selectedTier = 'tier1_small';
  } else if (estimatedTokens < (ROUTING_TIERS.tier2_medium.maxTokens * 0.9)) {
    selectedTier = 'tier2_medium';
  }
  
  const targetConfig = ROUTING_TIERS[selectedTier];
  
  try {
    // 3. Dispatch verzoek met strikt timeout-budget
    const upstreamResponse = await forwardRequest({
      target: targetConfig,
      payload: {
        ...req.body,
        model: targetConfig.model
      },
      timeoutMs: targetConfig.timeoutMs
    });
    
    res.setHeader('X-Routed-Tier', selectedTier);
    res.setHeader('X-Estimated-Tokens', estimatedTokens);
    return res.status(200).json(upstreamResponse);
  } catch (err: any) {
    // 4. Fallback-pad: escaleer naar Tier 3 als kleine tier faalt
    if (selectedTier !== 'tier3_large') {
      try {
        const fallbackTarget = ROUTING_TIERS.tier3_large;
        const fallbackResponse = await forwardRequest({
          target: fallbackTarget,
          payload: { ...req.body, model: fallbackTarget.model },
          timeoutMs: fallbackTarget.timeoutMs
        });
        res.setHeader('X-Routed-Tier', 'tier3_large_fallback');
        return res.status(200).json(fallbackResponse);
      } catch (fallbackErr: any) {
        return res.status(502).json({
          error: 'Gateway routing en fallback mislukt',
          details: fallbackErr.message
        });
      }
    }
    
    return res.status(504).json({
      error: 'Upstream model timeout op gekozen tier',
      tier: selectedTier
    });
  }
}

Dynamic compression prior to routing

Instead of immediately forwarding a bulky request to an expensive frontier model, a gateway can opt to compress the payload first. This process is also known as context compaction. During this process, the gateway removes redundant whitespace, prunes non-essential system information, or condenses intermediate chat messages.

When a 3,200-token payload is compressed down to 1,800 tokens, it suddenly falls within the threshold of a Tier 1 model. This delivers an immediate reduction in operational costs and prevents unnecessary routing to slower backend infrastructures. For practical payload optimization techniques, the article on prompt compression in your API pipeline offers concrete algorithms for noise removal.

Additionally, various community insights on context reduction are available; see, for example, the techniques outlined in the guide on token savings and proven tricks for methods to keep prompts concise without semantic loss.

Queue management and throughput: separating heavy and light workloads

An often overlooked side effect of variable payload size is Head-of-Line Blocking (HoL blocking). When heavy payloads of 80,000 tokens share the same workers and connections as light, interactive 100-token chat messages, network buffers become congested. The light requests are forced to wait until the heavy requests have been completely streamed and processed.

The solution lies in separating request streams based on calculated payload size. By connecting the gateway to dedicated queues, light workloads are prioritized across low-latency connections, while heavy, batch-like payloads are processed via dedicated workers with higher timeout thresholds. More details on this architecture can be found in the guide on priority queues for critical LLM tasks, which describes how capacity is distributed fairly.

Cost, latency, and production trade-offs

Implementing dynamic payload routing introduces distinct operational trade-offs. It is essential to weigh the cost savings against the added complexity of the gateway.

Aspect Without Payload Routing (Static) With Dynamic Payload Routing
Average cost per call High (all traffic configured for the largest expected model) Low (60-80% of volume runs on cheaper tiers)
Time-to-First-Token (TTFT) Slower for small prompts routed to large models Optimal (small prompts routed directly to fast models)
Gateway Latency Overhead 0 ms (direct passthrough proxy) 2 – 15 ms (time for tokenization and policy evaluation)
System Infrastructure Simple, static configuration More complex, requires active health checks and tier fallbacks
Error Susceptibility Fixed error profiles Potential for boundary issues and misrouting in edge cases

The operational gain is greatest for applications with a highly heterogeneous user base, where simple assistant queries alternate with in-depth document analyses. For uniform workloads (where every call has roughly the same size), the added complexity of a routing layer often does not outweigh the benefits.

Production Checklist for Dynamic Routing

For a stable rollout of dynamic payload routing in a production environment, the following technical measures must be implemented: