If your stack talks to multiple LLM vendors, the fastest way to consolidate OpenAI Anthropic Google API keys is to put a unified inference layer in front of them. You stop distributing three separate credentials to every microservice and instead issue one gateway key, then route by model name. This cuts secret sprawl and gives you a single place to enforce rate limits and observe spend.
1. Audit where keys live and how they’re used
Before writing any proxy code, map every place a provider key is referenced. Search your repos for sk-, ANTHROPIC_API_KEY, GOOGLE_API_KEY, and any Vault paths or CI secrets. Note which services call which models and whether they use streaming, function calling, or provider-specific extensions like Anthropic prompt caching.
Most teams find keys duplicated across:
- Backend services with direct SDK calls
- Serverless functions with env vars
- Local developer laptops and
.envfiles - CI pipelines running integration tests
You cannot consolidate OpenAI Anthropic Google API keys safely until you know every caller. Break the audit into a table:
| Service | Provider | Auth method | Models used | Special features |
|---|---|---|---|---|
| Search ranker | OpenAI | env var | gpt-4o-mini | streaming |
| Doc summarizer | Anthropic | Vault | claude-3-5-sonnet | prompt cache |
| Entity extract | file | gemini-1.5-pro | json mode |
2. Pick a consolidation strategy
You have two realistic paths: run your own thin proxy, or point everything at a managed OpenAI-compatible gateway.
Option A: Self-hosted reverse proxy
If you must keep all traffic in-house, write a small FastAPI or Envoy layer that accepts OpenAI-format requests and rewrites them for each backend. You keep the three provider keys on the proxy only.
# minimal FastAPI sketch
from fastapi import FastAPI, Request, Response
import httpx
app = FastAPI()
PROVIDER_KEYS = {
"openai": "sk-...",
"anthropic": "sk-ant-...",
"google": "ai-...",
}
@app.post("/v1/chat/completions")
async def proxy(req: Request):
body = await req.json()
model = body["model"]
if model.startswith("gpt"):
# forward to OpenAI
...
This works but you inherit the job of translating request shapes, handling streaming, and implementing fallback.
Option B: Managed OpenAI-compatible gateway
A gateway such as n4n.ai provides one OpenAI-compatible endpoint that addresses 240+ models, with automatic fallback when a provider is rate-limited and per-token usage metering. If you’d rather not operate your own translation layer, point the OpenAI SDK at its base URL and change only the model strings. Either route gets you to the same end: one credential, many backends.
3. Standardize on one request shape
The OpenAI chat completions schema is the de facto lingua franca. Even if you self-host, make your internal contract match it. A typical call looks like:
from openai import OpenAI
client = OpenAI(
base_url="https://your-gateway.example/v1", # or https://api.n4n.ai/v1
api_key="single-gateway-key",
)
resp = client.chat.completions.create(
model="anthropic/claude-3-5-sonnet",
messages=[
{"role": "system", "content": "You are terse."},
{"role": "user", "content": "Explain rate limits."},
],
temperature=0.2,
)
Native Anthropic and Google APIs diverge: Anthropic expects system as a top-level field and uses role: "assistant" differently; Google uses contents with parts. A good consolidation layer hides that. If you roll your own, write adapters that convert the OpenAI message list into each provider’s format. Do not let callers see those differences.
4. Route by model name, not by provider
Once the request shape is uniform, routing becomes a string prefix or a config map. Adopt a naming convention like provider/model-slug.
ROUTES = {
"openai/gpt-4o": "https://api.openai.com/v1",
"anthropic/claude-3-5-sonnet": "https://api.anthropic.com/v1",
"google/gemini-1.5-pro": "https://generativelanguage.googleapis.com/v1beta",
}
Callers then ask for google/gemini-1.5-pro without knowing the underlying key. This is the core mechanic to consolidate OpenAI Anthropic Google API keys: the key is resolved server-side, the caller only names a capability.
5. Handle auth and secret rotation centrally
Store the three provider keys in a secrets manager (Vault, AWS Secrets Manager, Doppler). The proxy or gateway reads them at startup and refreshes on a schedule. Rotate by updating the secret in one place; no service restart required if your proxy watches for changes.
Never return provider keys to clients. Issue a scoped gateway key per team:
# example: create a project key at the gateway
curl -X POST https://your-gateway.example/admin/keys \
-H "Authorization: Bearer $ADMIN_KEY" \
-d '{"project":"search","rate_limit":1000}'
If a team leaves, revoke one key. That is impossible when keys are scattered across ten repos.
6. Implement fallback and degradation
Providers throttle. When OpenAI returns 429, you want to shift to Anthropic or Google for the same task. With a managed gateway, automatic fallback can be configured per route. With self-hosted code, wrap the call:
def complete_with_fallback(messages, primary, backup):
try:
return client.chat.completions.create(model=primary, messages=messages)
except RateLimitError:
return client.chat.completions.create(model=backup, messages=messages)
Tradeoff: fallback models have different strengths. Don’t silently swap gpt-4o for gemini-1.5-flash in a latency-critical path without testing output quality. Log which model actually served the request.
7. Meter usage per token and per provider
Consolidation breaks the illusion that “the bill comes from one vendor.” You still pay three vendors. Emit a usage record on every response:
{
"project": "search",
"model": "anthropic/claude-3-5-sonnet",
"prompt_tokens": 120,
"completion_tokens": 45,
"provider": "anthropic",
"gateway_key": "pk_search_123"
}
Aggregate these into your existing cost dashboard. Per-token metering lets you attribute spend to the team that triggered it, which is the only way to control multi-provider LLM cost.
8. Test against provider quirks
Unification hides differences but does not erase them. Common pitfalls:
- Streaming: Anthropic and Google stream SSE differently. Verify your proxy forwards
text_deltacorrectly. - Function calling: OpenAI tool calls and Anthropic tool use are not byte-compatible. If you rely on it, test schema translation.
- Cache control: Anthropic uses
cache_controlbreakpoints; Google uses context caching. A gateway that honors client routing directives and forwards provider cache-control hints preserves those optimizations. If you self-host, you must pass those fields through explicitly.
Write integration tests that hit each backend through your consolidated endpoint with a canary key.
def test_anthropic_cache_passthrough():
resp = client.chat.completions.create(
model="anthropic/claude-3-5-sonnet",
messages=[{"role":"user","content":"Long doc..."}],
extra_body={"cache_control": {"type": "ephemeral"}},
)
assert resp.usage.cache_creation_tokens > 0
9. Roll out gradually
Don’t flip every service on day one. Start with one low-risk caller, like a background summarizer. Point it at the gateway, confirm metrics match direct calls, then migrate the next. Keep the old keys in the proxy only; remove them from services as they cut over.
A phased rollout limits blast radius if your translation layer mangles a request shape.
Common pitfalls and tradeoffs
Latency hop. Adding a proxy adds a network round trip. Put the gateway in the same region as your compute. Managed gateways usually have edge endpoints; self-hosted ones need careful deployment.
Single point of failure. If the proxy goes down, all LLM access dies. Run two instances and health-check the providers independently. Managed services handle this for you but introduce dependency on their uptime.
Model name drift. Providers rename models. Centralize a model alias table so latest-cheap maps to gpt-4o-mini today and something else next quarter.
Security scope. A gateway key with no per-project limit is dangerous. Always scope and rate-limit.
Cost visibility lag. Provider dashboards update hourly or daily. Your per-token metering should be near-real-time so you catch a runaway loop before the invoice does.
Consolidating OpenAI Anthropic Google API keys is not just about fewer secrets. It is about owning the routing, fallback, and metering logic in one place so your application code stays provider-agnostic and your platform team can enforce policy. Do the audit, pick a layer, standardize the request, and migrate service by service. The payoff is a stack that swaps models without code changes and bills accurately by team.