OpenAI Realtime
Verified Code examples on this page have been automatically tested and verified.Proxy OpenAI Realtime API traffic through agentgateway to get token usage tracking and observability for WebSocket-based interactions.
About
The OpenAI Realtime API uses WebSocket connections for low-latency, multimodal interactions. agentgateway can proxy these WebSocket connections and parse the response.done events to extract token usage data, including input tokens, output tokens, and cached token counts.
To enable token usage tracking, you must prevent the client and server from negotiating WebSocket frame compression. When the sec-websocket-extensions: permessage-deflate header is present, the WebSocket frames are compressed and agentgateway cannot parse the token usage data. Remove this header from the request so that frames remain uncompressed and parseable.
Realtime route type supports token usage tracking and observability. Other LLM policies such as prompt guards, prompt enrichment, and request-body rate limiting are not supported for WebSocket traffic.Before you begin
- Install and set up an agentgateway proxy.
- Set up access to the OpenAI or an OpenAI API-compatible LLM provider.
Step 1: Add the Realtime route type
Verify that your OpenAI AgentgatewayBackend includes the Realtime route type in the policies.ai.routes map. The default behavior routes all traffic as Completions. You must explicitly add the Realtime route type for the /v1/realtime path.
If you already set up multiple endpoints, add the /v1/realtime path to your existing AgentgatewayBackend.
kubectl apply -f- <<EOF
apiVersion: agentgateway.dev/v1alpha1
kind: AgentgatewayBackend
metadata:
name: openai
namespace: agentgateway-system
spec:
ai:
provider:
openai:
model: gpt-4
policies:
auth:
secretRef:
name: openai-secret
ai:
routes:
"/v1/chat/completions": "Completions"
"/v1/realtime": "Realtime"
"*": "Passthrough"
EOFStep 2: Remove the WebSocket compression header
Create an AgentgatewayPolicy resource that removes the sec-websocket-extensions header from requests to the OpenAI Realtime endpoint. This step prevents the client and server from negotiating permessage-deflate compression, which would make WebSocket frames unreadable for token tracking.
Create the AgentgatewayPolicy to strip the header. Target the HTTPRoute section that handles the
/v1/realtimepath.kubectl apply -f- <<EOF apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayPolicy metadata: name: realtime-strip-websocket-extensions namespace: agentgateway-system spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: openai sectionName: openai-realtime traffic: transformation: request: remove: - sec-websocket-extensions EOFVerify that the AgentgatewayPolicy is accepted.
kubectl get agentgatewaypolicy realtime-strip-websocket-extensions -n agentgateway-system
Step 3: Send a Realtime request
Send a request to the OpenAI Realtime API through agentgateway using a WebSocket client. The Realtime API uses WebSocket connections, so standard HTTP tools like curl do not work. Use a WebSocket client such as websocat, wscat, or a custom application.
Connect to the agentgateway proxy URL with the model query parameter and send the following client events as JSON messages.
Create a conversation item with a text message.
{"type":"conversation.item.create","item":{"type":"message","role":"user","content":[{"type":"input_text","text":"Say hello in one word."}]}}Trigger a text response.
{"type":"response.create","response":{"modalities":["text"]}}Look for a
response.doneevent in the server output. This event contains the token usage data that agentgateway extracts for metrics.{"type":"response.done","response":{...,"usage":{"total_tokens":225,"input_tokens":150,"output_tokens":75}}}
Step 4: Verify token tracking
After the Realtime request completes, verify that agentgateway recorded the token usage metrics.
- Open the agentgateway metrics endpoint.
- Look for the
agentgateway_gen_ai_client_token_usagemetric. The metric includes labels for the token type (inputoroutput) and the model used.
For more information about LLM metrics and observability, see LLM cost tracking.
Cleanup
You can remove the resources that you created in this guide.kubectl delete AgentgatewayPolicy realtime-strip-websocket-extensions -n agentgateway-system