> ## Documentation Index
> Fetch the complete documentation index at: https://tyk.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Run Tyk AI Studio With the Kubernetes Inference Gateway

> Put the Tyk AI Studio Edge Gateway in front of a Gateway API Inference Extension deployment to add authentication, budgets, filters, and analytics to self-hosted models.

## Availability

| Edition | Deployment Type |
| :- | :- |
| [Community](https://tyk.io/docs/ai-management/ai-studio/overview.md#community-edition) & [Enterprise](https://tyk.io/docs/ai-management/ai-studio/overview.md#enterprise-edition) | Self-Managed, Hybrid |

The Edge Gateway works with the Kubernetes Inference Gateway in both editions.

## Overview

The [Gateway API Inference Extension](https://gateway-api-inference-extension.sigs.k8s.io/) routes inference requests to self-hosted model servers, such as vLLM, SGLang, or TGI. Put the Tyk AI Studio Edge Gateway in front of it. The Edge Gateway adds App authentication, LLM access, budgets, filters, and token and cost analytics.

The Endpoint Picker of the extension selects a model server pod of an `InferencePool` for each request. The InferencePool serves an OpenAI-compatible API, so you add it to AI Studio as an LLM provider with the OpenAI vendor.

```mermaid theme={null}
flowchart TB
    C[Client with an App credential] --> GW[Kubernetes Gateway and HTTPRoute]
    GW --> EG["Tyk AI Studio Edge Gateway<br/>authentication, budgets, filters, analytics"]
    EG --> IR["Route to the InferencePool<br/>Endpoint Picker selects the pod"]
    IR --> P[vLLM, SGLang, or TGI pods]
    AS[Tyk AI Studio control plane] -. configuration over gRPC .-> EG
```

### What AI Studio Does Not Do

The Edge Gateway is not a Gateway API Inference Extension implementation. It does not watch `InferencePool` resources, implement the Endpoint Picker protocol, or select model server pods. For pod selection, put the Edge Gateway in front of a Kubernetes Gateway API implementation that supports the Inference Extension.

## Before You Start

* Tyk AI Studio installed with Helm. Refer to [Install Tyk AI Studio on Kubernetes](https://tyk.io/docs/ai-management/ai-studio/deployment-k8s.md).
* A Kubernetes Gateway API implementation with Inference Extension support, and an `InferencePool` that serves your models.
* The Prometheus Operator, if you turn on the `ServiceMonitor`. Without its CRDs, the install fails.
* An OpenTelemetry collector, if you turn on tracing.

## Deploy the Edge Gateway

The [AI Studio repository](https://github.com/TykTechnologies/ai-studio) includes reference values for this setup: `helm/my-values-inference-gateway.yaml`. The file does these things:

* Runs two Edge Gateway replicas with a PodDisruptionBudget and a spread across zones.
* Puts the Edge Gateways in the edge namespace `inference`.
* Blocks upstreams on internal addresses, except in-cluster Services. Refer to [Allow the Edge Gateway to Reach the InferencePool](#allow-the-edge-gateway-to-reach-the-inferencepool).
* Exposes metrics for Prometheus and sends traces to an OTLP collector.
* Uses a StatefulSet with a volume, so that buffered analytics survive a restart.
* Reads the Edge Gateway secrets from a Secret that you manage.

The file sets only the values for this setup. Use it with the values file of your AI Studio installation. Helm applies the files in order:

```bash theme={null}
cd ai-studio/helm
helm upgrade --install midsommar . \
  -f values-production.yaml \
  -f my-values-inference-gateway.yaml
```

Before you install, check these values:

| Value | What to Set |
| :- | :- |
| `microgateway.config.controlEndpoint` | Sets [CONTROL\_ENDPOINT](https://tyk.io/docs/ai-management/ai-studio/edge-gateway-env.md#control_endpoint): the AI Studio Service and its gRPC port, for example `midsommar:50051`. |
| `microgateway.config.edgeNamespace` | Sets [EDGE\_NAMESPACE](https://tyk.io/docs/ai-management/ai-studio/edge-gateway-env.md#edge_namespace). The file uses `inference`. |
| `microgateway.secrets.existingSecret` | The name of a Secret that you create. It must contain the keys [EDGE\_AUTH\_TOKEN](https://tyk.io/docs/ai-management/ai-studio/edge-gateway-env.md#edge_auth_token), [ENCRYPTION\_KEY](https://tyk.io/docs/ai-management/ai-studio/edge-gateway-env.md#encryption_key), and [TYK\_AI\_LICENSE](https://tyk.io/docs/ai-management/ai-studio/edge-gateway-env.md#tyk_ai_license), with the same values as AI Studio. |

The AI Studio Service name is the release name if it contains `midsommar`, else `<release>-midsommar`. For example, the release `ai-studio` gives `ai-studio-midsommar:50051`.

For all chart values, refer to the [values file of the chart](https://github.com/TykTechnologies/ai-studio/blob/main/helm/values.yaml).

Each Edge Gateway pod registers with AI Studio under its pod name. To check the Edge Gateways, go to **Portal administration > Edge Gateways**.

![Edge Gateways page with a connected Edge Gateway, its namespace, and its sync status](https://mintcdn.com/tyk/ApSc5Sm_i3ZOIekL/img/ai-management/ai-studio-k8s-inference-edge-gateways.png?fit=max\&auto=format\&n=ApSc5Sm_i3ZOIekL\&q=85\&s=679d09be872ad7bc6c36e170b8efa239)

## Allow the Edge Gateway to Reach the InferencePool

From v2.3.0, the Edge Gateway blocks internal addresses by default ([LLM\_UPSTREAM\_BLOCK\_INTERNAL](https://tyk.io/docs/ai-management/ai-studio/edge-gateway-env.md#llm_upstream_block_internal)). This also blocks the cluster address of the InferencePool. To keep the block and allow the cluster, set both values:

```yaml theme={null}
microgateway:
  config:
    blockInternalUpstreams: true            # LLM_UPSTREAM_BLOCK_INTERNAL
    allowedInternalHosts: ".svc.cluster.local"  # LLM_UPSTREAM_ALLOWED_INTERNAL_HOSTS
```

[LLM\_UPSTREAM\_ALLOWED\_INTERNAL\_HOSTS](https://tyk.io/docs/ai-management/ai-studio/edge-gateway-env.md#llm_upstream_allowed_internal_hosts) is a comma-separated list of hosts:

* An entry that starts with a dot matches the domain and its subdomains. For example, `.svc.cluster.local` matches `vllm.inference.svc.cluster.local`. It does not match `evilsvc.cluster.local`.
* Other entries must match the host exactly. The match is not case-sensitive.
* A host in this list is permitted, even when [LLM\_UPSTREAM\_ALLOWED\_HOSTS](https://tyk.io/docs/ai-management/ai-studio/edge-gateway-env.md#llm_upstream_allowed_hosts) is set. The gateway checks this list first.
* The gateway applies the list when it checks the upstream URL and again when it connects.

Do not use `LLM_UPSTREAM_ALLOWED_HOSTS` for in-cluster hosts. When it is set, the gateway refuses every upstream host that is not in the list, including the hosted vendors. For the related setting, refer to [ALLOW\_INTERNAL\_NETWORK\_ACCESS](https://tyk.io/docs/ai-management/ai-studio/edge-gateway-env.md#allow_internal_network_access).

## Create the LLM Provider

In AI Studio, create an LLM provider for the InferencePool. Refer to [Create an LLM Provider](https://tyk.io/docs/ai-management/ai-studio/how-to-configure-llm-provider.md#create-an-llm-provider).

| Field | Value |
| :- | :- |
| **Vendor** | **OpenAI**. The InferencePool serves the OpenAI-compatible API. |
| **API Endpoint** | The in-cluster URL that sends requests to the InferencePool, with `/v1`. |
| **API Key** | The key that the model server expects. If it needs no key, enter a placeholder. |
| **Allowed Models** | Regular expressions for the models that the InferencePool serves, for example `^meta-llama/Llama-3\.1-8B-Instruct$`. A pattern matches anywhere in the model name. |
| **Edge Availability** (Enterprise) | The edge namespace of the Edge Gateways, for example `inference`. Leave it empty to send the LLM to all Edge Gateways. In the Community Edition, every Edge Gateway receives all LLMs. |

> [!NOTE]
>
> The Endpoint Picker selects a pod only for requests that pass through a Kubernetes Gateway route to the InferencePool. If the API endpoint is the Service of the model server pods, the requests do not go through the Endpoint Picker.

![Edit LLM provider form with the OpenAI vendor, an in-cluster API endpoint, and the Restricted privacy level](https://mintcdn.com/tyk/ApSc5Sm_i3ZOIekL/img/ai-management/ai-studio-k8s-inference-llm-endpoint.png?fit=max\&auto=format\&n=ApSc5Sm_i3ZOIekL\&q=85\&s=f7bdcc180118fc278f4f88be95d3d7ce)

All other settings work as for a hosted vendor: privacy levels, budgets, filters, failover, and App access.

An Edge Gateway in the namespace `inference` receives the LLMs and Apps in that namespace and the global ones (no namespace). Make sure that the Apps that call the InferencePool are global or in the same namespace.

![Edge Availability section of the LLM provider form with the inference namespace selected](https://mintcdn.com/tyk/ApSc5Sm_i3ZOIekL/img/ai-management/ai-studio-k8s-inference-edge-availability.png?fit=max\&auto=format\&n=ApSc5Sm_i3ZOIekL\&q=85\&s=2bbd68ce82c6e953ffe5d1745622f8cb)

After you save, push the configuration to the Edge Gateways. Refer to [Push Configuration to Edge Gateways](https://tyk.io/docs/ai-management/ai-studio/how-to-push-edge-configuration.md).

## Route Traffic to the Edge Gateway

Add the Edge Gateway Service as a backend of an `HTTPRoute`. With the release name `midsommar`, the Service is `midsommar-microgateway` on port `8080`:

```yaml expandable theme={null}
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
  name: ai-gateway
spec:
  parentRefs:
    - name: inference-gateway
  hostnames:
    - ai.example.com
  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /v1
      backendRefs:
        - name: midsommar-microgateway
          port: 8080
```

The same Kubernetes Gateway can also have the route to the InferencePool. Then use a different hostname or listener for each route. The two routes then do not match the same requests.

Clients authenticate with an App credential and call the Edge Gateway:

* The Main Ingress, `/v1/chat/completions`, with the model string `{llmSlug}/{model}`.
* The OpenAI-compatible endpoint, `/ai/{llmSlug}/v1/chat/completions`.

Refer to [Endpoints](https://tyk.io/docs/ai-management/ai-studio/proxy.md#endpoints).

## Metrics and Traces

The reference values turn on metrics and tracing for the Edge Gateways:

| Value | Environment Variable | Effect |
| :- | :- | :- |
| `microgateway.metrics.allowUnauthenticated` | [METRICS\_ALLOW\_UNAUTHENTICATED](https://tyk.io/docs/ai-management/ai-studio/edge-gateway-env.md#metrics_allow_unauthenticated) | Serves `/metrics` without a token. Use it only for scrapes from inside the cluster. |
| `microgateway.metrics.serviceMonitor.enabled` | None | Creates a `ServiceMonitor` for the Prometheus Operator. |
| `microgateway.metrics.legacyNames` | `METRICS_LEGACY_NAMES` | Also sends the older `aistudio_*` metric names. Set it to `false` after your dashboards use the OpenTelemetry GenAI names. |
| `microgateway.tracing.endpoint` | [TRACING\_ENDPOINT](https://tyk.io/docs/ai-management/ai-studio/edge-gateway-env.md#tracing_endpoint) | The OTLP/gRPC collector, for example `otel-collector.observability:4317`. |

The Edge Gateway continues the trace of an incoming `traceparent` header, and sends it to the upstream. Thus, one trace can show the request from the Kubernetes Gateway, through the Edge Gateway, to the model server.
