Create an LLM API proxy

This page describes how to create an Apigee API proxy that fronts a large language model (LLM), using either the LLM template in the proxy creation wizard or the Apigee API. It also describes how to format the responses that Apigee generates into an LLM provider's native format. For an overview of LLM proxies and supported providers, see About LLM API proxies.

Before you begin

  • Make sure you have an Apigee environment to deploy the proxy to.
  • For Gemini and Claude models (served through Agent Platform):
    • Enable the Agent Platform API (aiplatform.googleapis.com) in your Google Cloud project.
    • Enable the model for your project in Model Garden. If a model you expect isn't listed in the wizard, use the Enable additional models in Agent Platform link in the model picker.
    • Create a deployment service account and grant it the Agent Platform User (roles/aiplatform.user) role. Grant the user who deploys the proxy the Service Account User (roles/iam.serviceAccountUser) role on that service account. For more information, see Using Google authentication.
  • For OpenAI models: obtain an OpenAI API key. You can either provide the key in the wizard (which Apigee stores in a new encrypted key value map (KVM) at deploy time) or pre-create an encrypted KVM that holds the key in an api-key entry.

Create the proxy

You can create an LLM proxy from the LLM template in the proxy creation wizard (Console), or by authoring a proxy bundle and importing it with the Apigee API (API).

Console

To create an LLM API proxy using the Apigee UI:

  1. In the Apigee UI, go to Proxy development > API proxies, and then click Create to open the proxy creation wizard.

    Create proxy wizard, Template step.

  2. On the Template step, select the LLM template card.
  3. Select a model provider and model from the model list. Apigee uses your selection to pre-fill the proxy's target URL.
  4. Provide outbound authentication for the provider:
    • Gemini or Claude: No credentials are entered here; you select the deployment service account on the Deploy step.
    • OpenAI: In OpenAI authentication, either select Provide an API key and paste your key, or select Use an existing key value map and enter the name of an encrypted KVM that contains an api-key entry.
  5. Click Continue. On the Proxy details step, enter a name and base path. The target URL is pre-filled from the model you selected; you can edit it if needed.
  6. Continue to the Deploy step. Select one or more environments, and—for Gemini and Claude proxies—select the service account to deploy with.
  7. Click Create.

API

To create an LLM proxy with the Apigee API, author a proxy bundle and import it. To make the proxy an LLM proxy, add the <ProxyOperationMode>LLM</ProxyOperationMode> element to the <APIProxy> root element of the bundle. This activates LLM response formatting. For details about the element, see The ProxyOperationMode element.

<!-- apiproxy/my-llm-api.xml -->
<APIProxy name="my-llm-api">
  <ProxyOperationMode>LLM</ProxyOperationMode>
  <Description>My LLM proxy.</Description>
</APIProxy>

Configure the target endpoint and outbound authentication for your provider. The provider is resolved from the target URL (see Provider detection). Select a provider:

Gemini

Target the Agent Platform OpenAI-compatible endpoint. Gemini authenticates with the proxy's deployment service account (Google authentication), so no API key or KVM is required.

<!-- apiproxy/targets/default.xml -->
<TargetEndpoint name="default">
  <HTTPTargetConnection>
    <URL>https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/endpoints/openapi/chat/completions</URL>
  </HTTPTargetConnection>
</TargetEndpoint>

Replace PROJECT_ID with your Google Cloud project ID, and specify the model in the request body (for example, "model": "google/gemini-2.5-flash"). Because this is the OpenAI-compatible endpoint, responses are returned in the OpenAI Chat Completions format. To return the Gemini generateContent format instead, target …/publishers/google/models/MODEL:generateContent.

Claude

Target the model's native Agent Platform endpoint. Claude authenticates with the proxy's deployment service account (Google authentication), so no API key or KVM is required.

<!-- apiproxy/targets/default.xml -->
<TargetEndpoint name="default">
  <HTTPTargetConnection>
    <URL>https://aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/global/publishers/anthropic/models/claude-opus-4:rawPredict</URL>
  </HTTPTargetConnection>
</TargetEndpoint>

Replace PROJECT_ID with your Google Cloud project ID and the model with the Claude model you enabled. Apigee adds the request fields that Agent Platform requires (such as the API version and a maximum token count) when they are absent.

OpenAI

Target OpenAI's Chat Completions endpoint. OpenAI authenticates with an API key that the proxy reads from an encrypted key value map (KVM).

<!-- apiproxy/targets/default.xml -->
<TargetEndpoint name="default">
  <HTTPTargetConnection>
    <URL>https://api.openai.com/v1/chat/completions</URL>
  </HTTPTargetConnection>
</TargetEndpoint>

Create an encrypted key value map (KVM) that holds your API key in an api-key entry, so the proxy can read it at runtime:

# Create an encrypted, environment-scoped KVM.
curl -X POST \
  "https://apigee.googleapis.com/v1/organizations/ORG/environments/ENV/keyvaluemaps" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  -d '{"name": "my-llm-api-openai-credentials", "encrypted": true}'

# Add the OpenAI API key in the api-key entry.
curl -X POST \
  "https://apigee.googleapis.com/v1/organizations/ORG/environments/ENV/keyvaluemaps/my-llm-api-openai-credentials/entries" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  -d '{"name": "api-key", "value": "OPENAI_API_KEY"}'

Replace ORG with your Apigee organization, ENV with the environment where you deploy the proxy, and OPENAI_API_KEY with your key.

Package the apiproxy directory as a ZIP file and import it. The following example imports the bundle as a new API proxy named my-llm-api:

curl -X POST \
  "https://apigee.googleapis.com/v1/organizations/ORG/apis?name=my-llm-api&action=import" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -F "file=@my-llm-api.zip"

Replace ORG with your Apigee organization name. After the bundle is imported, deploy the revision to an environment. For Gemini and Claude proxies, deploy with the service account described in Before you begin:

curl -X POST \
  "https://apigee.googleapis.com/v1/organizations/ORG/environments/ENV/apis/my-llm-api/revisions/1/deployments?serviceAccount=SA_EMAIL" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)"

Replace ENV with the environment to deploy to, SA_EMAIL with the deployment service account, and the revision number if it isn't 1. For OpenAI proxies, omit the serviceAccount query parameter.

Apigee validates <ProxyOperationMode> when you import or create the proxy. An unsupported value rejects the request with an HTTP 400 error, and no revision is created:

{
  "error": {
    "code": 400,
    "message": "Unsupported ProxyOperationMode value 'mvp'.",
    "status": "INVALID_ARGUMENT"
  }
}

Because <ProxyOperationMode> is stored per revision, you can inspect a specific revision's value by fetching that revision:

curl \
  "https://apigee.googleapis.com/v1/organizations/ORG/apis/my-llm-api/revisions/1" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)"

How Apigee calls each provider

After the proxy is created, Apigee calls each provider as follows:

  • Gemini: requests are forwarded to Agent Platform in the OpenAI-compatible chat completions format.
  • Claude: the proxy targets the model's native Agent Platform endpoint. Apigee adds the request fields Agent Platform requires (such as the API version and a maximum token count) when they are absent, so minimal requests succeed.
  • OpenAI: requests are sent to OpenAI's Chat Completions endpoint, authenticated with the API key from your key value map.

Test your API proxy

After the proxy is deployed, send a request to it at your environment group's hostname and the proxy's base path. Use the request body format expected by the selected provider. In each example, replace apigee.example.com with your environment group's hostname and BASE_PATH with the proxy's base path.

Gemini

Gemini uses the OpenAI-compatible chat completions format. Specify the model with a google/ prefix.

curl -X POST "https://apigee.example.com/BASE_PATH" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "google/gemini-2.5-flash",
    "messages": [{"role": "user", "content": "Hello"}]
  }'

Claude

Claude uses the Anthropic Messages format. The model is set by the target URL, and Apigee adds the anthropic_version and max_tokens fields if you omit them.

curl -X POST "https://apigee.example.com/BASE_PATH" \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [{"role": "user", "content": "Hello"}]
  }'

OpenAI

OpenAI uses the Chat Completions format.

curl -X POST "https://apigee.example.com/BASE_PATH" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "Hello"}]
  }'

Format Apigee responses for an LLM proxy

On an LLM proxy, Apigee formats the responses that it generates (policy faults, upstream errors, and custom messages) into the resolved provider's native format, so that AI clients and SDKs can parse them. Because these responses are indistinguishable from the model's, Apigee stays transparent in the agent-to-model interaction, and agents need no gateway-specific code to handle them. Successful responses from the model pass through unchanged. You can format a response in two ways:

  • Custom message. Author an AssignMessage policy with an <LLMResponse> element. You supply plain message text; Apigee wraps it in the provider envelope.
  • Default fault. A policy raises a fault and no AssignMessage with <LLMResponse> runs on the response path. Apigee emits the provider's error envelope carrying the fault message and preserves the fault's HTTP status code.

Author a custom message with LLMResponse

The following AssignMessage policy returns a friendly message. The <Message> element holds the text, and the optional <Reason> element sets a label that Apigee reports in the X-Apigee-LLM-Intercept-Reason response header:

<AssignMessage name="AM-quota-friendly">
  <AssignTo createNew="false" transport="http" type="response"/>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <LLMResponse>
    <Message>You've hit your daily request limit. Try again later, or upgrade your plan for more capacity.</Message>
    <FinishReason>stop</FinishReason>
    <Reason>quota_friendly</Reason>
  </LLMResponse>
</AssignMessage>

To return the message when a policy raises a fault—for example, when a Quota policy is exceeded—reference the policy from a FaultRule:

<FaultRules>
  <FaultRule name="quota-fault">
    <Condition>fault.name = "QuotaViolation"</Condition>
    <Step><Name>AM-quota-friendly</Name></Step>
  </FaultRule>
</FaultRules>

The <LLMResponse> element is the same for every provider; only the response envelope on the wire differs. The HTTP status is 200 by default, or the value you set in <StatusCode>. A status below 400 produces the provider's success envelope; a status of 400 or higher produces the provider's error envelope.

For the policy above, Apigee returns one of the following, depending on the provider resolved from the target URL:

OpenAI

Returned for an OpenAI target and for a Gemini proxy that targets the Agent Platform OpenAI-compatible endpoint:

HTTP/1.1 200 OK
Content-Type: application/json
X-Apigee-LLM-Intercept-Reason: quota_friendly
{
  "id": "chatcmpl-...",
  "object": "chat.completion",
  "created": 1752480000,
  "model": "gpt-4o",
  "choices": [{
    "index": 0,
    "message": {"role": "assistant", "content": "You've hit your daily request limit. Try again later, or upgrade your plan for more capacity."},
    "finish_reason": "stop"
  }]
}

Gemini

Returned for a proxy that targets the Gemini native generateContent endpoint:

HTTP/1.1 200 OK
Content-Type: application/json
X-Apigee-LLM-Intercept-Reason: quota_friendly
{
  "candidates": [{
    "content": {
      "parts": [{"text": "You've hit your daily request limit. Try again later, or upgrade your plan for more capacity."}],
      "role": "model"
    },
    "finishReason": "STOP",
    "index": 0
  }],
  "modelVersion": "gemini-2.5-flash"
}

Anthropic (Claude)

Returned for a Claude proxy that targets the Agent Platform Anthropic native endpoint:

HTTP/1.1 200 OK
Content-Type: application/json
X-Apigee-LLM-Intercept-Reason: quota_friendly
{
  "id": "msg_...",
  "type": "message",
  "role": "assistant",
  "content": [{"type": "text", "text": "You've hit your daily request limit. Try again later, or upgrade your plan for more capacity."}],
  "model": "claude-3-5-sonnet",
  "stop_reason": "end_turn"
}

Return an error with a specific status code

To return an error envelope, set <StatusCode> to 400 or higher:

<AssignMessage name="AM-safety-block">
  <AssignTo createNew="false" transport="http" type="response"/>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <LLMResponse>
    <Message>This request violates our safety policy.</Message>
    <Reason>safety_block</Reason>
    <StatusCode>451</StatusCode>
  </LLMResponse>
</AssignMessage>

Apigee returns the provider's error envelope with the status code you set. For example, for an OpenAI proxy:

HTTP/1.1 451 Unavailable For Legal Reasons
Content-Type: application/json
X-Apigee-LLM-Intercept-Reason: safety_block
{
  "error": {
    "message": "This request violates our safety policy.",
    "type": "invalid_request_error",
    "param": null,
    "code": null
  }
}

Default fault responses

If a policy raises a fault and no AssignMessage with <LLMResponse> runs on the response path, Apigee emits the provider's error envelope carrying the fault message and preserves the fault's HTTP status code. For example, when a Quota policy is exceeded on an OpenAI proxy:

HTTP/1.1 429 Too Many Requests
Content-Type: application/json
{
  "error": {
    "message": "Quota exceeded for policy Quota-1.",
    "type": "rate_limit_error",
    "param": null,
    "code": null
  }
}

FinishReason mapping

When you set <FinishReason>, Apigee maps your value to each provider's native field. Values that a provider cannot represent fall back to that provider's closest equivalent.

<FinishReason> value Gemini finishReason OpenAI finish_reason Anthropic stop_reason
stop (default)STOPstopend_turn
max_tokensMAX_TOKENSlengthmax_tokens
safetySAFETYcontent_filterend_turn
tool_callOTHERtool_callstool_use
otherOTHERstopend_turn

What's next