# Where should an LLM gateway sit: SDK, sidecar, service or edge?

URL: https://infro.io/blog/where-should-an-llm-gateway-sit-sdk-sidecar-service-or-edge
Author: INFRO Editorial (https://infro.io/blog/editorial)
Publisher: INFRO
Published: 2026-12-01
Last modified: 2026-12-01
Last reviewed: 2026-12-01 (next review due 2026-12-31)
Topic: Architecture and migration
Format: Comparison

> Put the gateway where it can both hold the provider keys and be the only route out. For most teams that is a shared service, with callers changing only a base URL and key. Use a sidecar when you need a local hop, an SDK when you accept advisory control, and the edge only in front of untrusted clients.

Key takeaways:
- Placement decides four things features cannot fix: which process holds the provider key, how far the failure spreads, whether budgets are global, and whether callers can go around you.
- An SDK-embedded gateway is advisory: it governs only the code that imports it, and every service still holds a provider key.
- A shared service gives the strongest key custody and exact global budgets, at the price of one network hop and a shared failure domain you must design for.
- A sidecar removes the central hop and keeps keys out of the app, but budgets and policy become per-pod state that needs a shared store.
- Whatever you pick, enforcement is only real when egress rules make the gateway the only path to the provider.

## Placement is a separate decision from features

Two gateways with identical features behave very differently depending on where they run. The placement decides which process holds the provider key, how far one failure spreads, whether a budget is one number or many, and whether a caller can simply skip the gateway. If you are still deciding whether you need one, start with [what an LLM gateway is](https://infro.io/blog/what-is-an-llm-gateway) and [build versus buy](https://infro.io/blog/build-vs-buy-llm-gateway). This post assumes you have decided, you call providers on your own keys, and you now have to choose where it sits.

There are four placements:

- **SDK**: the gateway logic is a library inside each application process.
- **Sidecar**: a proxy runs next to each application instance (same pod or host) and the app calls it over localhost or a socket.
- **Service**: one shared deployment that every application calls over the network.
- **Edge**: a proxy at the network perimeter, close to end users or in front of the rest of your estate.

Microsoft's architecture guidance frames the underlying choice the same way: without a gateway, retry, circuit-breaking and backoff logic is each client's job, and you may not be able to change client code or may be limited to a specific SDK.[1] Where the gateway goes decides how much of that you can take back.

## The four placements side by side

|  | SDK | Sidecar | Service | Edge |
| --- | --- | --- | --- | --- |
| Added latency | None on the wire; some CPU in-process | A local hop, no network crossing | One network hop to a shared tier | One hop, but nearest the user; extra if origin also calls providers |
| Failure domain | The app process: a library bug is an app bug | One pod: failures are isolated per instance | All callers share it | Everything behind that edge |
| Key custody | Every service holds a provider key | Sidecar holds or fetches keys; the app need not | Central: one place holds provider keys | Edge secret store; limited by the runtime |
| Policy enforcement | Advisory: only code that imports it | Per-pod config; global limits need shared state | Central; budgets and allowlists are exact | Strong for request shape; weak for global spend state |
| Bypass risk | Highest: any other code path or language skips it | Medium: the app can still reach the provider unless egress is blocked | Lowest once egress is closed | Only covers traffic that passes the edge |

Table: How the placements differ on the five criteria that matter most. Qualitative; no vendor figures.

The sections below explain each cell and where it breaks.

## SDK: a library in every process

The SDK placement is the lowest-friction one. You wrap the provider client with retry, fallback and logging code, or adopt a library that does it. There is no new deployable and no extra hop.

What you give up is custody and enforcement. Each service must be able to call the provider directly, so each holds a provider key, in its own environment variables or secret mount. Rotating a key means redeploying every consumer. A leaked service credential is a leaked provider credential. Policy is advisory: a budget check written into the library only governs code that calls it. A second language, a script, a notebook or a copy-pasted `curl` goes around it.

The failure domain is the app itself. That is a benefit (no shared dependency) and a cost: a retry bug or a memory leak in the library ships inside every service, and fixing it means a fleet-wide upgrade. Version drift is the usual result, so two services behave differently under the same provider error.

Choose it when you have one or two services in one language, no shared budget to enforce, and the main goal is retry and fallback logic you would otherwise write anyway. Treat it as a stepping stone, not an enforcement layer.

## Sidecar: a local proxy per instance

A sidecar keeps the gateway out of the app's code but inside its blast radius. The app speaks an OpenAI-shaped API to `localhost`; the sidecar adds retries, fallback, timeouts and telemetry, then calls the provider.

Latency is the best of the networked options, because the first hop never leaves the host. Failure isolation is good: a crashed sidecar takes down one instance, and your orchestrator already knows how to restart and drain it. Key custody improves if the sidecar reads credentials from a secret store and the app never sees them, though this depends on the pod's secret mounts being scoped so the app container cannot read the same material.

The weak point is state. A budget of "$X per project per day" or a limit of N concurrent requests per key is a global quantity, and each sidecar sees only its own traffic. With 40 pods you either divide the limit by 40 and waste headroom, or add a shared store (Redis or a database) that every sidecar consults, which reintroduces a central dependency on the request path. Policy rollout is the second cost: changing an allowlist means pushing configuration to every pod and trusting that it all landed.

Sidecars suit platform teams that already run a service mesh, need the lowest practical overhead, and mostly want per-instance resilience and telemetry rather than hard shared limits.

## Service: one shared gateway

The reference architectures from the cloud vendors are service placements. AWS's multi-provider guidance puts a LiteLLM proxy behind an Application Load Balancer on ECS or EKS, keeps virtual API keys and configuration in RDS, uses ElastiCache for shared settings, and stores external provider credentials in Secrets Manager.[2] Microsoft describes the same shape as a reverse-proxy gateway that decouples the application from the model platform, offering federated authentication, rate limiting, cross-model monitoring and health-based load balancing.[1]

The service gives the strongest answer on three of the five criteria. **Custody**: provider credentials live in one place; callers hold a revocable gateway key, not the provider key. **Policy**: allowlists, guardrails and budgets are evaluated against one view of traffic, so a cap per project or per key is a single counter. **Bypass**: once the only network path to the provider runs through the gateway, there is nothing to skip.

The costs are real. The extra hop is a network round trip, though Microsoft notes that network latency is usually a small part of the overall prompt and completion flow.[1] The failure domain is shared, and Microsoft names this directly: the gateway can become a single point of failure through platform availability, code or configuration deployments, or a misconfigured critical endpoint.[1] You then own its availability, which is why [fail-open versus fail-closed](https://infro.io/blog/gateway-down-should-ai-traffic-fail-open-or-fail-closed) has to be decided before launch, not during the incident.

> **Example: Illustrative latency arithmetic**
> These numbers are made up to show the method, not measured. Suppose a chat call takes 2,400 ms end to end and the gateway hop adds 15 ms: that is 0.6% of the call. If your fastest path is a 120 ms embedding call, the same 15 ms is 12.5%. The hop matters for short calls and for time to first token on streaming, not for long generations. Measure with [the method in this post](https://infro.io/blog/how-much-latency-does-an-ai-gateway-add-a-measurement-method) on your own traffic.

## Edge: a gateway at the perimeter

An edge gateway runs near the user, in a CDN or edge-compute runtime, or as the entry tier in front of a gateway service. AWS's guidance shows the second form: CloudFront and AWS WAF in front of the load balancer that fronts the gateway containers.[2]

Edge is the right place for things that belong to the request's outer shape: TLS, bot and abuse filtering, coarse per-IP or per-client rate limits, and geographic rules. It is also the only option for browser and mobile clients that you cannot trust with a key at all. For those callers the edge holds the secret and exposes a narrow, authenticated route.

It is a poor place for global spend state. Edge runtimes are distributed, short-lived and limited in what they can store consistently, so an exact per-project budget is hard to keep there. It also cannot see server-to-server traffic that never passes the perimeter: a backend job in your VPC calling a provider is invisible to it. And it does not by itself remove the provider key from your estate, because origin services may still hold one.

In practice, edge is a front layer added to a service, not a replacement for one.

## Failure domains and what survives an outage

The failure question has two halves: what the gateway does when a provider fails, and what happens when the gateway itself fails.

For provider failures, behaviour is the same in every placement and bounded by the protocol: you can retry or switch providers only before the response starts. INFRO's documented behaviour is typical: failover and fallbacks operate strictly before the first token, and a connection that fails before then is replaced by the next one so the client sees only a slightly higher time to first token.[3] Once a stream has begun, no placement can silently move it. See [which errors to retry, fail over or fail fast](https://infro.io/blog/which-llm-api-errors-to-retry-fail-over-or-fail-fast).

For gateway failures, the placements differ:

1. **SDK**: no separate failure; the library fails with the app. Nothing to fail open to, because the provider key is already local.
2. **Sidecar**: per-pod failures. Readiness probes and restarts handle most of it. A shared state store, if you added one, becomes the shared dependency.
3. **Service**: needs redundancy across zones, health checks, and a documented stance on whether callers fall back to a direct provider call. That fallback is exactly the bypass path, so allow it only on purpose, for named workloads, and alert on its use.
4. **Edge**: usually distributed by construction, but a bad configuration push can still take every region at once.

## Key custody and bypass are one problem

"Is the key safe?" and "can callers skip the gateway?" are the same question seen from two sides. If a service holds a working provider key, it can call the provider without the gateway, whatever the gateway's policy says.

The custody ladder, weakest to strongest:

1. Every service holds a provider key (SDK placement).
2. Each pod's sidecar reads a provider key, apps do not (sidecar, with scoped mounts).
3. Only the gateway tier holds provider keys; callers hold gateway keys (service).

In INFRO's model, provider credentials are saved encrypted and sealed in INFRO, and applications only ever hold an INFRO key that carries its own label and optional spend limit.[4] That is the third rung: a leaked application key is bounded by that key's limits and can be revoked without touching the provider account.

Custody alone is not enough, because teams keep old keys. Close the path with network policy so workloads can reach only the gateway on the way out:

```yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: llm-egress-gateway-only
spec:
  podSelector:
    matchLabels:
      llm-caller: "true"
  policyTypes: ["Egress"]
  egress:
    - to:
        - namespaceSelector:
            matchLabels:
              name: llm-gateway
    - to:
        - namespaceSelector: {}
      ports:
        - protocol: UDP
          port: 53
```

Then verify rather than assume. [How to block direct provider calls with egress rules](https://infro.io/blog/how-to-block-direct-ai-provider-calls-with-egress-rules) covers the network side. For the provider side, a usage check that compares the provider's own per-key usage with what the gateway served will flag a key that is still being used directly; INFRO offers this for OpenAI and Anthropic with a separate read-only usage credential.

## A rule for choosing

Apply these in order and stop at the first that fires.

1. **Callers you cannot trust with any key** (browsers, mobile apps, third-party embeds): edge or service, with the key held server-side only. Never ship a provider key in a client.
2. **You need a budget, allowlist or per-customer cap that must hold across all instances**: service. Global limits want one counter. A sidecar plus shared store is the same architecture with an extra moving part.
3. **You can close egress and want the lowest local overhead, with mostly per-instance concerns**: sidecar.
4. **One team, one language, no shared limits, and a retry/fallback problem only**: SDK, and plan to move to a service when the second team arrives.
5. **Otherwise**: service. It is the only placement where custody, policy and bypass all point the same way.

The compact version: *put enforcement where the provider key lives, and make that the only path out.* Features, vendors and hosting choices come after that.

Placements combine. A common end state is a service as the enforcement point, a thin client change on callers, and an edge layer for abuse filtering. The client change should stay thin: with INFRO it is a base URL and a key on the OpenAI SDK, or its first-party SDKs for Python, TypeScript and Dart, which are typed clients for the same service rather than an embedded gateway.[5]

## Observability should not depend on placement

Pick one telemetry shape and keep it across placements so you can move later without rewriting dashboards. OpenTelemetry's generative AI semantic conventions are the common vocabulary, and the project notes they have moved to their own repository, so pin the version you emit rather than following a moving page.[6] A service can push spans from the request path to your own collector; INFRO sends OTLP/HTTP traces with request ID, model, route, timing, token counts and estimated cost, with prompt and response captures not included.[7]

## The service placement on your own keys

INFRO is the service placement, run as a hosted control plane over provider accounts you already hold: you connect your own provider accounts, call one API, and your providers invoice you directly while INFRO charges a subscription and takes 0% of model spend.[8] On top of that placement it provides routing and failover across your connections, budgets, key limits and rotation, model allowlists and guardrails, and per-request usage and cost. If your constraints point to a sidecar or an SDK-only approach, the rule above says so; INFRO is not an embedded library.

## Questions

### Is an LLM gateway the same thing as an AI gateway?

The two terms are used interchangeably for a layer between applications and model providers. "AI gateway" is the broader label and is more often used when the layer also handles image, audio, video or tool traffic. The placement question is identical for both.

### Can an ordinary API gateway do this job?

It can terminate TLS, authenticate callers and rate-limit by request count, which covers part of the service placement. It does not know tokens, streaming responses, model fallbacks or per-request provider cost without extra work. See [AI gateway vs API gateway](https://infro.io/blog/ai-gateway-vs-api-gateway).

### Does the placement change who the provider bills?

No. On your own keys the provider invoices your account for inference whichever placement you choose. Placement changes who holds the key, what is enforced and what you can observe, not the billing relationship.

## Sources

1. Access Foundry Models and other language models through a gateway — Microsoft Learn, Azure Architecture Center. https://learn.microsoft.com/en-us/azure/architecture/ai-ml/guide/azure-openai-gateway-guide (accessed 2026-12-01)
2. Guidance for Multi-Provider Generative AI Gateway on AWS — Amazon Web Services. https://aws.amazon.com/solutions/guidance/multi-provider-generative-ai-gateway-on-aws/ (accessed 2026-12-01)
3. Failover & fallbacks — INFRO. https://infro.io/docs/features/fallbacks (accessed 2026-12-01)
4. Authentication — INFRO. https://infro.io/docs/authentication (accessed 2026-12-01)
5. SDKs & frameworks — INFRO. https://infro.io/docs/sdks (accessed 2026-12-01)
6. Generative AI semantic conventions (moved) — OpenTelemetry. https://opentelemetry.io/docs/specs/semconv/gen-ai/ (accessed 2026-12-01)
7. Exports & OpenTelemetry — INFRO. https://infro.io/docs/observability/exports (accessed 2026-12-01)
8. INFRO documentation: overview — INFRO. https://infro.io/docs (accessed 2026-12-01)
