Skip to content

Commit

Permalink
feat: add additional fallback for anthropic (PostHog#28179)
Browse files Browse the repository at this point in the history
  • Loading branch information
k11kirky authored Feb 1, 2025
1 parent 23c8f8a commit 8224c5e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
32 changes: 32 additions & 0 deletions plugin-server/src/utils/ai-costs/anthropic_overrides.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { ModelRow } from './types'

export const costs: ModelRow[] = [
{
model: 'claude-3-5-haiku',
cost: {
prompt_token: 8e-7,
completion_token: 0.000004,
},
},
{
model: 'claude-3-5-sonnet',
cost: {
prompt_token: 0.000003,
completion_token: 0.000015,
},
},
{
model: 'claude-3-opus',
cost: {
prompt_token: 0.000015,
completion_token: 0.000075,
},
},
{
model: 'claude-2',
cost: {
prompt_token: 0.000008,
completion_token: 0.000024,
},
},
]
2 changes: 2 additions & 0 deletions plugin-server/src/utils/ai-costs/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { costs as anthropicCosts } from './anthropic'
import { costs as anthropicOverrides } from './anthropic_overrides'
import { costs as cohereCosts } from './cohere'
import { costs as deepseekCosts } from './deepseek'
import { costs as googleCosts } from './google'
Expand All @@ -11,6 +12,7 @@ import type { ModelRow } from './types'
export const costs: ModelRow[] = [
...openaiCosts,
...anthropicCosts,
...anthropicOverrides,
...googleCosts,
...deepseekCosts,
...perplexityCosts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,18 @@ export const extractCoreModelParams = (event: PluginEvent): PluginEvent => {
}

const findCostFromModel = (aiModel: string): ModelRow | undefined => {
// Check if the model is an exact match
let cost = costs.find((cost) => cost.model.toLowerCase() === aiModel.toLowerCase())
// Check if the model is a variant of a known model
if (!cost) {
cost = costs.find((cost) => aiModel.toLowerCase().includes(cost.model.toLowerCase()))
}
// Check if the model is a variant of a known model
if (!cost) {
cost = costs.find((cost) => cost.model.toLowerCase().includes(aiModel.toLowerCase()))
}
if (!cost) {
console.warn(`No cost found for model: ${aiModel}`)
}
return cost
}

0 comments on commit 8224c5e

Please sign in to comment.