tokenade
Log inenfr
Start free

LLM Gateway vs Token Optimizer: What Each Cuts

An LLM gateway like LiteLLM or Portkey controls where requests go and who pays for them. A token optimizer shrinks what the agent puts in each request. Different layers, different savings.

TL;DR — An LLM gateway (LiteLLM, Portkey) sits at the API layer: routing, budgets, response caching, logs. It controls spend but sends the same request it receives. A token optimizer like Tokenade works on the machine, shrinking command output, file reads and MCP manifests before the request exists. Teams want the gateway for control; anyone paying per token wants the optimizer. They stack.

Profile photo of Paul Irolla

By Paul Irolla

Founder · AI & developer tools · Tokenade

Ph.D. in AI · builds token-optimization tooling for AI agents

View author page
9 min read
Cite this page

Part of the reduce AI coding agent token usage pillar. This page compares two categories, not two products, because people keep buying one expecting the other.

LLM gateway vs token optimizer in one sentence

A gateway decides where a request goes and who pays for it; a token optimizer decides how big the request is. LiteLLM and Portkey sit between your apps and the model providers, at the API layer. Tokenade sits between your coding agent and its tools, on the machine where you work.

So they reduce different things. A gateway caps, routes and attributes spend. An optimizer lowers the number of tokens each turn carries. If your problem is "the bill is too big because every turn is huge", only the second one touches it.

Gateway features below were read on LiteLLM's and Portkey's official docs and on Anthropic's Claude Code gateway docs on 2026-10-15. The only Tokenade figure on this page comes from the published THOL benchmark, which I maintain; the methodology is here.

The short version

CategoryExamplesWhere it sitsWhat it reducesBest for
LLM gatewayLiteLLM, PortkeyBetween client and provider APIOverspend, provider outages, repeated identical callsTeams, apps, governance
Token optimizerTokenadeBetween the agent and its tools, locallyTokens per turn: command output, file reads, MCP manifestsAnyone paying per token for coding agents

What does an LLM gateway actually do?

It gives you one API in front of many providers, plus control over who spends what. LiteLLM describes itself as an open-source gateway that puts 100+ LLMs behind one OpenAI-compatible interface (LiteLLM docs, read 2026-10-15). Portkey's gateway documents a universal API, fallbacks, conditional routing, load balancing and guardrails (Portkey docs, read 2026-10-15).

The feature set is broadly the same across the category:

  • Routing and fallbacks. Send traffic to another provider or model when one fails or rate-limits.
  • Budgets and keys. LiteLLM's virtual keys manage access, budgets and rate limits per team or user, with spend tracked per key, team and user.
  • Response caching. Return a stored answer instead of calling the model again.
  • Logs and observability. Every request recorded in one place.

Anthropic's own Claude Code docs list the same value: credentials kept server-side, usage attributed per developer, budgets and rate limits enforced in one place, audit logging, and provider switching without touching developer machines (Claude Code gateway docs, read 2026-10-15).

Portkey's docs now carry the line "Portkey is now Prisma AIRS AI Gateway", so expect that name in procurement conversations.

What does a token optimizer actually do?

It shrinks what the agent sends, before the request is built. A coding agent spends most of its tokens reading: test output, git log, whole files opened to change four lines, MCP tool manifests loaded on every turn. A token optimizer intercepts those at the tool boundary and hands the agent a smaller version.

Tokenade does this through native hooks in the agents you already use. It filters command output, replaces whole-file reads with structure-first reads and semantic search, and defers MCP manifests until a tool is actually needed. The compaction runs on your machine, not on a server in the middle.

On the published THOL campaign, Tokenade cuts session cost by 39% on long sessions, against running no tool at all. That is a whole-session number measured on the bill, not a per-command compression ratio.

Where does each one sit in the pipe?

The optimizer acts before the request exists; the gateway acts after. Follow one agent turn:

  1. The agent runs pytest and gets 4,000 lines back.
  2. Token optimizer: compacts that output to the failures and the summary.
  3. The agent assembles the request: system prompt, history, tool results.
  4. LLM gateway: checks the key's budget, picks a provider, checks its cache, logs the call, forwards it.
  5. The provider bills the tokens it received.

The gateway sees step 4's request as finished goods. The routing, budget and caching features documented by LiteLLM and Portkey forward the prompt they receive; none of them is described as rewriting the tool results inside it. If those 4,000 lines reach step 4, they get billed, whichever provider you route to.

What does each one actually reduce?

A gateway reduces overspend and waste between calls; an optimizer reduces the size of each call. Side by side:

Cost lineLLM gatewayToken optimizer
Noisy command output in contextNoYes
Whole-file readsNoYes
MCP manifests re-sent every turnNoYes
Identical repeated requestsYes, response cacheNo
Runaway spend from one key or teamYes, budgets and limitsNo
Expensive model used for easy workYes, if you write the routing rulesNo
Provider outageYes, fallbacksNo
Knowing who spent whatYes, per-key trackingLocal savings dashboard only

The rows barely overlap. That is the whole argument of this page.

Why doesn't a gateway cache save much on agent traffic?

Because agent requests are almost never identical. Portkey's simple cache hits only when the full request body matches: messages, model, temperature, every parameter. Its docs say even one changed character is a miss (Portkey cache docs, read 2026-10-15). LiteLLM's cache likewise returns a stored response "when you make the same request twice" (LiteLLM caching, read 2026-10-15).

A coding agent appends to its history on every turn, so turn 12 is never byte-identical to turn 11. Exact-match response caching shines on repeated FAQ-style prompts in a product, not on a growing agent transcript. That is my reasoning from how the cache keys are defined, not a measurement.

Semantic caching (Portkey's cosine-similarity mode, LiteLLM's Redis, Valkey or Qdrant semantic backends) loosens the match. On code, "similar meaning" is risky: two prompts can be close in embedding space and need different answers. I would not put it in front of an agent that edits files.

Do not confuse either with provider-side prompt caching, which discounts a repeated prefix rather than skipping the call. LiteLLM documents that separately. A gateway has to forward the caching headers correctly for it to keep working.

When is an LLM gateway the right pick?

When the problem is control, not volume.

  • Several developers or services share provider keys, and you need per-person budgets and offboarding by revoking one credential.
  • You call several providers and want one API and automatic fallbacks.
  • Compliance wants every model request logged in one place.
  • Your app sends genuinely repeated prompts, where a response cache pays.
  • You need a hard ceiling so an overnight agent cannot run up a bill.

One trade-off to know before you route Claude Code through one: Anthropic's docs say a gateway credential replaces the claude.ai subscription login for those requests, so usage is billed per token to whoever owns the forwarded key. They also warn that a gateway which does not forward new Claude Code capabilities breaks the matching features, so it needs updating as Claude Code evolves.

When is a token optimizer the right pick?

When the problem is that every turn is too big.

  • You pay per token, or you keep hitting plan limits on long sessions.
  • Your sessions are build, test and refactor loops with lots of tool output.
  • You run MCP servers whose manifests ride along on every turn.
  • You are one developer, and running and maintaining a proxy server is more infrastructure than the problem deserves.
  • You want the compaction to happen on your machine, before anything leaves it.

Should you run both?

Yes, if you need both control and smaller requests; they do not conflict. The optimizer works inside the agent's tool calls, the gateway on the outbound API call. The agent sends a smaller request, and the gateway still routes, budgets, caches and logs it as usual.

The combination also makes the gateway's numbers more useful. Per-key spend tracking will show the drop after you install the optimizer, which is an independent reading of the saving you can trust more than any vendor dashboard, mine included. Measuring agent token usage covers how to read it.

What's not different enough to matter?

  • "Both save money." True, and useless for deciding. Ask which cost line you are attacking.
  • Dashboards. Both categories ship one. A gateway's dashboard counts what was spent; Tokenade's counts what was avoided. You want the first for accounting.
  • Open source. LiteLLM and Portkey's gateway are open source; Tokenade is proprietary, with a 7-day free trial. That matters for self-hosting policy, not for which layer you need.
  • Model choice. A gateway can route to cheaper models; you can also just pick a cheaper model in your agent. Routing rules are only as good as the person writing them.

Which one should you choose?

  • Solo developer on Claude Code, Codex or Cursor: token optimizer. A gateway adds infrastructure without shrinking turns.
  • Team sharing provider keys: gateway first for budgets and attribution, optimizer on each laptop for volume.
  • Product calling LLMs from a backend: gateway. Your prompts are yours to trim in code.
  • Agents running unattended overnight: both. Gateway budget as the ceiling, optimizer to reach it later.
  • Not sure where tokens go: measure a week first, then decide. Claude Code pricing explains what the bill is made of.

FAQ

Is LiteLLM a token optimizer?

No. LiteLLM is a gateway: one API for 100+ LLMs, with routing, fallbacks, budgets, spend tracking and response caching. Its documented features forward the prompt it receives. It can lower spend through caching and routing, but it does not shrink the command output or file contents your agent put in the request.

Can a semantic cache replace a token optimizer?

Not for coding agents. A semantic cache skips a call when a new prompt is close enough to a previous one. Agent turns keep growing and often need different answers despite similar wording, so hits are rare and a wrong hit is costly. An optimizer instead makes every call smaller, cached or not.

Does a gateway break Claude Code features?

It can. Anthropic's docs warn that a gateway which does not forward new Claude Code capabilities breaks the matching features, and that a gateway credential replaces subscription billing with per-token billing. A token optimizer works through the agent's own hooks, so it does not sit on the API path at all.

Do I need a gateway if I already use Tokenade?

Only if you need what a gateway is for: shared keys, per-team budgets, provider fallbacks or central audit logs. Tokenade reduces the tokens each turn carries; it does not enforce budgets across a team. Plenty of teams will want both, and they stack cleanly.

Where do my prompts and API keys go?

With a hosted gateway, every request — prompts, code context and the provider key or a gateway credential — passes through the gateway's servers before it reaches the model; self-hosting moves that hop onto infrastructure you run. Tokenade adds no network hop: it compacts on the computer where you work, before the request leaves, and your key stays with your agent as it was.


See also:

FAQ

Cut your AI agent's token bill.

Ranked #1 on the Token-Harness Optimizer Leaderboard. Zero config.

Tokenade cuts the token bill of AI agents: one install, zero config, and it trims what your agent sends to the model.