For the complete documentation index, see llms.txt. Markdown versions of all docs pages are available by appending .md to any docs URL.
OpenAI Realtime
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.
You must also force the connection to the LLM provider to use HTTP/1.1. WebSocket upgrades rely on the HTTP/1.1 Upgrade mechanism. If the connection to the provider is negotiated over HTTP/2 instead, the WebSocket handshake fails and the Realtime request does not work. Set policies.http.version to HTTP1 on the EnterpriseAgentgatewayBackend.
Note
The 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 the Solo Enterprise for agentgateway control plane by following the quick start guide.
- Set up access to the OpenAI or an OpenAI API-compatible LLM provider.
Step 1: Add the Realtime route type
Verify that your OpenAI EnterpriseAgentgatewayBackend 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.
Also set policies.http.version to HTTP1 so that the connection to the provider uses HTTP/1.1 instead of negotiating HTTP/2.
If you already set up multiple endpoints, add the /v1/realtime path to your existing EnterpriseAgentgatewayBackend.
kubectl apply -f- <<EOF
apiVersion: enterpriseagentgateway.solo.io/v1alpha1
kind: EnterpriseAgentgatewayBackend
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"
http:
version: HTTP1
EOFStep 2: Remove the WebSocket compression header
Create an EnterpriseAgentgatewayPolicy 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 EnterpriseAgentgatewayPolicy to strip the header. Target the HTTPRoute section that handles the
/v1/realtimepath.kubectl apply -f- <<EOF apiVersion: enterpriseagentgateway.solo.io/v1alpha1 kind: EnterpriseAgentgatewayPolicy 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 EnterpriseAgentgatewayPolicy 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 EnterpriseAgentgatewayPolicy realtime-strip-websocket-extensions -n agentgateway-system