Est.

API Key Routing Architecture When a Managed Platform Runs Claude Code or Codex Against Your Own Anthropic or OpenAI Account

Where your key lives and where requests flow decide what a platform can enforce.

· 8 min read
Cover illustration for “API Key Routing Architecture When a Managed Platform Runs Claude Code or Codex Against Your Own Anthropic or OpenAI Account”
Cloud Agent Environments · November 19, 2026 · 8 min read · 1,846 words

A bring-your-own API key cloud coding agent runner lets a managed platform run Claude Code or Codex and bill inference to your own Anthropic or OpenAI account, while the platform manages the runner infrastructure for you. Its value depends on the routing architecture. Key storage, gateway injection, and endpoint control determine what the platform can protect, observe, and enforce.

What BYOK routing actually means for a managed platform

A customer-owned credential identifies the account that pays for inference, while the configured endpoint routes requests through the platform’s gateway or directly to the provider. Neither choice establishes the other. A request can use the customer’s key and go straight to the provider, so the platform stays outside the inference path. Routing through a platform alone does not show whose account pays for the request.

Two patterns therefore need distinct treatment. Subscription passthrough uses an organization subscription or embedded credentials, with no API key on the machine. API key routing instead injects the customer’s Anthropic or OpenAI key into upstream requests. These are distinct BYOK billing and custody arrangements. The distinction determines which credentials the platform handles, which provider account receives the charges, and what evidence the platform can offer about each request.

How Claude Code and Codex expose their routing configuration

Claude Code exposes inference routing through ANTHROPIC_BASE_URL and its authentication configuration, including ANTHROPIC_API_KEY. Its configured endpoint covers the main agent, sub-agents, and background tasks. That coverage has a defined boundary, however. A small number of non-inference requests, including the fast-mode availability check and the WebFetch domain safety check, still call api.anthropic.com directly.

Codex exposes its endpoint override through OPENAI_BASE_URL, but credential selection remains a separate choice. The openai-apikey provider path uses the OpenAI Platform account that owns the key, rather than the Codex account login path and its subscription account. Both the destination and the authentication path have to match how the system is meant to work.

Routing coverage also depends on configuration inheritance. Cursor’s documented sub-agent bug ignored the custom base URL and sent those calls to its internal endpoint instead. The bug was present as of July 2026 and resolved by approximately October 2026. Its relevance to Claude Code and Codex is the failure mode illustrated by the resolved bug. A configured main agent cannot, by itself, prove that every subordinate process follows the same route.

The gateway's role: key injection, path rewriting, and credential isolation

The gateway separates session authentication from provider authentication. In the gateway-held-key pattern, the agent sends a session credential to localhost or the platform’s gateway URL and does not hold the customer’s raw provider key. The gateway authenticates that request, retrieves the appropriate credential, and forwards the call with the customer’s key. The provider then bills the associated account. This boundary is where a platform’s custody promise becomes an enforceable mechanism.

agentgateway’s backend authentication guide documents per-route backendAuth injection as an illustration of that separation. The destination path must also match the provider API for authentication to work. Claude Code routes must rewrite to the Anthropic Messages API path, /v1/messages, while Codex routes must resolve to OpenAI’s completions and responses endpoints.

Provider isolation adds another constraint to dispatch. An Anthropic key cannot authenticate an OpenAI route and must never be forwarded to a non-Anthropic endpoint. ProxyPool Hub’s routing update describes per-app credential binding, including routing Claude Code through the customer’s Claude account and Codex through the customer’s Azure OpenAI key. It also describes fallback chains for each binding to use when rate limits are reached. Such fallback remains a credential-routing decision, so it cannot override the member-level ownership boundaries of a managed deployment.

Where keys are stored and how they reach the gateway at request time

A platform’s security properties depend on where the credential rests and which components can retrieve it. In the gateway-held-key architecture, the gateway retrieves the key from the secret store and injects it into the outbound request header. The agent process never gets the raw key. Neither does a developer working inside that session.

Codex Router documents per-user protected files with mode 600 on POSIX and a current-user ACL on Windows, permissions that do not establish encryption at rest. In the managed architecture described here, the corresponding requirement is a per-workspace encrypted secret scoped to the member’s session. Diagnostics can report credential presence and source without exposing the value.

The agent-harness per-member key issue says that end-user Anthropic API keys are the allowed route for hosted Claude Code and that Anthropic bars third parties from collecting, storing, or intermediating Claude.ai session tokens. Subscription forwarding is not a substitute for that design. A compliant implementation under those constraints binds each member’s own API key to that member’s sessions rather than treating a subscription token as a reusable platform credential.

What actually gets routed through the platform versus sent directly to the provider

The inference gateway sees only traffic that traverses it. When a request goes straight to Anthropic or OpenAI, the gateway’s audit log, spend caps, and policy enforcement do not cover it, even if it uses the customer’s own key. Credential ownership does not expand routing coverage. Claude Code’s non-inference exceptions illustrate why a platform must distinguish inference coverage from total network coverage. The historical Cursor bug illustrates the more consequential case of inference itself bypassing the configured gateway.

Code execution introduces a different boundary. Anthropic’s self-hosted runner documentation distinguishes execution on customer compute from inference sent to the provider over HTTPS. The inference route also does not show where the platform stores the session transcript or which part of the system schedules the session.

A security questionnaire needs a separate answer for each of these four points. They are where code executes, where inference runs, where the transcript lives, and who controls session scheduling. Each answer describes a different plane of the system. “Self-hosted,” “managed,” and “BYOK” cannot answer all four without further architectural detail.

What the platform can and cannot observe once routing is established

A gateway can show request volume, token counts, the model tiers chosen, and what each workspace spends in real time. The platform must still record that data and make it available. Controls such as SSO enforcement, organization, group, or user spend caps, and OTLP audit logging also require a session-to-member mapping above the forwarding layer. Anthropic’s self-hosted gateway provides a reference architecture for this pattern through a stateless container backed by PostgreSQL, shipped June 29, 2026.

That architecture still has limits on what it reveals. The routing path alone does not expose model reasoning internals or local tool activity that is not sent as API requests. It also misses traffic from agent processes that do not inherit the base URL configuration. A transcript belongs to the application layer, where the record includes what the agent said, which tools it called, and what they returned. HTTP metadata alone is not session visibility.

The reviewable pull request adds a further, actionable audit artifact. It lets the team inspect the resulting work as a unit, rather than reconstructing the change from individual inference requests. That makes it the highest-fidelity artifact for reviewing the delivered change. It complements, rather than replaces, the transcript and request-level evidence needed for compliance.

How per-member key isolation works in multi-user managed environments

Team-scale BYOK requires more than a separate key for each provider. Every member’s session must use that member’s credential, without exposing it to another member or funding another member’s work. Ownership must survive every dispatch decision. A shared pool, the owner’s key, or an unscoped fallback breaks that requirement even if the provider and endpoint are otherwise correct.

The agent-harness issue specifies this isolation boundary explicitly. Each member adds an Anthropic key and, for Codex, an OpenAI key, with encrypted storage scoped per member. The owner’s subscription and keys remain restricted to the owner’s sessions. The issue describes how sessions are wired for each member but does not show that every such setup uses gateway-held credentials. The same rule applies in both cases, though the designs differ in who holds the raw key.

For a gateway-held implementation, credential lookup must therefore be indexed by member and provider. The gateway resolves the correct entry when dispatching the request. A member’s rate limit cannot justify silently charging the owner’s account. Access controls must also keep diagnostic endpoints or session logs from revealing the credential, because storage isolation fails if another interface gives it away.

Configuring Claude Code and Codex on a managed platform correctly

The practical setup follows four connected checks. First, point the agent’s base URL at the platform gateway. Then establish gateway injection of the customer’s provider key, encrypted per-member storage, and receipt of that key by the upstream provider. These checks span the full authentication path. Stopping at an accepted base URL leaves the billing and custody properties unproven.

For Claude Code, set ANTHROPIC_BASE_URL to the gateway URL and ANTHROPIC_AUTH_TOKEN to the gateway’s session credential. If the gateway reads the x-api-key header instead, use ANTHROPIC_API_KEY for that credential. Otherwise, leave ANTHROPIC_API_KEY empty. The gateway resolves and injects the actual Anthropic key, while sub-agents inherit the configured inference route.

For Codex, set OPENAI_BASE_URL to the gateway URL and use the openai-apikey provider path. Keep provider credentials separate. An Anthropic key assigned as OPENAI_API_KEY cannot authenticate Codex requests to OpenAI, because the providers use separate authentication systems.

Before relying on the integration in production, check that the gateway reports the key as present and the provider as ready. Then send a live request and confirm billing on the customer’s provider account. Presence alone is not end-to-end verification. Also check for the common mistake of pointing the agent directly at the provider, which bypasses gateway visibility and leaves the provider key in the agent environment, outside the platform’s secret store.

Auditing the routing path honestly after setup

After setup, the audit should test each of the four planes on its own. Confirm that code executes in an isolated environment scoped to the member and that inference reaches the provider using that member’s key. Confirm that the platform captures the transcript per session and controls session scheduling rather than leaving it to an autonomous agent process. Each checkpoint tests a distinct architectural claim. A successful inference call establishes none of the other three on its own.

Provider billing is the ground truth for credential injection. Compare the customer’s Anthropic or OpenAI billing record with the platform’s reported usage alongside gateway logs. A mismatch can indicate bypass traffic or injection of the wrong key. Platform audit records should independently identify the member, session, model tier, token count, and timestamp for each request. Together, the platform and provider records connect what was routed with what was billed.

For parallel, multi-member deployments, make that comparison for each member. Checking only the total spend can hide a session that falls back to someone else’s credential. Reviewable pull requests then connect the session evidence to the changes the team can inspect and approve. Genuine BYOK control rests on those verifiable boundaries, from secret storage through dispatch to billing and review, rather than on the presence of a field labeled “API key.”

Sources

  1. Per-member API keys: household members run hosted Claude Code and Codex on their own Anthropic/OpenAI keys · Issue #393 · dflippojr/agent-harness · GitHub
  2. Run Claude Code, Codex, and Antigravity CLI through AI Gateway
  3. Support engine.model-routing for Claude and Codex on native Anthropic/OpenAI providers · Issue #73051 · github/gh-aw · GitHub

More in Cloud Agent Environments