Skip to content

Commit 059de0f

Browse files
authored
Merge pull request RooCodeInc#915 from jcbdev/openroute_api_usage_data_failures
add retries to openrouter generation endpoint to prevent failures on …
2 parents ceec089 + b85960b commit 059de0f

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

src/api/providers/openrouter.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -185,31 +185,35 @@ export class OpenRouterHandler implements ApiHandler, SingleCompletionHandler {
185185
// }
186186
}
187187

188-
await delay(500) // FIXME: necessary delay to ensure generation endpoint is ready
189-
190-
try {
191-
const response = await axios.get(`https://openrouter.ai/api/v1/generation?id=${genId}`, {
192-
headers: {
193-
Authorization: `Bearer ${this.options.openRouterApiKey}`,
194-
},
195-
timeout: 5_000, // this request hangs sometimes
196-
})
188+
// retry fetching generation details
189+
let attempt = 0
190+
while (attempt++ < 10) {
191+
await delay(200) // FIXME: necessary delay to ensure generation endpoint is ready
192+
try {
193+
const response = await axios.get(`https://openrouter.ai/api/v1/generation?id=${genId}`, {
194+
headers: {
195+
Authorization: `Bearer ${this.options.openRouterApiKey}`,
196+
},
197+
timeout: 5_000, // this request hangs sometimes
198+
})
197199

198-
const generation = response.data?.data
199-
console.log("OpenRouter generation details:", response.data)
200-
yield {
201-
type: "usage",
202-
// cacheWriteTokens: 0,
203-
// cacheReadTokens: 0,
204-
// openrouter generation endpoint fails often
205-
inputTokens: generation?.native_tokens_prompt || 0,
206-
outputTokens: generation?.native_tokens_completion || 0,
207-
totalCost: generation?.total_cost || 0,
208-
fullResponseText,
209-
} as OpenRouterApiStreamUsageChunk
210-
} catch (error) {
211-
// ignore if fails
212-
console.error("Error fetching OpenRouter generation details:", error)
200+
const generation = response.data?.data
201+
console.log("OpenRouter generation details:", response.data)
202+
yield {
203+
type: "usage",
204+
// cacheWriteTokens: 0,
205+
// cacheReadTokens: 0,
206+
// openrouter generation endpoint fails often
207+
inputTokens: generation?.native_tokens_prompt || 0,
208+
outputTokens: generation?.native_tokens_completion || 0,
209+
totalCost: generation?.total_cost || 0,
210+
fullResponseText,
211+
} as OpenRouterApiStreamUsageChunk
212+
return
213+
} catch (error) {
214+
// ignore if fails
215+
console.error("Error fetching OpenRouter generation details:", error)
216+
}
213217
}
214218
}
215219
getModel(): { id: string; info: ModelInfo } {

0 commit comments

Comments
 (0)