Docs Local Kubernetes Blog Enterprise Community Get Started GitHub

Transformations

About CEL expressions

CEL (Common Expression Language) transformations allows you to use dynamic expressions to extract and transform values from requests and responses.

For an overview of supported CEL expressions, see the agentgateway docs.

About this guide

This guide walks you through how to set up CEL-based transformations for the OpenAI LLM provider. Note that you can use the same AgentgatewayPolicy resource to apply transformations to MCP server, inference, or agent routes.

Before you begin

  1. Set up an agentgateway proxy.
  2. Set up access to the OpenAI LLM provider.
ℹ️
Note that this guide assumes that you want to apply the policy to the OpenAI LLM provider. You can use other LLM providers or apply external auth to an MCP server or agent. Make sure to adjust these steps to apply to your AgentgatewayBackend type.

Set up transformations

  1. Create an AgentgatewayPolicy with your transformation rules. In this example, you use a CEL expression to extract the path from the request. The request path is then added to the response-gateway header.

    kubectl apply -f- <<EOF
    apiVersion: agentgateway.dev/v1alpha1
    kind: AgentgatewayPolicy
    metadata:
      name: transformation
      namespace: agentgateway-system
    spec:
      targetRefs:
      - group: gateway.networking.k8s.io
        kind: Gateway
        name: agentgateway-proxy
      traffic:
        transformation:
          response:
            set:
            - name: response-gateway
              value: "'response path ' + request.path"
    EOF
  2. Send a request to the OpenAI API. Verify that the request succeeds and that you see the response-gateway header with a value of response path /openai.

    curl -v "${INGRESS_GW_ADDRESS}:8080/openai" -H content-type:application/json -H "X-User-ID: user123" -d '{
     "model": "gpt-3.5-turbo",
     "messages": [
       {
         "role": "system",
         "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."
       },
       {
         "role": "user",
         "content": "In the style of Shakespeare, write a series of sonnets that explain the concept of recursion in programming."
       }
     ]
    }'
    curl -v "localhost:8080/openai" -H content-type:application/json -H "X-User-ID: user123" -d '{
     "model": "gpt-3.5-turbo",
     "messages": [
       {
         "role": "system",
         "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."
       },
       {
         "role": "user",
         "content": "In the style of Shakespeare, write a series of sonnets that explain the concept of recursion in programming."
       }
     ]
    }'

    Example output:

    * upload completely sent off: 358 bytes
    < HTTP/1.1 200 OK
    < content-type: application/json
    < access-control-expose-headers: X-Request-ID
    < openai-organization: solo-io-1
    < openai-processing-ms: 1082
    < openai-version: 2020-10-01
    ...
    < response-gateway: response path /openai
    < content-length: 1198
    

Cleanup

You can remove the resources that you created in this guide.
kubectl delete AgentgatewayPolicy transformation -n agentgateway-system