My model gateway spent 7,008 tokens to say hi
I put every agent's model calls and tool servers behind one self-hosted gateway. Two weeks later a one-word prompt cost 7,008 input tokens, the fix lived in a database instead of the config file, and the public hostname was adding two seconds to every first token.

By October I had too many places where model keys and endpoints lived. My terminal agent had its own provider config, other agents had theirs, and every time I switched models I edited three files on two machines. So on October 9 I moved everything behind Bifrost, an open source LLM gateway, running in a container on the home cluster. Agents talk to one OpenAI-compatible endpoint with a virtual key. The gateway holds the real provider keys, picks the provider, and logs every call.
The same commit moved my MCP servers behind it too. Bifrost can act as an MCP client, connect to tool servers, and offer their tools to whatever model is being called. On October 19 the chat bridge's MCP server went behind the gateway as well. One place for models, one place for tools. It seemed tidy.
A one-word prompt
On October 23 I sent the gateway a prompt of hi, and the call used 7,008 input tokens.
Seven thousand tokens is a lot for two letters. The cause was the tidy part. The MCP clients were configured with allow_on_all_virtual_keys, so every chat completion through any key got every tool schema from every connected MCP server injected into it. My terminal agent already sends its own tools. On top of that, the gateway added all of its tools to every single call, whether the caller wanted them or not. Without the MCP clients, the same hi cost 31 tokens.
With that setting, every call through the gateway started 7,000 tokens deep, the cheap background jobs included, before the caller had said anything.
The config file that wasn't the config
The fix looked simple: remove the MCP clients from Bifrost's config.json and restart. I did that, and nothing changed.
Bifrost keeps its live configuration in its SQLite database, config.db, and the MCP settings there are what counts. The MCP client list is in a table there, along with a second table that maps virtual keys to MCP configs. Editing the JSON and restarting left both tables exactly as they were. The clients had to come out of the database.
After that the tool servers moved back to being configured per client. My terminal agent and Claude Code each connect to the memory server and the chat bridge directly, and the gateway only does models. The rule went into the agent notes in bold: the gateway must not hold MCP clients.
Two seconds from somewhere else
While I was in there, I measured latency.
Agents reached the gateway through its public hostname. That hostname went out to a Cloudflare edge, in Marseille or Singapore judging by the response headers, came back in through Cloudflare to my house, and only then reached the container. Every request left the country and came back before a model saw it.
| Route | Time to first token | Throughput |
|---|---|---|
| Public hostname, through Cloudflare | +1.5 to 2.5 s | 39 to 188 tok/s, with timeouts |
| LAN address, direct | /health in 8 ms |
173 to 214 tok/s |
| Through the local nginx, no tunnel | 0.55 to 0.69 s | 187 to 236 tok/s |
| OpenRouter directly, no gateway | 0.6 to 1.2 s | 141 to 245 tok/s |
Once the Cloudflare hop was gone, the gateway was no slower than calling OpenRouter directly. The hostname was moved off Cloudflare's proxy to plain DNS pointing at my own nginx with a Let's Encrypt certificate and proxy_buffering off, so streaming works. Machines on the LAN got a DNS rewrite so the same hostname resolves locally, and my terminal agent talked to the LAN address directly. That afternoon also got the nginx box some hardening it should have had already: a rate limit, a method allowlist, scanner paths dropped without a response, and fail2ban.
That setup lasted until November 11, when the gateway moved to a VPS and the LAN address went away. Everything uses the public hostname again now, and the agent notes say not to bring the LAN routing back.
What stuck
Two rules from that day are still in force. The gateway handles model calls and nothing else, because a feature that adds tools to "all keys" adds them to the calls that didn't ask for any. And when a change to a config file does nothing, check whether the program still reads that file after its first start. Bifrost didn't, and I spent a restart finding that out.