Skip to content

Commit 8e21ed3

Browse files
committed
fix(agents): add apiKeyIndex and attemptNumber fields to rotation callbacks
The existing field in ApiKeyRetryParams tracks the key-rotation position (0-based index), not the per-key retry count. Changing its semantics would break existing plugin consumers. Instead, add explicit (0-based key position) and (1-based per-key retry count) fields while preserving the existing behavior for backward compatibility. Mark as deprecated to guide consumers toward the new fields. Fixes #102938
1 parent f7a665a commit 8e21ed3

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

‎src/agents/api-key-rotation.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,41 @@ describe("executeWithApiKeyRotation", () => {
312312
}),
313313
);
314314
});
315+
316+
it("passes apiKeyIndex and attemptNumber to rotation callbacks", async () => {
317+
const shouldRetry = vi.fn(() => true);
318+
const onRetry = vi.fn();
319+
const execute = vi
320+
.fn<(apiKey: string) => Promise<string>>()
321+
.mockRejectedValueOnce(new Error("Rate limit"))
322+
.mockResolvedValueOnce("ok");
323+
324+
await expect(
325+
executeWithApiKeyRotation({
326+
provider: "openai",
327+
apiKeys: ["key-1", "key-2"],
328+
shouldRetry,
329+
onRetry,
330+
execute,
331+
}),
332+
).resolves.toBe("ok");
333+
334+
// shouldRetry receives apiKeyIndex (0) and attemptNumber (1)
335+
expect(shouldRetry).toHaveBeenCalledWith(
336+
expect.objectContaining({
337+
attempt: 0,
338+
apiKeyIndex: 0,
339+
attemptNumber: 1,
340+
}),
341+
);
342+
343+
// onRetry receives apiKeyIndex (0) and attemptNumber (1)
344+
expect(onRetry).toHaveBeenCalledWith(
345+
expect.objectContaining({
346+
attempt: 0,
347+
apiKeyIndex: 0,
348+
attemptNumber: 1,
349+
}),
350+
);
351+
});
315352
});

‎src/agents/api-key-rotation.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ import { collectProviderApiKeys, isApiKeyRateLimitError } from "./live-auth-keys
1818
type ApiKeyRetryParams = {
1919
apiKey: string;
2020
error: unknown;
21+
/** @deprecated Use apiKeyIndex for key-rotation position or attemptNumber for per-key retry count. */
2122
attempt: number;
23+
/** Zero-based index of the current key in the rotation array. */
24+
apiKeyIndex: number;
25+
/** One-based attempt count for the current key (resets to 1 when rotating to a new key). */
26+
attemptNumber: number;
2227
};
2328

2429
type ExecuteWithApiKeyRotationOptions<T> = {
@@ -65,7 +70,14 @@ export async function executeWithApiKeyRotation<T>(
6570
lastError = error;
6671
const message = formatErrorMessage(error);
6772
const rotateKey = params.shouldRetry
68-
? params.shouldRetry({ apiKey, error, attempt: apiKeyIndex, message })
73+
? params.shouldRetry({
74+
apiKey,
75+
error,
76+
attempt: apiKeyIndex,
77+
apiKeyIndex,
78+
attemptNumber,
79+
message,
80+
})
6981
: isApiKeyRateLimitError(message);
7082

7183
if (rotateKey) {
@@ -74,7 +86,14 @@ export async function executeWithApiKeyRotation<T>(
7486
if (apiKeyIndex + 1 >= keys.length) {
7587
break;
7688
}
77-
params.onRetry?.({ apiKey, error, attempt: apiKeyIndex, message });
89+
params.onRetry?.({
90+
apiKey,
91+
error,
92+
attempt: apiKeyIndex,
93+
apiKeyIndex,
94+
attemptNumber,
95+
message,
96+
});
7897
break;
7998
}
8099

0 commit comments

Comments
 (0)