YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix: return 503 for spending limit errors instead of raw 400
Louis Beaumont32990725fda9
diff --git a/packages/ai-gateway/src/providers/anthropic-proxy.ts b/packages/ai-gateway/src/providers/anthropic-proxy.tsindex ce7a57ee2..5687db148 100644--- a/packages/ai-gateway/src/providers/anthropic-proxy.ts+++ b/packages/ai-gateway/src/providers/anthropic-proxy.ts@@ -69,7 +69,32 @@ export async function proxyToAnthropic(if (!response.ok) {const error = await response.text();console.error('Anthropic API error:', response.status, error);- // Return the Anthropic error as-is — clients already expect this format++ // Detect spending/rate limit errors and return 503 so clients know to retry+ // instead of surfacing a confusing 400 "invalid_request_error"+ const isSpendingLimit = error.includes('reached your specifi') || error.includes('spending limit');+ const isRateLimit = error.includes('rate_limit') || response.status === 429;++ if (isSpendingLimit) {+ return new Response(JSON.stringify({+ type: 'error',+ error: {+ type: 'overloaded_error',+ message: 'AI service is temporarily at capacity. Please try again in a few minutes.',+ },+ }), {+ status: 503,+ headers: { 'Content-Type': 'application/json', 'Retry-After': '60' },+ });+ }++ if (isRateLimit) {+ return new Response(error, {+ status: 429,+ headers: { 'Content-Type': 'application/json', 'Retry-After': '30' },+ });+ }+return new Response(error, {status: response.status,headers: { 'Content-Type': 'application/json' },diff --git a/packages/ai-gateway/src/test/anthropic-proxy.unit.test.ts b/packages/ai-gateway/src/test/anthropic-proxy.unit.test.tsindex 04cf01023..022a43fce 100644--- a/packages/ai-gateway/src/test/anthropic-proxy.unit.test.ts+++ b/packages/ai-gateway/src/test/anthropic-proxy.unit.test.ts@@ -185,6 +185,62 @@ describe('proxyToAnthropic', () => {expect(data.error.type).toBe('invalid_request_error');});+ it('should return 503 for spending limit errors instead of raw 400', async () => {+ globalThis.fetch = async () => {+ return new Response(JSON.stringify({+ type: 'error',+ error: {+ type: 'invalid_request_error',+ message: 'You have reached your specified monthly spending limit for this API key.',+ },+ }), { status: 400, headers: { 'Content-Type': 'application/json' } });+ };++ const request = new Request('http://localhost/v1/messages', {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({+ model: 'claude-opus-4-6',+ max_tokens: 100,+ messages: [{ role: 'user', content: 'test' }],+ }),+ });++ const response = await proxyToAnthropic(request, 'sk-ant-test-key');+ expect(response.status).toBe(503);+ expect(response.headers.get('Retry-After')).toBe('60');++ const data: any = await response.json();+ expect(data.error.type).toBe('overloaded_error');+ expect(data.error.message).toContain('temporarily at capacity');+ });++ it('should return 429 for rate limit errors', async () => {+ globalThis.fetch = async () => {+ return new Response(JSON.stringify({+ type: 'error',+ error: {+ type: 'rate_limit_error',+ message: 'Rate limited',+ },+ }), { status: 429, headers: { 'Content-Type': 'application/json' } });+ };++ const request = new Request('http://localhost/v1/messages', {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({+ model: 'claude-opus-4-6',+ max_tokens: 100,+ messages: [{ role: 'user', content: 'test' }],+ }),+ });++ const response = await proxyToAnthropic(request, 'sk-ant-test-key');+ expect(response.status).toBe(429);+ expect(response.headers.get('Retry-After')).toBe('30');+ });+it('should handle network/fetch errors gracefully', async () => {globalThis.fetch = async () => {throw new Error('Network timeout');