import Anthropic from '@anthropic-ai/sdk'; /** * The client, or null when there is no key. * * Null rather than a throw, because an unconfigured environment is a working * one: submissions still arrive and wait undrafted. The worker treats null * exactly as it treats a failed call, which keeps one path rather than two. * * Constructed once and cached. The SDK holds a connection pool, and building * one per submission would be wasteful on a route a stranger can trigger. */ let cached: Anthropic | null = null; let resolved = false; export function getAnthropicClient(): Anthropic | null { if (resolved) return cached; const key = process.env.ANTHROPIC_API_KEY; cached = key !== undefined && key.trim() !== '' ? new Anthropic({ apiKey: key }) : null; resolved = true; return cached; } /** Exposed for tests, which need a fresh decision per case. */ export function resetAnthropicClient(): void { cached = null; resolved = false; }