Kong authenticates the request, normalizes its headers, and sends it to OpenAI. The Post-Function Policy adds OpenInference attributes to the spans, and the OpenTelemetry Policy exports them to Phoenix.
sequenceDiagram
participant C as Client
participant K as Kong AI Gateway
participant L as OpenAI
participant P as Arize Phoenix
C->>K: POST /llm-tracing-arize-phoenix (apikey, x-correlation-id)
activate K
K->>K: Key Auth AI Auth Strategy validates apikey (else 401)
K->>K: Request Transformer Advanced Policy removes x-user-id
K->>K: Post-Function Policy writes input attributes
K->>L: Chat completion with the AI Model Provider's credentials
activate L
L-->>K: Response
deactivate L
K->>K: Post-Function Policy writes output attributes
K-->>C: Response
K->>P: OpenTelemetry Policy exports spans over OTLP/HTTP
deactivate KThe AI Model lists the three AI Policies in policies. The Post-Function Policy runs after the other AI Policies in each phase and after authentication, so it reads the headers that the Request Transformer Advanced Policy already changed.
Authentication is an AI Auth Strategy, not an AI Policy. The Key Auth AI Auth Strategy reads the key from a request header and matches it against the AI Consumer credentials. On a match, Kong attaches the AI Consumer to the request. The AI Consumer is the identity that rate limits, ACLs, analytics, and in this cookbook, Phoenix’s user column use.
The following settings configure the Key Auth AI Auth Strategy and the AI Consumer:
|
Setting
|
Purpose
|
key_names: [apikey, x-api-key]
|
Kong reads the key from the apikey or x-api-key header. Key Auth compares the whole header value, so it can’t read a key from an Authorization: Bearer header. The note in Run the demo shows the workaround.
|
key_in_header: true, key_in_query: false
|
Kong accepts keys only in headers. Keys in a query string end up in access logs.
|
hide_credentials: true
|
Kong removes the key before it forwards the request, so OpenAI never sees the AI Consumer key.
|
name: phoenix-client (AI Consumer)
|
The Post-Function Policy reads kong.client.get_consumer().username and writes it to user.id. The AI Consumer schema has name but no username. This cookbook hasn’t confirmed that username returns the AI Consumer’s name on a live AI Gateway.
|
credentials[].api_key
|
The AI Consumer’s key. The field is write-only, so it takes a !secret reference to CONSUMER_API_KEY, even for a demo value.
|
Kong adds X-Consumer-Username and related headers to the upstream request, not to the response. Per-consumer usage appears in Konnect analytics.
The Request Transformer Advanced Policy changes the request headers before the Post-Function Policy reads them. It runs remove before add.
The following settings configure the Request Transformer Advanced Policy:
|
Setting
|
Purpose
|
remove.headers: [x-user-id]
|
Removes a client-supplied user header, so that only the AI Consumer identity reaches the trace.
|
add.headers: x-correlation-id
|
Sets x-correlation-id from x-request-id or x-session-id when the client sends one of them. The template is a Lua expression, so or picks the first header that is set. This cookbook hasn’t confirmed that add keeps a client’s own x-correlation-id. If the value is empty, the Post-Function Policy uses the request ID instead.
|
The AI Model Provider holds the OpenAI credentials. The AI Model matches requests on /llm-tracing-arize-phoenix, runs the three AI Policies, and sends the request to OpenAI. When tracing is on, Kong adds gen_ai.* attributes to the spans, for example the model and the token counts. See the Gen AI OpenTelemetry spans attributes reference for the full list.
The following settings configure the AI Model Provider and the AI Model:
|
Setting
|
Purpose
|
config.auth (AI Model Provider)
|
The OpenAI credentials. Kong adds them to each upstream request.
|
formats: [{type: openai}]
|
The AI Model accepts OpenAI-format chat completion requests.
|
policies
|
Attaches the Request Transformer Advanced, OpenTelemetry, and Post-Function Policies to this AI Model only.
|
config.route.model
|
The AI Model answers to the CHAT_MODEL value in the body’s model field (body_param: model), which the demo sends. Without a selector, an AI Model accepts only its own name as model.
|
targets[].name
|
The upstream model, from CHAT_MODEL. Change it and re-apply to swap models without changing clients.
|
config.response_streaming: deny
|
Clients can’t set stream=true. The Post-Function Policy reads the complete response body to write output.value and the output messages.
|
config.max_request_body_size: 10485760
|
Accepts request bodies up to 10 MB, instead of the 8 MB default, for long conversation contexts.
|
config.logging.payloads: true
|
Kong logs request and response payloads. The default is false.
|
config.route.protocols: [http, https]
|
Accepts HTTP, so you can call the local data plane at http://localhost:8000.
|
config.route.strip_path: true
|
Kong removes /llm-tracing-arize-phoenix from the path before it forwards the request.
|
The OpenTelemetry Policy sends the spans from the data plane to Phoenix. The data plane settings from the Kong Konnect prerequisite decide whether Kong records spans at all.
The following settings configure the OpenTelemetry Policy:
|
Setting
|
Purpose
|
traces_endpoint
|
The Phoenix OTLP/HTTP URL, from PHOENIX_OTLP_ENDPOINT, including the /v1/traces path.
|
headers.x-project-name
|
The Phoenix project, from PHOENIX_PROJECT_NAME. Phoenix creates the project on the first span. The header works only on the OTLP/HTTP endpoint, not on gRPC.
|
resource_attributes.service.name
|
The service name on each span: kong-gateway.
|
sampling_rate: 1
|
Samples every request. This value overrides the data plane’s sampling rate. Lower it for production traffic.
|
propagation
|
Reads and writes W3C traceparent headers. When an application sends traceparent, Kong’s spans join the application’s trace.
|
The Post-Function Policy runs Lua in two phases. It reads the spans with kong.tracing.get_spans(). The first span is Kong’s root span, and a span with a gen_ai.operation.name attribute or a name that starts with chat is the LLM span.
The following settings configure the Post-Function Policy:
|
Setting
|
Purpose
|
access
|
Turns on response buffering, then writes openinference.span.kind: CHAIN, session.id, user.id, and input.value on the root span. The LLM span doesn’t exist yet in the access phase, so the code can’t write on it. session.id comes from x-correlation-id, or from the request ID (ngx.var.request_id) when the header is empty. Message parsing reads only string content.
|
header_filter
|
Sets the span status to OK, or to ERROR for a status of 400 or higher. If the response body is JSON, it writes output.value on the root span and the LLM span, and llm.output_messages.* on the LLM span, each cut at 64 KB. If the provider compresses the response (OpenAI sends gzip), it skips them: a binary attribute value makes Phoenix reject the whole span batch with 422.
|
The Lua uses only functions that the default strict Lua sandbox allows. See Lua sandboxing in AI Gateway for the sandbox modes and the allowed functions.
Production credentials: This cookbook stores the AI Consumer’s API key and the OpenAI credentials in environment variables for simplicity. In production, use AI Vaults to reference them from a secret manager, for example AWS Secrets Manager, HashiCorp Vault, GCP Secret Manager, or Azure Key Vault. Spans carry full prompts and responses, so review who can read the Phoenix project.