Skip to content

Commit 409268d

Browse files
committed
Randomly choose between api keys to use based on
1 parent 78f036e commit 409268d

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

backend/src/claude.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { RATE_LIMIT_POLICY } from './constants'
77
import { env } from './env.mjs'
88
import { saveMessage } from './billing/message-cost-tracker'
99
import { logger } from './util/logger'
10+
import { randBoolFromStr } from 'common/util/string'
1011

1112
export type model_types = (typeof claudeModels)[keyof typeof claudeModels]
1213

@@ -38,8 +39,10 @@ export const promptClaudeStream = async function* (
3839
ignoreHelicone = false,
3940
} = options
4041

41-
const apiKey =
42-
Math.random() > 0.5 ? env.ANTHROPIC_API_KEY : env.ANTHROPIC_API_KEY2
42+
// Randomly choose between the two API keys to improve rate limits.
43+
const apiKey = randBoolFromStr(fingerprintId)
44+
? env.ANTHROPIC_API_KEY
45+
: env.ANTHROPIC_API_KEY2
4346

4447
if (!apiKey) {
4548
throw new Error('Missing ANTHROPIC_API_KEY')

common/src/util/string.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { sumBy } from 'lodash'
2+
13
export const truncateString = (str: string, maxLength: number) => {
24
if (str.length <= maxLength) {
35
return str
@@ -15,7 +17,8 @@ export const replaceNonStandardPlaceholderComments = (
1517
placeholder: '// ... existing code ...',
1618
},
1719
{
18-
regex: /\/\*\s*\.{3}\s*.*(?:rest|unchanged|keep|file).*(?:\s*\.{3})?\s*\*\//gi,
20+
regex:
21+
/\/\*\s*\.{3}\s*.*(?:rest|unchanged|keep|file).*(?:\s*\.{3})?\s*\*\//gi,
1922
placeholder: '/* ... existing code ... */',
2023
},
2124
// Python, Ruby, R comments
@@ -25,7 +28,8 @@ export const replaceNonStandardPlaceholderComments = (
2528
},
2629
// HTML-style comments
2730
{
28-
regex: /<!--\s*\.{3}\s*.*(?:rest|unchanged|keep|file).*(?:\s*\.{3})?\s*-->/gi,
31+
regex:
32+
/<!--\s*\.{3}\s*.*(?:rest|unchanged|keep|file).*(?:\s*\.{3})?\s*-->/gi,
2933
placeholder: '<!-- ... existing code ... -->',
3034
},
3135
// SQL, Haskell, Lua comments
@@ -48,3 +52,7 @@ export const replaceNonStandardPlaceholderComments = (
4852

4953
return updatedContent
5054
}
55+
56+
export const randBoolFromStr = (str: string) => {
57+
return sumBy(str.split(''), (char) => char.charCodeAt(0)) % 2 === 0
58+
}

0 commit comments

Comments
 (0)