/** * Wrapper around the Anthropic SDK for extraction calls. * * Per ADR-02, this module is the ONLY place in the codebase permitted * to call the Anthropic API. Query-time code paths must not import * from `@anthropic-ai/sdk`. * * Responsibilities: * - Call Opus 4.7 with the pre-drafted extraction prompt from * `src/extraction/prompt.ts` (no extended thinking, per ADR-02 * and prompt.ts documentation) * - Classify errors into retry / fail-loud per the matrix in * CLAUDE.md's step 5 failure handling section * - Retry with exponential backoff on retryable errors * - Parse and validate the model's JSON response against * ExtractionResult */ import type Anthropic from "@anthropic-ai/sdk"; import { type ExtractionResult } from "./prompt.js"; import { type UsageInfo } from "./pricing.js"; export type RetryClassification = "retry" | "fail"; /** * Canonical reasons a ParseError can surface. Each reason is * deterministic per (LLM output, validation logic) — same input is * expected to produce the same failure on retry, so the classifier * routes ParseError to "fail" without retry per A1 v0.8 absorption. */ export type ParseErrorReason = "json-parse" | "shape-invalid" | "claims-not-array"; /** * Typed exception for LLM-output-shape failures distinct from * Anthropic-API failures. Per A1 v0.8 absorption + research/v0.5- * candidates.md #1: prior catch-all logging conflated parse-vs-API * failures during v0.4 Step 5 httpx 24-error investigation; cohort * users at v1.0 launch get substantively distinguishable error * messages enabling self-diagnosis (parse failure → recheck LLM * output shape; API failure → retry with backoff). */ export declare class ParseError extends Error { readonly reason: ParseErrorReason; readonly preview: string; constructor(reason: ParseErrorReason, preview: string, message: string); } /** * Classify an error as retryable or not. Exported for direct unit * testing — the retry-loop tests exercise the wrapper end-to-end with * stub clients, but this pure predicate carries the core logic. */ export declare function classifyError(err: unknown): RetryClassification; /** * Outcome of a single `extract()` call. * * - `result` — parsed claims, or `null` if the document was skippable * (max-tokens stop or malformed JSON). * - `usage` — token counts from the final successful API response. * Always present when `extract` resolves (even with `result: null`, * the call consumed tokens). Retries that threw before we saw a * response are NOT reflected here — those are invisible to us. * Throwing paths (retry-exhausted, non-retryable errors) never * reach this return shape. */ export interface ExtractionCallResult { result: ExtractionResult | null; usage: UsageInfo; } export interface ExtractionClient { /** * Run the extraction prompt against a single document body. Returns * parsed and validated claims plus token usage. Throws on * irrecoverable failure. * * A `null` `result` signals the document was skippable (malformed * JSON or max-tokens stop) — the caller decides whether to log and * move on. `usage` is still populated in that case because the API * call did consume tokens. */ extract(documentBody: string): Promise; } export interface CreateExtractionClientOptions { anthropic: Anthropic; /** Max retry attempts for retryable errors. Default: 3. */ maxRetries?: number; /** Base backoff in ms. Doubles per attempt, capped at maxBackoffMs. Default: 1000. */ baseBackoffMs?: number; /** Upper bound per backoff step. Default: 30_000. */ maxBackoffMs?: number; /** For tests — inject a fake sleep. Default: real setTimeout. */ sleep?: (ms: number) => Promise; } export declare function createExtractionClient(options: CreateExtractionClientOptions): ExtractionClient; //# sourceMappingURL=anthropic-client.d.ts.map