Repository navigation
Expand file tree
/
Copy pathtoken_ratelimit.yaml
More file actions
223 lines (219 loc) · 6.59 KB
/
Copy pathtoken_ratelimit.yaml
File metadata and controls
223 lines (219 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# Copyright Envoy AI Gateway Authors
# SPDX-License-Identifier: Apache-2.0
# The full text of the Apache license is available in the LICENSE file at
# the root of the repo.
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: envoy-ai-gateway-token-ratelimit
spec:
controllerName: gateway.envoyproxy.io/gatewayclass-controller
---
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: envoy-ai-gateway-token-ratelimit
namespace: default
spec:
gatewayClassName: envoy-ai-gateway-token-ratelimit
listeners:
- name: http
protocol: HTTP
port: 80
---
apiVersion: aigateway.envoyproxy.io/v1alpha1
kind: AIGatewayRoute
metadata:
name: envoy-ai-gateway-token-ratelimit
namespace: default
spec:
schema:
name: OpenAI
# parentRefs:
# - name: envoy-ai-gateway-token-ratelimit
# kind: Gateway
# group: gateway.networking.k8s.io
# Use parentRefs above instead of targetRefs below, targetRefs will be deprecated in the next version.
# Here, we use targetRefs just for the reference as well as to ensure there's an upgrade path.
targetRefs:
- name: envoy-ai-gateway-token-ratelimit
kind: Gateway
group: gateway.networking.k8s.io
rules:
- matches:
- headers:
- type: Exact
name: x-ai-eg-model
value: rate-limit-funky-model
backendRefs:
- name: envoy-ai-gateway-token-ratelimit-testupstream
# The following metadata keys are used to store the costs from the LLM request.
llmRequestCosts:
- metadataKey: llm_input_token
type: InputToken
- metadataKey: llm_output_token
type: OutputToken
- metadataKey: llm_total_token
type: TotalToken
# This configures the token limit based on the CEL expression.
# For a demonstration purpose, the CEL expression returns 100000000 only when the input token is 3,
# otherwise it returns 0 (no token usage).
- metadataKey: llm_cel_calculated_token
type: CEL
cel: "input_tokens == uint(3) ? 100000000 : 0"
---
apiVersion: aigateway.envoyproxy.io/v1alpha1
kind: AIServiceBackend
metadata:
name: envoy-ai-gateway-token-ratelimit-testupstream
namespace: default
spec:
schema:
name: OpenAI
backendRef:
name: envoy-ai-gateway-token-ratelimit-testupstream
kind: Backend
group: gateway.envoyproxy.io
---
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: Backend
metadata:
name: envoy-ai-gateway-token-ratelimit-testupstream
namespace: default
spec:
endpoints:
- fqdn:
hostname: envoy-ai-gateway-token-ratelimit-tesetupstream.default.svc.cluster.local
port: 80
---
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
name: envoy-ai-gateway-token-ratelimit-policy
namespace: default
spec:
# Applies the rate limit policy to the gateway.
targetRefs:
- name: envoy-ai-gateway-token-ratelimit
kind: Gateway
group: gateway.networking.k8s.io
rateLimit:
type: Global
global:
rules:
# This configures the input token limit, and it has a different budget than others,
# so it will be rate limited separately.
- clientSelectors:
- headers:
# Have the rate limit budget be per unique "x-user-id" header value.
- name: x-user-id
type: Distinct
limit:
# Configures the number of "tokens" allowed per hour, per user.
requests: 10
unit: Hour
cost:
request:
from: Number
# Setting the request cost to zero allows to only check the rate limit budget,
# and not consume the budget on the request path.
number: 0
response:
from: Metadata
metadata:
# This is the fixed namespace for the metadata used by AI Gateway.
namespace: io.envoy.ai_gateway
# Limit on the input token.
key: llm_input_token
# Repeat the same configuration for a different token type.
# This configures the output token limit, and it has a different budget than others,
# so it will be rate limited separately.
- clientSelectors:
- headers:
- name: x-user-id
type: Distinct
limit:
requests: 100
unit: Hour
cost:
request:
from: Number
number: 0
response:
from: Metadata
metadata:
namespace: io.envoy.ai_gateway
key: llm_output_token
# Repeat the same configuration for a different token type.
# This configures the total token limit, and it has a different budget than others,
# so it will be rate limited separately.
- clientSelectors:
- headers:
- name: x-user-id
type: Distinct
limit:
requests: 1000
unit: Hour
cost:
request:
from: Number
number: 0
response:
from: Metadata
metadata:
namespace: io.envoy.ai_gateway
key: llm_total_token
# Repeat the same configuration for a different token type.
# This configures the token limit based on the CEL expression.
- clientSelectors:
- headers:
- name: x-user-id
type: Distinct
limit:
requests: 1000
unit: Hour
cost:
request:
from: Number
number: 0
response:
from: Metadata
metadata:
namespace: io.envoy.ai_gateway
key: llm_cel_calculated_token
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: envoy-ai-gateway-token-ratelimit-tesetupstream
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: envoy-ai-gateway-token-ratelimit-tesetupstream
template:
metadata:
labels:
app: envoy-ai-gateway-token-ratelimit-tesetupstream
spec:
containers:
- name: testupstream
image: docker.io/envoyproxy/ai-gateway-testupstream:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: envoy-ai-gateway-token-ratelimit-tesetupstream
namespace: default
spec:
selector:
app: envoy-ai-gateway-token-ratelimit-tesetupstream
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP