Chapter 2 of 6 · Architecture

Claude Code and Desktop through a Foundry AI gateway

Optional module · Draft 4.5 hours

Chapter 2 of 6

Architecture at a glance#

Claude Code obtains an Entra access token through an Azure CLI helper. Desktop signs in through the browser with authorization code and PKCE, then sends an Entra ID token. Both clients call the same HTTPS gateway base URL.

Claude Code / Claude Desktop
  -> optional Application Gateway WAF
  -> APIM: validate client -> resolve budget -> enforce token quota
  -> Foundry /anthropic/v1/messages, authenticated with APIM managed identity

Entra authenticates the user. APIM validates the issued token against the exact tenant and application, requires Gateway.Invoke, and uses normalized upn plus model as the monthly counter key. The policy removes client key headers and replaces Authorization before forwarding to Foundry.

The backend is https://<foundry-resource>.services.ai.azure.com/anthropic. If the APIM API suffix is foundry, both clients use https://<gateway-hostname>/foundry. APIM removes its API suffix when it builds the backend path, so /foundry/v1/messages reaches /anthropic/v1/messages. Application Gateway, when used, must preserve that mapping and the query string.

Design choices and tradeoffs#

DecisionChosen approachBenefitsCosts and limitations
ProtocolNative Anthropic Messages passthroughPreserves tools, beta fields, and streamingOpenAI-compatible import is a different protocol
Caller tokensSeparate Code access-token and Desktop ID-token validatorsEach audience has its own claim contractDesktop ID-token acceptance is specific to this documented gateway integration
Backend identityAPIM managed identityNo provider keys on developer devicesAPIM acts as a shared principal; Foundry does not authorize the end user
BudgetsAPI-included fragment with defaults and UPN overridesApplies without relying on product policy selectionUPN changes can create a new balance; counters are gateway-local
NetworkExisting approved path, optional WAFAvoids deploying a second gateway just for this modulePublic APIM must be restricted if WAF is the approved sole entry point

The token policy limits prompt and completion usage. Streaming counts are estimated, and concurrent calls can exceed a quota before their responses are processed. A token quota is not a hard currency budget. Cache-related usage and billed Claude Consumption Units need separate cost reconciliation.

Architecture guidance#

Optional module

Claude Code and Desktop through a Foundry AI gateway slide deck