|
| 1 | +// OpenRouter provider preferences per model family, and the Claude BYOK guard. |
| 2 | +// |
| 3 | +// Claude must run only on DebateLab's own Anthropic key, which is set up as |
| 4 | +// BYOK in OpenRouter. `only: ["anthropic"]` keeps Bedrock and Vertex out, and |
| 5 | +// `allow_fallbacks: false` stops OpenRouter from trying any other provider. |
| 6 | +// OpenRouter tries a BYOK key before its own capacity for the same provider, |
| 7 | +// but only the key's "Never use shared capacity" setting in OpenRouter stops it |
| 8 | +// from spending OpenRouter credits on Anthropic when the key fails. The usage |
| 9 | +// warning below is how a leak would show up. |
| 10 | + |
| 11 | +export function isClaudeModel(model: string): boolean { |
| 12 | + return model.startsWith("anthropic/"); |
| 13 | +} |
| 14 | + |
| 15 | +export function openRouterProviderOptions(model: string): Record<string, unknown> { |
| 16 | + if (isClaudeModel(model)) return { only: ["anthropic"], allow_fallbacks: false, require_parameters: true }; |
| 17 | + // Omitting sort and order keeps OpenRouter's balanced provider load balancing. |
| 18 | + return { require_parameters: true }; |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * A Claude call OpenRouter refused or failed. With fallbacks off this usually |
| 23 | + * means the Anthropic key is out of credit or erroring, so the request is not |
| 24 | + * retried elsewhere and the student sees a provider outage. The reserved daily |
| 25 | + * request is given back only when the failure came before any output. |
| 26 | + */ |
| 27 | +export class ClaudeProviderUnavailableError extends Error { |
| 28 | + readonly status: number | null; |
| 29 | + /** The model had already written text when the failure arrived. */ |
| 30 | + readonly afterOutput: boolean; |
| 31 | + |
| 32 | + constructor(status: number | null, afterOutput = false) { |
| 33 | + super("AI provider request failed"); |
| 34 | + this.name = "ClaudeProviderUnavailableError"; |
| 35 | + this.status = status; |
| 36 | + this.afterOutput = afterOutput; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** Throws the Claude-specific error for Claude models, the generic one otherwise. */ |
| 41 | +export function providerRequestError(model: string, status: number | null, afterOutput = false): Error { |
| 42 | + return isClaudeModel(model) ? new ClaudeProviderUnavailableError(status, afterOutput) : new Error("AI provider request failed"); |
| 43 | +} |
| 44 | + |
| 45 | +export const CLAUDE_UNAVAILABLE_CODE = "provider_unavailable"; |
| 46 | +export const CLAUDE_UNAVAILABLE_STATUS = 503; |
| 47 | +// The client maps a provider error naming Anthropic and "temporarily |
| 48 | +// unavailable" to its translated outage message. |
| 49 | +export const CLAUDE_UNAVAILABLE_MESSAGE = "Anthropic Claude is temporarily unavailable. Please try again later."; |
| 50 | + |
| 51 | +/** |
| 52 | + * Warns when a Claude response did not run on the BYOK key (`is_byok` false) |
| 53 | + * or OpenRouter charged for it (`cost` above zero). Past the monthly BYOK |
| 54 | + * allowance, OpenRouter's BYOK fee also shows up as cost. |
| 55 | + */ |
| 56 | +export function warnIfClaudeNotByok(model: string, rawUsage: Record<string, unknown> | undefined, source: string): boolean { |
| 57 | + if (!isClaudeModel(model) || !rawUsage) return false; |
| 58 | + const cost = typeof rawUsage.cost === "number" ? rawUsage.cost : undefined; |
| 59 | + if (rawUsage.is_byok !== false && !(cost !== undefined && cost > 0)) return false; |
| 60 | + const costDetails = rawUsage.cost_details && typeof rawUsage.cost_details === "object" |
| 61 | + ? rawUsage.cost_details as Record<string, unknown> |
| 62 | + : undefined; |
| 63 | + console.warn(JSON.stringify({ |
| 64 | + event: "claude_not_byok", |
| 65 | + source, |
| 66 | + model, |
| 67 | + is_byok: rawUsage.is_byok ?? null, |
| 68 | + cost: cost ?? null, |
| 69 | + upstream_inference_cost: costDetails?.upstream_inference_cost ?? null, |
| 70 | + })); |
| 71 | + return true; |
| 72 | +} |
0 commit comments