envoy-guide
Module 7 · Going Deeper·18 min·advanced

ext_proc — body mutation, AI-friendly

Stream request and response bodies through an external gRPC service for transformation.

What ext_proc unlocks that nothing else does

envoy.filters.http.ext_authz can inspect requests and add headers, but it cannot mutate response bodies. WASM and Lua can mutate bodies locally, but they're constrained to the languages that compile to the sandbox.

envoy.filters.http.ext_proc is the third option: a streaming gRPC filter that hands every phase of a request and response — headers, body chunks, trailers — to an external service, message by message, in both directions. Your service mutates whatever it wants and streams the result back.

The killer use cases the official site under-sells: PII redaction in responses, prompt rewriting for LLM gateways, content moderation, lossless body-included audit logging, and on-the-fly compression / transcoding.

Minimal envoy.yaml

YAML
http_filters:
- name: envoy.filters.http.ext_proc
  typed_config:
    "@type": type.googleapis.com/envoy.extensions.filters.http.ext_proc.v3.ExternalProcessor
    grpc_service:
      envoy_grpc: { cluster_name: ext_proc }
      timeout: 2s
    failure_mode_allow: false
    processing_mode:
      request_header_mode: SEND
      response_header_mode: SEND
      request_body_mode: NONE
      response_body_mode: BUFFERED         # buffer entire response
      response_trailer_mode: SKIP
- name: envoy.filters.http.router
  typed_config:
    "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

clusters:
- name: ext_proc
  type: STRICT_DNS
  typed_extension_protocol_options:
    envoy.extensions.upstreams.http.v3.HttpProtocolOptions:
      "@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions
      explicit_http_config: { http2_protocol_options: {} }
  load_assignment:
    cluster_name: ext_proc
    endpoints:
    - lb_endpoints:
      - endpoint: { address: { socket_address: { address: ext-proc, port_value: 50051 }}}

A tiny Go ext_proc service (lowercase the body)

go
func (s *srv) Process(stream extprocv3.ExternalProcessor_ProcessServer) error {
  for {
    req, err := stream.Recv()
    if err != nil { return err }
    resp := &extprocv3.ProcessingResponse{}
    switch v := req.Request.(type) {
    case *extprocv3.ProcessingRequest_ResponseBody:
      body := bytes.ToLower(v.ResponseBody.Body)
      resp.Response = &extprocv3.ProcessingResponse_ResponseBody{
        ResponseBody: &extprocv3.BodyResponse{
          Response: &extprocv3.CommonResponse{
            BodyMutation: &extprocv3.BodyMutation{
              Mutation: &extprocv3.BodyMutation_Body{Body: body},
            },
          },
        },
      }
    default:
      resp.Response = &extprocv3.ProcessingResponse_RequestHeaders{
        RequestHeaders: &extprocv3.HeadersResponse{},
      }
    }
    if err := stream.Send(resp); err != nil { return err }
  }
}

Three pitfalls

BUFFERED holds the whole body in memory

response_body_mode: BUFFERED means Envoy waits for the entire body before the client sees a byte. Fine for small JSON. For LLM-streamed responses or large downloads, use STREAMED and emit BodyMutation per chunk — but your service must keep state, and you cannot grow the body across a chunk boundary without re-buffering.

failure_mode_allow: false + flaky processor = outage

A bad deploy of the processor will take production with it. Pair ext_proc with health-checked endpoints, generous outlier detection (Lesson 27), and either failure_mode_allow: true (open) or a circuit breaker so the data plane survives if the processor doesn't.

Order vs ext_authz matters for security

ext_proc before ext_authz means your processor sees unauthenticated traffic — anyone can spoof a request and watch your processor handle it. Put ext_proc after auth filters so it only sees verified traffic, with auth-added headers (x-jwt-sub, etc.) already populated.

Hands-on: lowercase the world

Hands-on · See bidirectional mutation
  1. Run the Go ext_proc above in one container, your Envoy in another (with the config above) on a shared docker network.

  2. Hit an upstream that returns "HELLO WORLD":

Shell
curl -s localhost:10000/anything

The client sees "hello world". The upstream still emits caps; the mutation happens at Envoy.

  1. Switch response_body_mode from BUFFERED to STREAMED. Have the upstream return a 1MB payload and watch in tcpdump (or Envoy access logs with %DURATION%) — bytes arrive at the client incrementally instead of in one blob.

  2. Stop the processor. With failure_mode_allow: false the next request returns 500. Flip to true and confirm requests pass through unmodified.

Real-world example · Helio AI (LLM gateway)
Problem
Helio runs an LLM gateway in front of OpenAI, Anthropic, and a self-hosted Llama. Compliance requires every prompt is scanned for customer PII *before* it leaves the building, and every response is scanned for hallucinated PII *before* it returns. ext_authz can't touch the body. WASM can't embed the Python ML stack.
Envoy solution
A Python ext_proc service using Microsoft Presidio. ext_proc gives them message-by-message mutation in both directions, and the Python team owns its repo separate from the Envoy config. Compliance signs off; product ships in weeks instead of quarters.

When to reach for ext_proc

You need…ext_procWASMLua
Mutate response body✅✅ (small)⚠ (single chunk)
Use Python / heavy ML libs✅❌❌
Sub-millisecond per request❌ (network hop)✅✅
Share state across many Envoys✅ (centralised service)❌❌
No build / toolchain⚠ (need a service)❌✅

ext_proc is the right answer when you're doing AI/ML mutation, when the logic needs to be in Python, or when the policy is updated independently from the proxy.

Quick check

Which capability does ext_proc have that ext_authz does NOT?

Practical task
Finish the quick check and tick off the task to unlock.
Quick checkTasks 0/3