Zero Labs
Industry News / AI

Your App Is Calling LLMs Directly. That’s Going to Hurt.

Source · Towards AI By Amish Kushwaha

From the publisher’s feed, collected in the ZeroLabs newsroom.

A practical guide to LLM Gateway: the control plane your AI stack is missingI want to tell you about a scenario I’ve seen play out more than once.A backend team integrates an LLM. It goes well. Then a second team does the same. Then a third. Each team has their own API keys, their own retry logic, their own logging or rather, no logging at all. Then one day, your OpenAI bill doubles with no obvious explanation. A script went rogue at 2am. A prompt in production is silently leaking user data to the model. And when GPT-4 gets deprecated, you’ve got four services to update.This is the operational tax of calling LLM APIs directly at scale. It doesn’t bite you immediately, it bites you at exactly the wrong time.The fix is an LLM Gateway. And once you understand what it is, you’ll wonder how you shipped AI features without one.What is an LLM Gateway?An LLM Gateway is a middleware layer, a reverse proxy that sits between your application(s) and one or more LLM provider APIs.Your services talk to one endpoint. The gateway handles everything that happens next:» Routing to the right provider » Enforcing rate limits » Logging tokens and cost » Caching responses » Handling streaming responses » Failing over gracefully when a provider hiccupsThink of it like an API gateway but purpose-built for the unique properties of LLM traffic.The Problems It Solves1. Provider Lock-In and Fragile IntegrationsDirect integrations mean your code is coupled to a specific provider’s SDK and API shape. When you want to try a different model or when a model gets deprecated — it’s a code change across every service that touches LLMs.A gateway gives you one consistent interface. Internally, it translates your request to whatever provider you’re targeting. Switching from GPT-5.4 to Claude Sonnet 4.6? Change a config line, not your application code.Most gateways expose an OpenAI-compatible interface, which has become industry’s standard for LLM APIs.2. Zero ObservabilityRaw LLM API calls are black boxes from an ops perspective. You don’t know:» How many tokens each feature is consuming » Which team is driving your costs » Which requests are slow or failing » What prompts are actually hitting the modelA gateway gives you per-request telemetry: token counts, latency breakdowns (including time-to-first-token), cost attribution by team or endpoint, and request/response logging with optional redaction.This alone is worth the setup time. You can’t optimize what you can’t see.3. No Rate Limiting or Cost ControlsLLM providers enforce rate limits at the org level. If one team’s runaway job starts hammering the API, it affects every team. And if you have no budget guardrails, you’ll find out you needed them when you open next month’s bill.A gateway lets you enforce quotas per team, per service, or per user before requests even leave your infrastructure. You can set hard token budgets and get alerts or automatic blocks when thresholds are crossed.4. Reliability and FallbacksLLM APIs go down. They return 429s. They have degraded performance during peak hours. If your app makes a direct API call and OpenAI is having an incident, your feature is broken.A gateway lets you define fallback chains: try Provider A, and if that fails or is too slow, automatically retry with Provider B. This is remarkably easy to configure in most gateways and dramatically improves reliability.5. Security and ComplianceDirect integrations often mean API keys scattered across environment configs. They also mean prompts containing PII (Personally Identifiable Information) flying off to external APIs without any guardrails.A gateway centralizes secrets management. Your services authenticate to the gateway, and the gateway holds provider credentials. It can also redact PII from prompts before they leave your network, and maintain tamper-proof audit logs of every prompt and completion.How It Works Under the HoodAt the mechanical level, a gateway is an HTTP proxy. But several things make LLM proxying genuinely tricky to implement well.Request flow in a LLM GatewayThe Streaming ProblemThis is where most naive gateway implementations fall down. LLM responses stream token-by-token using Server-Sent Events (SSE). You can’t just buffer the entire response, log it, and forward it as that kills the streaming UX.A proper gateway must proxy the stream in real-time while intercepting it to count tokens and record metadata. It reads each chunk, forwards it immediately to the client, and assembles a log record on the side. Getting this right requires careful handling of backpressure and connection lifecycle.Semantic CachingExact-match caching (same prompt → same cached response) is straightforward. Semantic caching is where things get interesting.The idea: embed the incoming prompt as a vector, do a similarity search against cached embeddings, and if the nearest match is above a threshold then serve the cached response instead of making a provider call.The tricky part is picking the right threshold. If it is too low and you serve stale/wrong answers; too high and your cache hit rate is negligible. Most production deployments tune this per use-case.Real Tools Worth KnowingYou don’t have to build this yourself (though it’s a fascinating project if you want to). Several solid options exist:LiteLLM → The most widely adopted open-source option. Supports 100+ providers under a unified OpenAI-compatible interface. Self-hostable. Has a proxy server mode that works as a drop-in gateway. If you want full control without vendor lock-in on the gateway layer itself, start here.OpenRouter → More consumer/developer-facing, but useful for routing across dozens of models without separate integrations.Portkey → Managed SaaS with excellent observability tooling, routing, and a clean dashboard. Good choice if you want the operational value without managing infrastructure.Helicone → Strongly observability-focused. A good pick if your primary pain point is visibility into costs and usage patterns.When Do You Actually Need One?You don’t need a gateway on day one. A single service calling a single provider is fine to start.You probably need one when any of these are true:» Multiple services or teams are making independent LLM calls » Reliability matters and you need fallbacks when a provider is down » LLM spend is non-trivial and you have no visibility into where it’s going » Compliance requires audit logs of what data touches external AI APIs » You want to experiment with multiple providers without rewriting codeIf you’re at the stage where you’re asking “which model should we use for this feature?” then you’re probably at the stage where you want a gateway.Putting It All TogetherThink of an LLM Gateway the same way you think about any other infrastructure primitive: the same way you wouldn’t expose your database directly to the internet, you probably shouldn’t expose raw LLM provider APIs directly to your application layer at scale.The gateway is the control plane for your AI infrastructure, the place where you enforce policy, gain visibility, handle failure, and maintain flexibility. It decouples what your app wants to do (call an LLM) from how, where, and at what cost that happens.As LLMs become deeper infrastructure (not just features), the gateway layer becomes less optional and more load-bearing.Start simple. But know it’s coming.Your App Is Calling LLMs Directly. That’s Going to Hurt. was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.