AIToday

LLM API gateway masks real keys, adds spending controls

Hacker News1d ago
LLM API gateway masks real keys, adds spending controls

Key takeaway

A new API gateway masks real credentials for DeepSeek, OpenAI, and Anthropic behind opaque tokens, each scoped to a single provider with an expiration time, IP allowlist, and USD spending budget. The gateway verifies each token before substituting in the real upstream secret and forwarding the request, reducing credential exposure and enabling fine-grained spend control.

Summaries like this, in your inbox every morning.

Sign up free →

3 Key Points

  • What happened

    A developer has built an API gateway that sits between clients and LLM providers (DeepSeek, OpenAI, Anthropic), using opaque tokens instead of exposing real API keys. The gateway validates each token against an expiration time, IP allowlist, and a USD spending budget before forwarding requests upstream.

  • Why it matters

    Teams can now issue scoped, budget-limited tokens to developers or tools without sharing real API credentials. This reduces the risk of key leaks and lets organizations enforce spend limits per client or project without modifying upstream provider integrations.

  • What to watch

    The implementation has two known gaps. Budget enforcement is not strict—a single large or concurrent request can exceed the token's budget limit. Cost tracking is also not guaranteed reliable if the client, gateway, or upstream connection fails abruptly; the gateway will still serve the request but may not bill it correctly.

In Depth

The gateway is a Cloudflare Worker that intercepts API requests from clients and routes them to DeepSeek, OpenAI, or Anthropic on behalf of the user, keeping the real API credentials hidden from the client side.

Clients authenticate using opaque tokens created by an administrator and stored in a D1 database. Each token is scoped to exactly one provider and enforces three constraints: an expiration time (stored as milliseconds since Unix epoch), an IP allowlist (checked against Cloudflare's CF-Connecting-IP header), and a provisioned budget in USD. On each incoming request, the gateway looks up the token, verifies it has not expired, originates from an allowed IP, and has not yet spent its full budget. If all checks pass, the gateway substitutes the real upstream API key (stored in the Worker's secrets) and forwards the request. If any check fails, the request is rejected.

The implementation supports five API endpoints across the three providers, verified by @er91 on 2026-07-14: DeepSeek's /deepseek/chat/completions and /deepseek/v1/messages endpoints; OpenAI's /openai/v1/responses endpoint; and Anthropic's /anthropic/v1/messages endpoint. The gateway works with agentic coding tools including OpenCode, Claude Code CLI, and Codex CLI.

Deployment requires creating a new Cloudflare Worker, setting up a D1 database using the provided schema, and installing the Rust toolchain (to build the wasm32-unknown-unknown target). Administrators configure upstream API keys via the Worker's secrets, then issue test tokens by inserting rows into the tokens table. For example, a token might allow spend up to $1.00 USD, with expiration at a specified Unix timestamp, and restricted to a single client IP or opened to all IPs using ["*"].

The gateway has two documented limitations. First, budget enforcement is not strict: the gateway only checks if a token has exceeded its budget at the beginning of a request, so a single large request or multiple concurrent requests can go over budget before being rejected. Second, cost tracking is not fully reliable: if the client or upstream connection fails abruptly, or if the gateway crashes while calculating or persisting the cost, the request is still served but may not be billed. The author notes these gaps and suggests enhancements (such as setting max_tokens dynamically based on available budget, or implementing monitoring to detect billing failures) but has chosen not to implement them in the initial version to keep the gateway simple and to avoid modifying requests.

Context & Analysis

The gateway addresses a practical security concern: teams distributing API access to multiple developers or tools face a dilemma between convenience and credential safety. Sharing real API keys widely increases the risk of leaks and makes it difficult to audit or limit spending per user or application. By introducing a token-based layer, the gateway decouples the problem of credential distribution from the problem of spending oversight.

The design scope is narrow and intentional. The gateway supports only static credentials (API keys and long-lived access tokens), not refresh-token flows, because implementing token refresh within the gateway would require either access to unstable internal APIs or a way to reliably identify users across changing tokens—neither of which is feasible for all three providers. The gateway is built for Cloudflare Workers and D1 database, which means deployment is tightly coupled to Cloudflare's infrastructure.

The two documented limitations—non-strict budget enforcement and unreliable cost tracking during failures—represent conscious trade-offs. The author deliberately avoids modifying requests (e.g., clamping max_tokens) and does not implement full monitoring and retry logic, preferring a simpler gateway that forwards requests as-is. Users relying on strict spend limits or perfect billing accuracy would need to add external monitoring or implement request modification themselves.

FAQ

Which LLM providers does this gateway support?
The gateway supports DeepSeek, OpenAI, and Anthropic. It also allows DeepSeek's Anthropic-compatible /v1/messages endpoint to be used with Claude Code CLI as an alternative to Anthropic.
What are the main limitations of the budget control system?
Budget enforcement is not strict—the gateway only checks at the start of a request, so a single large request or concurrent requests can exceed the budget. Cost tracking is also not fully reliable; if the client, upstream, or gateway fails abruptly, the request may still be served but not billed correctly.
Where do the real API keys need to be stored?
Real upstream API keys live only in the Cloudflare Worker's secrets and are never exposed to the client. Clients instead authenticate with opaque tokens stored in a D1 database.

Get the latest Large Language Models news every morning

AI-summarized, only the topics you pick — one digest a day via Email, Slack, or Discord.

Free · takes 30 seconds · unsubscribe anytime

Discussion

No comments yet. Be the first to share your thoughts!

Log in to join the discussion

Related Articles

Stay ahead with AI news

Get curated AI news from 200+ sources delivered daily to your inbox. Free to use.

Get Started Free

Free · takes 30 seconds · unsubscribe anytime

1 minute a day. The AI essentials.

200+ sources · Email / LINE / Slack

Get it free →