OpenAI SDK
Use the OpenAI Python or Node.js SDK to send requests through agentgateway deployed in Kubernetes.
Before you begin
Get the gateway URL
export INGRESS_GW_ADDRESS=$(kubectl get svc -n agentgateway-system agentgateway-proxy \
-o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo "Gateway address: $INGRESS_GW_ADDRESS"export INGRESS_GW_ADDRESS=$(kubectl get svc -n agentgateway-system agentgateway-proxy \
-o jsonpath='{.status.loadBalancer.ingress[0].hostname}')
echo "Gateway address: $INGRESS_GW_ADDRESS"After port-forwarding, the gateway is accessible at http://localhost:8080. Use localhost:8080 wherever the instructions reference $INGRESS_GW_ADDRESS.
kubectl port-forward -n agentgateway-system svc/agentgateway-proxy 8080:80Python
Install the OpenAI SDK in your Python project.
pip install openaiCreate and run the following script to send a request through agentgateway. Replace
<route-path>with the path from your HTTPRoute configuration (for example,/openai).ℹ️Do not include/v1in thebase_url— the OpenAI SDK appends it automatically.import os from openai import OpenAI gateway_address = os.environ["INGRESS_GW_ADDRESS"] client = OpenAI( base_url=f"http://{gateway_address}/<route-path>", api_key="anything", # placeholder if gateway has no auth ) response = client.chat.completions.create( model="gpt-4o-mini", messages=[{"role": "user", "content": "Hello from Kubernetes!"}], ) print(response.choices[0].message.content)
Node.js
Install the OpenAI SDK in your Node.js project.
npm install openaiCreate and run the following script to send a request through agentgateway. Replace
<route-path>with the path from your HTTPRoute configuration (for example,/openai).ℹ️Do not include/v1in thebaseURL— the OpenAI SDK appends it automatically.import OpenAI from "openai"; const gatewayAddress = process.env.INGRESS_GW_ADDRESS; const client = new OpenAI({ baseURL: `http://${gatewayAddress}/<route-path>`, apiKey: "anything", }); const response = await client.chat.completions.create({ model: "gpt-4o-mini", messages: [{ role: "user", content: "Hello from Kubernetes!" }], }); console.log(response.choices[0].message.content);