Repository Public
1/**2 * The only place the Anthropic API is called. Server and worker only: the key comes from the environment.3 * Every call is streamed (long outputs), metered into generation_usage, checked against budgets before it4 * starts, cancellable, and retried by the SDK with exponential backoff on 429 / 5xx / connection errors.5 */6import Anthropic from "@anthropic-ai/sdk";7import { sql } from "@/lib/db";8import { env } from "@/lib/env";9import { log } from "@/lib/log";10import { getSettings, type Settings } from "@/server/settings";11import { costUsd, priceKnown } from "./pricing";1213export class AiError extends Error {14 constructor(public code: "not_configured" | "budget" | "refusal" | "truncated" | "auth" | "rate_limited" | "bad_request" | "upstream" | "cancelled" | "invalid_output" | "timeout", message: string, public retryable = false) { super(message); }15}1617let cached: Anthropic | null = null;18export function anthropic(): Anthropic {19 if (!env.anthropicApiKey) throw new AiError("not_configured", "ANTHROPIC_API_KEY is not set on the server");20 return (cached ??= new Anthropic({ apiKey: env.anthropicApiKey, maxRetries: 4, timeout: 20 * 60_000 }));21}2223/** Translates SDK errors into the studio's own error codes (typed classes, never message matching). */24export function mapAiError(e: unknown, aborted = false): AiError | unknown {25 if (e instanceof AiError) return e;26 if (e instanceof Anthropic.APIUserAbortError || aborted) return new AiError("cancelled", "Cancelled");27 if (e instanceof Anthropic.AuthenticationError || e instanceof Anthropic.PermissionDeniedError) return new AiError("auth", "The Anthropic API key was refused");28 if (e instanceof Anthropic.RateLimitError) return new AiError("rate_limited", "The Anthropic API rate limit was reached", true);29 if (e instanceof Anthropic.BadRequestError) return new AiError("bad_request", `The Anthropic API refused the request: ${e.message.slice(0, 300)}`);30 if (e instanceof Anthropic.APIConnectionTimeoutError) return new AiError("timeout", "The Anthropic API timed out", true);31 if (e instanceof Anthropic.APIConnectionError) return new AiError("upstream", "The Anthropic API could not be reached", true);32 if (e instanceof Anthropic.APIError) return new AiError("upstream", `Anthropic API error ${e.status ?? ""}`.trim(), (e.status ?? 500) >= 500);33 return e;34}3536// ── Models ──────────────────────────────────────────────────────────────────────────────────────────3738export interface ModelInfo { id: string; name: string; maxOutputTokens: number | null; contextTokens: number | null; priceKnown: boolean }39let modelCache: { at: number; models: ModelInfo[] } | null = null;4041/** Models this key can actually call, from the Models API (cached 10 minutes). */42export async function listModels(force = false): Promise<ModelInfo[]> {43 if (!force && modelCache && Date.now() - modelCache.at < 600_000) return modelCache.models;44 const models: ModelInfo[] = [];45 try {46 for await (const m of anthropic().models.list()) {47 const x = m as unknown as { max_tokens?: number; max_input_tokens?: number };48 models.push({ id: m.id, name: m.display_name, maxOutputTokens: x.max_tokens ?? null, contextTokens: x.max_input_tokens ?? null, priceKnown: priceKnown(m.id) });49 }50 } catch (e) { throw mapAiError(e); }51 modelCache = { at: Date.now(), models };52 return models;53}5455/** The model in use: the administrator's choice if it still exists, otherwise the environment default. */56export async function resolveModel(settings?: Settings): Promise<string> {57 const s = settings ?? (await getSettings());58 const wanted = s.aiModel || env.anthropicDefaultModel;59 const models = await listModels();60 if (models.some((m) => m.id === wanted)) return wanted;61 if (models.some((m) => m.id === env.anthropicDefaultModel)) return env.anthropicDefaultModel;62 throw new AiError("not_configured", `Model ${wanted} is not available to this API key`);63}6465/** Models for which the API offers a server-side fallback when a safety classifier declines a request. */66const SERVER_FALLBACK = /^claude-(fable-5-[12]|opus-5(-5)?|sonnet-5-5)$/;6768// ── Budgets ─────────────────────────────────────────────────────────────────────────────────────────6970export async function spend(): Promise<{ today: number; month: number }> {71 const [r] = await sql<{ today: string; month: string }[]>`72 SELECT coalesce(sum(cost_usd) FILTER (WHERE at >= date_trunc('day', now())), 0) AS today,73 coalesce(sum(cost_usd) FILTER (WHERE at >= date_trunc('month', now())), 0) AS month FROM generation_usage`;74 return { today: Number(r.today), month: Number(r.month) };75}7677async function assertBudget(s: Settings, jobId: string | null): Promise<void> {78 const { today, month } = await spend();79 if (s.aiBudgetDailyUsd > 0 && today >= s.aiBudgetDailyUsd) throw new AiError("budget", `Daily AI budget reached (${today.toFixed(2)} / ${s.aiBudgetDailyUsd} USD)`);80 if (s.aiBudgetMonthlyUsd > 0 && month >= s.aiBudgetMonthlyUsd) throw new AiError("budget", `Monthly AI budget reached (${month.toFixed(2)} / ${s.aiBudgetMonthlyUsd} USD)`);81 if (jobId && s.aiBudgetPerJobUsd > 0) {82 const [j] = await sql<{ c: string }[]>`SELECT coalesce(sum(cost_usd), 0) AS c FROM generation_usage WHERE job_id = ${jobId}`;83 if (Number(j.c) >= s.aiBudgetPerJobUsd) throw new AiError("budget", `Per-job AI budget reached (${Number(j.c).toFixed(2)} / ${s.aiBudgetPerJobUsd} USD)`);84 }85}8687// ── One call ────────────────────────────────────────────────────────────────────────────────────────8889export interface CallInput {90 stage: string; jobId: string | null;91 /** Stable instructions: cached across the calls of a job (and across jobs for five minutes). */92 system: string;93 messages: Anthropic.Beta.BetaMessageParam[];94 effort: Settings["aiEffortGenerate"];95 maxTokens?: number;96 signal?: AbortSignal;97 /** Called with the number of characters received so far, at most a few times a second. */98 onProgress?: (chars: number) => void;99}100export interface CallResult { text: string; model: string; usage: { inputTokens: number; outputTokens: number; costUsd: number }; requestId: string | null }101102export async function callClaude(input: CallInput): Promise<CallResult> {103 const settings = await getSettings();104 await assertBudget(settings, input.jobId);105 const model = await resolveModel(settings);106 const started = Date.now();107 let chars = 0, lastTick = 0;108 try {109 const stream = anthropic().beta.messages.stream({110 model,111 max_tokens: Math.min(input.maxTokens ?? settings.aiMaxOutputTokens, settings.aiMaxOutputTokens),112 system: [{ type: "text", text: input.system, cache_control: { type: "ephemeral" } }],113 messages: input.messages,114 output_config: { effort: input.effort },115 ...(SERVER_FALLBACK.test(model) ? { betas: ["server-side-fallback-2026-07-01"], fallbacks: "default" as const } : {}),116 }, { signal: input.signal });117 stream.on("text", (delta) => {118 chars += delta.length;119 if (input.onProgress && Date.now() - lastTick > 400) { lastTick = Date.now(); input.onProgress(chars); }120 });121 const message = await stream.finalMessage();122 const u = message.usage;123 const cost = costUsd(message.model, u);124 await sql`INSERT INTO generation_usage (job_id, model, stage, input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, cost_usd, duration_ms, stop_reason, request_id)125 VALUES (${input.jobId}, ${message.model}, ${input.stage}, ${u.input_tokens}, ${u.output_tokens}, ${u.cache_read_input_tokens ?? 0}, ${u.cache_creation_input_tokens ?? 0}, ${cost}, ${Date.now() - started}, ${message.stop_reason}, ${stream.request_id ?? null})`;126 log("info", "ai.call", { stage: input.stage, jobId: input.jobId, model: message.model, inputTokens: u.input_tokens, outputTokens: u.output_tokens, cacheRead: u.cache_read_input_tokens ?? 0, costUsd: Number(cost.toFixed(5)), ms: Date.now() - started, stop: message.stop_reason });127 if (message.stop_reason === "refusal") throw new AiError("refusal", "The model declined this request" + (message.stop_details?.category ? ` (${message.stop_details.category})` : ""));128 const text = message.content.flatMap((b) => (b.type === "text" ? [b.text] : [])).join("");129 if (message.stop_reason === "max_tokens") throw new AiError("truncated", "The answer was cut off by the output limit; raise it in the settings or ask for a shorter sheet");130 return { text, model: message.model, usage: { inputTokens: u.input_tokens + (u.cache_read_input_tokens ?? 0) + (u.cache_creation_input_tokens ?? 0), outputTokens: u.output_tokens, costUsd: cost }, requestId: stream.request_id ?? null };131 } catch (e) {132 throw mapAiError(e, input.signal?.aborted);133 }134}135136/** Input size of a prospective call, for the cost estimate shown before a generation is launched. */137export async function countInputTokens(system: string, messages: Anthropic.MessageParam[]): Promise<number> {138 const model = await resolveModel();139 const r = await anthropic().messages.countTokens({ model, system, messages });140 return r.input_tokens;141}142143/** Health probe for the dashboard: key present, API reachable, configured model available. No tokens are spent. */144export async function aiHealth(): Promise<{ configured: boolean; ok: boolean; model: string | null; detail: string }> {145 if (!env.anthropicApiKey) return { configured: false, ok: false, model: null, detail: "ANTHROPIC_API_KEY is not set" };146 try { const model = await resolveModel(); return { configured: true, ok: true, model, detail: "ok" }; }147 catch (e) { return { configured: true, ok: false, model: null, detail: e instanceof AiError ? e.message : "unreachable" }; }148}149