Retry openrouter generation endpoint to fix context limit errors

This commit is contained in:
Saoud Rizwan 2025-02-08 20:15:38 -08:00
parent 4d406fd989
commit 43200007cf
2 changed files with 30 additions and 15 deletions

View file

@ -194,8 +194,31 @@ export class OpenRouterHandler implements ApiHandler {
// }
}
await delay(500) // FIXME: necessary delay to ensure generation endpoint is ready
if (genId) {
await delay(500) // FIXME: necessary delay to ensure generation endpoint is ready
try {
const generationIterator = this.fetchGenerationDetails(genId)
const generation = (await generationIterator.next()).value
// console.log("OpenRouter generation details:", generation)
yield {
type: "usage",
// cacheWriteTokens: 0,
// cacheReadTokens: 0,
// openrouter generation endpoint fails often
inputTokens: generation?.native_tokens_prompt || 0,
outputTokens: generation?.native_tokens_completion || 0,
totalCost: generation?.total_cost || 0,
}
} catch (error) {
// ignore if fails
console.error("Error fetching OpenRouter generation details:", error)
}
}
}
@withRetry({ maxRetries: 3, baseDelay: 200, maxDelay: 600, retryAllErrors: true })
async *fetchGenerationDetails(genId: string) {
// console.log("Fetching generation details for:", genId)
try {
const response = await axios.get(`https://openrouter.ai/api/v1/generation?id=${genId}`, {
headers: {
@ -203,21 +226,11 @@ export class OpenRouterHandler implements ApiHandler {
},
timeout: 5_000, // this request hangs sometimes
})
const generation = response.data?.data
console.log("OpenRouter generation details:", response.data)
yield {
type: "usage",
// cacheWriteTokens: 0,
// cacheReadTokens: 0,
// openrouter generation endpoint fails often
inputTokens: generation?.native_tokens_prompt || 0,
outputTokens: generation?.native_tokens_completion || 0,
totalCost: generation?.total_cost || 0,
}
yield response.data?.data
} catch (error) {
// ignore if fails
console.error("Error fetching OpenRouter generation details:", error)
throw error
}
}

View file

@ -2,16 +2,18 @@ interface RetryOptions {
maxRetries?: number
baseDelay?: number
maxDelay?: number
retryAllErrors?: boolean
}
const DEFAULT_OPTIONS: Required<RetryOptions> = {
maxRetries: 3,
baseDelay: 1_000,
maxDelay: 10_000,
retryAllErrors: false,
}
export function withRetry(options: RetryOptions = {}) {
const { maxRetries, baseDelay, maxDelay } = { ...DEFAULT_OPTIONS, ...options }
const { maxRetries, baseDelay, maxDelay, retryAllErrors } = { ...DEFAULT_OPTIONS, ...options }
return function (_target: any, _propertyKey: string, descriptor: PropertyDescriptor) {
const originalMethod = descriptor.value
@ -25,7 +27,7 @@ export function withRetry(options: RetryOptions = {}) {
const isRateLimit = error?.status === 429
const isLastAttempt = attempt === maxRetries - 1
if (!isRateLimit || isLastAttempt) {
if ((!isRateLimit && !retryAllErrors) || isLastAttempt) {
throw error
}