大模型API错误处理:构建防弹级AI应用
错误处理是构建生产级AI应用的关键。本指南将帮助您全面了解大模型API的错误类型,掌握专业的错误处理策略,确保应用稳定运行。
常见错误类型详解
认证错误(4xx)
401 Unauthorized
不可重试API密钥无效或过期
// 处理方案
if (error.status === 401) {
logger.error('Invalid API key');
// 通知运维更新密钥
await notifyOps('API_KEY_INVALID');
// 返回友好错误
throw new AuthError('服务暂时不可用,请稍后重试');
}403 Forbidden
需检查配额权限不足或配额用尽
// 处理方案
if (error.status === 403) {
const reason = error.data?.reason;
if (reason === 'quota_exceeded') {
// 切换到备用账号或等待重置
return await useBackupAccount();
}
}限流错误(429)
429 Too Many Requests
可重试请求频率超过限制
// 智能重试策略
async function handleRateLimit(error) {
const retryAfter = error.headers['retry-after'] ||
error.headers['x-ratelimit-reset-after'];
if (retryAfter) {
// 使用服务器建议的等待时间
await sleep(retryAfter * 1000);
} else {
// 指数退避算法
const backoff = Math.min(
1000 * Math.pow(2, retryCount),
60000 // 最大等待60秒
);
await sleep(backoff + Math.random() * 1000);
}
return retry();
}服务端错误(5xx)
500 Internal Server Error
可重试服务器内部错误
503 Service Unavailable
可降级服务暂时不可用
业务错误
context_length_exceeded
上下文长度超过模型限制
// 处理方案:智能截断
function truncateContext(messages, maxTokens) {
let totalTokens = 0;
const truncated = [];
// 从最新的消息开始保留
for (let i = messages.length - 1; i >= 0; i--) {
const tokens = countTokens(messages[i]);
if (totalTokens + tokens <= maxTokens) {
truncated.unshift(messages[i]);
totalTokens += tokens;
} else {
break;
}
}
return truncated;
}完整的错误处理架构
class LLMAPIClient {
constructor(config) {
this.config = config;
this.retryConfig = {
maxRetries: 3,
retryableErrors: [429, 500, 502, 503, 504],
backoffMultiplier: 2,
maxBackoff: 60000
};
}
async callAPI(params) {
let lastError;
for (let attempt = 0; attempt <= this.retryConfig.maxRetries; attempt++) {
try {
// 添加超时控制
const response = await this.makeRequest(params, {
timeout: 30000,
signal: AbortSignal.timeout(30000)
});
// 验证响应
this.validateResponse(response);
// 记录成功
this.metrics.recordSuccess(attempt);
return response;
} catch (error) {
lastError = error;
// 记录错误
this.logError(error, attempt);
// 判断是否可重试
if (!this.shouldRetry(error, attempt)) {
throw this.wrapError(error);
}
// 等待后重试
await this.waitBeforeRetry(error, attempt);
// 错误恢复策略
params = await this.applyRecoveryStrategy(error, params);
}
}
throw new MaxRetriesError(lastError);
}
shouldRetry(error, attempt) {
// 不可重试的错误
if (error.code === 'invalid_api_key' ||
error.code === 'insufficient_quota') {
return false;
}
// 达到最大重试次数
if (attempt >= this.retryConfig.maxRetries) {
return false;
}
// 检查HTTP状态码
return this.retryConfig.retryableErrors.includes(error.status);
}
async waitBeforeRetry(error, attempt) {
let delay;
// 优先使用服务器指定的重试时间
if (error.retryAfter) {
delay = error.retryAfter * 1000;
} else {
// 指数退避 + 抖动
delay = Math.min(
1000 * Math.pow(this.retryConfig.backoffMultiplier, attempt),
this.retryConfig.maxBackoff
);
delay += Math.random() * 1000; // 添加随机抖动
}
await new Promise(resolve => setTimeout(resolve, delay));
}
async applyRecoveryStrategy(error, params) {
switch (error.code) {
case 'context_length_exceeded':
// 压缩上下文
return {
...params,
messages: this.compressMessages(params.messages)
};
case 'model_overloaded':
// 降级到更快的模型
return {
...params,
model: this.getFallbackModel(params.model)
};
default:
return params;
}
}
}高级错误处理策略
🔄 断路器模式
class CircuitBreaker {
constructor(threshold = 5, timeout = 60000) {
this.failureCount = 0;
this.threshold = threshold;
this.timeout = timeout;
this.state = 'CLOSED';
this.nextAttempt = Date.now();
}
async call(fn) {
if (this.state === 'OPEN') {
if (Date.now() < this.nextAttempt) {
throw new Error('Circuit breaker is OPEN');
}
this.state = 'HALF_OPEN';
}
try {
const result = await fn();
this.onSuccess();
return result;
} catch (error) {
this.onFailure();
throw error;
}
}
onSuccess() {
this.failureCount = 0;
this.state = 'CLOSED';
}
onFailure() {
this.failureCount++;
if (this.failureCount >= this.threshold) {
this.state = 'OPEN';
this.nextAttempt = Date.now() + this.timeout;
}
}
}🎯 智能降级
class FallbackStrategy {
constructor() {
this.modelHierarchy = [
{ name: 'gpt-4', quality: 10, cost: 10 },
{ name: 'gpt-3.5-turbo', quality: 7, cost: 1 },
{ name: 'cached-response', quality: 5, cost: 0 },
{ name: 'static-response', quality: 3, cost: 0 }
];
}
async execute(task) {
for (const model of this.modelHierarchy) {
try {
if (model.name === 'cached-response') {
return await this.getCachedResponse(task);
}
if (model.name === 'static-response') {
return this.getStaticResponse(task);
}
return await this.callModel(model.name, task);
} catch (error) {
console.warn(`Fallback from ${model.name}`, error);
continue;
}
}
throw new Error('All fallback options exhausted');
}
}错误监控与告警
实时错误追踪系统
监控指标
- 错误率阈值> 1%
- 响应时间P99< 5s
- 重试成功率> 80%
- 降级触发率< 5%
告警规则
// 告警配置
const alerts = {
criticalErrorRate: {
condition: 'error_rate > 5%',
window: '5m',
action: 'page_oncall'
},
highLatency: {
condition: 'p99_latency > 10s',
window: '10m',
action: 'notify_team'
},
quotaWarning: {
condition: 'quota_usage > 80%',
window: '1h',
action: 'email_admin'
}
};错误恢复最佳实践
1. 优雅降级
当主服务不可用时,提供降级但可用的服务
- • 使用缓存的响应
- • 切换到简化功能
- • 提供静态内容
- • 延迟非关键操作
2. 错误隔离
防止错误扩散影响整个系统
- • 使用独立的错误边界
- • 隔离不同功能模块
- • 实施超时控制
- • 限制并发请求数
3. 快速恢复
最小化错误影响,快速恢复服务
- • 自动故障转移
- • 健康检查机制
- • 回滚策略
- • 灾备方案