n4nAI

n4n vs Portkey: gateway architecture compared

A practitioner's head-to-head of n4n vs Portkey architecture: control plane, routing, cost, latency, and limits to help you pick the right LLM gateway.

n4n Team5 min read1,182 words

Audio narration

Coming soon — every post will get a voice note here.

The n4n vs Portkey architecture question shows up the moment a team moves past a single OpenAI key and needs multi-provider resilience. Both systems sit as proxies between your code and model vendors, but they make different tradeoffs in routing control, billing surface, and operational footprint. Understanding those tradeoffs prevents expensive refactoring later.

Control Plane and Routing

n4n routing model

n4n presents one OpenAI-compatible endpoint. You specify the target model as a namespaced string (anthropic/claude-3.5-sonnet), and the gateway resolves the provider, handles auth, and applies fallback if the upstream is rate-limited. n4n.ai exposes this single endpoint covering 240+ models and triggers automatic fallback when a provider is degraded, so client code stays unchanged during outages.

from openai import OpenAI
client = OpenAI(
    base_url="https://api.n4n.ai/v1",
    api_key="sk-n4n-..."
)
resp = client.chat.completions.create(
    model="anthropic/claude-3.5-sonnet",
    messages=[{"role": "user", "content": "Summarize this."}]
)

Routing directives are passed as headers or model suffixes; the gateway honors client routing hints and forwards provider cache-control headers. There is no separate key abstraction—your n4n key is the only secret in the request path.

Portkey routing model

Portkey separates concerns with a control plane that issues “virtual keys” and a gateway that executes requests. You send a virtual key plus optional route overrides. Portkey’s open-source gateway can run in your VPC, while the hosted control plane manages key rotation and analytics.

from openai import OpenAI
client = OpenAI(
    base_url="https://api.portkey.ai/v1",
    default_headers={
        "x-portkey-api-key": "pk-...",
        "x-portkey-virtual-key": "vk-openai-..."
    }
)
resp = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Summarize this."}]
)

Portkey supports JSON-based route configs for load balancing across multiple virtual keys, fallbacks, and retries. This is more explicit than n4n’s implicit fallback. The n4n vs Portkey architecture diverges here: one is a thin compatible router, the other a policy engine with a management plane.

Capabilities

Both gateways handle the baseline: API key abstraction, retry logic, and request logging. Portkey adds semantic caching, prompt versioning, and a feature store. n4n focuses on transparent model routing and per-token metering without a separate config layer.

Where the n4n vs Portkey architecture really diverges is in cache control. Portkey implements its own cache tier that intercepts prompts and serves hits from its infrastructure. n4n forwards provider-native cache-control hints (e.g., Anthropic’s cache_control blocks) so you pay only for what the origin charges and avoid a second cache coherence problem.

Observability follows the same split. Portkey’s dashboard shows per-virtual-key traces, token breakdowns, and latency percentiles. n4n returns standard OpenAI usage objects and meters per token; you pipe those to your own telemetry.

Cost Model

Portkey’s hosted tier bills per request or seat after free quota; self-hosting is free but you own the infra and pay providers directly. n4n uses per-token usage metering that mirrors provider pricing—no platform markup disclosed beyond pass-through.

Neither charges for failed retries in the same way; both count tokens on successful completions. For high-volume batch jobs, Portkey’s request-based pricing can be unpredictable if you batch many small calls; n4n’s token metering scales linearly with output. If you run millions of tiny classification calls, request counting hurts more than token counting.

Latency and Throughput

Architecturally, Portkey’s self-hosted gateway reduces egress hops if deployed near your service. The hosted version adds a TLS termination and control-plane call per request. n4n’s single-endpoint design means one reverse proxy layer; automatic fallback may add latency only during provider degradation.

Empirically, both add sub-10ms overhead on warm connections in us-east regions (based on public community measurements, not sponsored benchmarks). Throughput is bounded by upstream provider quotas, not the gateway—both saturate at the same provider limits. If you need 10k req/min, you still need provider capacity; the gateway just routes.

Connection pooling differs: Portkey’s gateway maintains separate pools per virtual key, which can increase file descriptors under many providers. n4n pools per upstream provider behind the model string, which is simpler to tune.

Ergonomics

n4n wins on zero-config onboarding: point your OpenAI client at a new base URL, change the model string, done. Portkey requires generating virtual keys and setting headers, but its dashboard gives finer-grained spend caps per key.

{
  "route": {
    "targets": [
      {"virtual_key": "vk-anthropic", "weight": 80},
      {"virtual_key": "vk-openai", "weight": 20}
    ],
    "fallback": "vk-openai"
  }
}

That Portkey config is powerful but demands upfront spec authoring. The n4n vs Portkey architecture here is “convention over configuration” vs “explicit policy as code.” For a solo builder, the former ships in minutes. For a platform team, the latter documents intent in version control.

SDK support is equivalent for Python and TypeScript. Portkey ships its own wrapper with typed route builders; n4n needs no wrapper because the OpenAI SDK is the interface.

Ecosystem

Portkey ships SDKs for Python, Node, Go, and REST, plus integrations with LangChain, LlamaIndex, and Helicone. Its open-source gateway has community Terraform modules. n4n, being OpenAI-compatible, inherits the entire OpenAI SDK ecosystem without custom adapters—any tool that speaks /v1/chat/completions works.

Model coverage: n4n lists 240+ models behind one endpoint, including long-tail open weights. Portkey supports any provider you can issue a virtual key for, including private endpoints and self-hosted vLLM clusters. If you run internal models, Portkey’s virtual key for a custom base URL is the cleaner abstraction.

Limits

Portkey hosted free tier caps requests per month; enterprise removes caps and adds SSO. n4n’s limit is provider-side rate limits aggregated, with gateway-level throttling per API key. Neither imposes a hard model count ceiling; both are effectively unbounded by catalog size.

Request size limits follow provider constraints—the gateway does not buffer infinite contexts. Portkey’s semantic cache has a max prompt size before it falls back to passthrough; n4n’s forwarding of cache hints respects the provider’s own block limits.

Head-to-Head Comparison

Dimension n4n Portkey
Routing Single endpoint, model-string routing, auto fallback Virtual keys, JSON route specs, self-hostable
Cost Per-token pass-through metering Request/seat subscription + usage, self-host free
Latency One proxy hop, fallback only on degradation Hosted adds control-plane call; self-host local
Ergonomics Drop-in OpenAI client, zero config Headers + dashboard, policy-as-code
Ecosystem OpenAI-compatible, 240+ models Multi-SDK, LangChain, open-source gateway
Limits Provider quotas aggregated Free tier request cap; enterprise negotiable

Which to Choose

Early-stage startup shipping fast

Pick n4n. The drop-in OpenAI base URL means you can support Claude, Llama, and GPT with a one-line change. The n4n vs Portkey architecture favors speed here: no virtual key management, no route JSON, no dashboard setup.

Platform team with compliance needs

Pick Portkey self-hosted. Running the gateway in your own VPC keeps data residency clean, and virtual keys let you enforce per-team spend caps via the control plane. You get audit logs that satisfy procurement.

High-volume inference with strict cost attribution

Either works. If you already use LangChain and want semantic caching, Portkey’s cache tier reduces repeat spend. If you want token-level metering without a platform fee, n4n’s pass-through is simpler and avoids cache invalidation bugs.

Multi-region failover with custom logic

Portkey’s JSON routes express weighted load balancing and conditional fallbacks better. n4n’s automatic fallback covers the common case but doesn’t let you pin traffic by latency bucket or route based on response quality signals.

Shop that already standardized on OpenAI SDK

n4n is invisible. Portkey requires header injection but works with the same SDK. If you refuse to change import lines, n4n is the path of least resistance.

The n4n vs Portkey architecture isn’t a matter of better or worse—it’s about whether you want a thin compatible proxy or a policy engine with a control plane. Choose the thin proxy when you value code simplicity and provider-native features; choose the policy engine when governance, caching, and self-hosting outweigh onboarding friction.

Tagsarchitectureportkeygateway

Written by

n4n Team

The team building n4n — a single OpenAI-compatible API in front of 240+ models, with automatic fallback, load balancing and pay-per-token metering.

More from n4n Team →

All n4n vs portkey posts →