|
5 | 5 | using System.Diagnostics; |
6 | 6 | using System.Globalization; |
7 | 7 | using System.Net; |
| 8 | +using System.Text; |
| 9 | +using System.Text.Json; |
8 | 10 | using DevProxy.Abstractions.Plugins; |
9 | 11 | using DevProxy.Abstractions.Proxy; |
10 | 12 | using DevProxy.Plugins.Behavior; |
@@ -165,4 +167,49 @@ public async Task RateLimitingPlusRetryAfter_ThrottledResponseCarriesRetryAfter( |
165 | 167 | out _), |
166 | 168 | "Retry-After header should be an integer seconds value."); |
167 | 169 | } |
| 170 | + |
| 171 | + [Fact] |
| 172 | + public async Task LanguageModelRateLimiting_Throttle_ReturnsOpenAIRateLimitError() |
| 173 | + { |
| 174 | + await using var origin = await FakeOrigin.StartAsync(); |
| 175 | + var urls = KestrelProxyHarness.BuildUrlsToWatch(origin.Host); |
| 176 | + |
| 177 | + var plugin = new LanguageModelRateLimitingPlugin( |
| 178 | + SharedHttpClient, |
| 179 | + NullLogger<LanguageModelRateLimitingPlugin>.Instance, |
| 180 | + urls, |
| 181 | + ProxyConfig, |
| 182 | + PluginConfig.FromJson(""" |
| 183 | + { "promptTokenLimit": 10, "completionTokenLimit": 100, "resetTimeWindowSeconds": 300 } |
| 184 | + """)); |
| 185 | + |
| 186 | + await using var proxy = await KestrelProxyHarness.StartAsync( |
| 187 | + origin.Host, [plugin]); |
| 188 | + using var client = proxy.CreateHttpClient(); |
| 189 | + |
| 190 | + // /echo returns the request body, so the usage below is read back as the |
| 191 | + // response's token usage. #1 drains the prompt token limit, #2 is throttled. |
| 192 | + const string requestBody = """ |
| 193 | + { |
| 194 | + "model": "gpt-4o", |
| 195 | + "messages": [ { "role": "user", "content": "hi" } ], |
| 196 | + "usage": { "prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15 } |
| 197 | + } |
| 198 | + """; |
| 199 | + using var firstContent = new StringContent(requestBody, Encoding.UTF8, "application/json"); |
| 200 | + using var first = await client.PostAsync(new Uri($"http://{origin.Host}/echo"), firstContent); |
| 201 | + using var secondContent = new StringContent(requestBody, Encoding.UTF8, "application/json"); |
| 202 | + using var throttled = await client.PostAsync(new Uri($"http://{origin.Host}/echo"), secondContent); |
| 203 | + |
| 204 | + Assert.Equal(HttpStatusCode.OK, first.StatusCode); |
| 205 | + Assert.Equal(HttpStatusCode.TooManyRequests, throttled.StatusCode); |
| 206 | + Assert.True(throttled.Headers.Contains("retry-after")); |
| 207 | + |
| 208 | + using var json = JsonDocument.Parse(await throttled.Content.ReadAsStringAsync()); |
| 209 | + var error = json.RootElement.GetProperty("error"); |
| 210 | + Assert.Equal("rate_limit_exceeded", error.GetProperty("code").GetString()); |
| 211 | + Assert.Equal("tokens", error.GetProperty("type").GetString()); |
| 212 | + var message = error.GetProperty("message").GetString(); |
| 213 | + Assert.StartsWith("Rate limit reached for gpt-4o on tokens per min (TPM): Limit 10, Used 10.", message, StringComparison.Ordinal); |
| 214 | + } |
168 | 215 | } |
0 commit comments