← All @molecule/* packages · App templates
@molecule/api-ai-anthropicProvider bond · ai · API (Node) · v1.1.0 · Apache-2.0
Anthropic Claude AI provider for molecule.dev
npm install @molecule/api-ai-anthropicnpm · Source on GitHub · Implements @molecule/api-ai
@molecule/api-ai-anthropic is a provider bond on the API (Node) side: it implements the ai core interface (@molecule/api-ai) with a concrete vendor or library behind it.
Your code calls the core; you wire this provider once at startup. Swapping vendors later is one line in that wiring, not a rewrite.
Works with: @molecule/api-bond, @molecule/api-i18n, @molecule/api-secrets
Secrets: ANTHROPIC_API_KEY
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Anthropic ai-anthropic provider for molecule.dev.
provider
npm install @molecule/api-ai-anthropic @molecule/api-ai @molecule/api-bond @molecule/api-i18n @molecule/api-secrets
AnthropicConfigConfiguration for anthropic.
interface AnthropicConfig {
/** API key. Defaults to ANTHROPIC_API_KEY env var. */
apiKey?: string
/** Default model. Defaults to 'claude-opus-5'. */
defaultModel?: string
/** Maximum tokens for completions. */
maxTokens?: number
/** Base URL override (for proxies). */
baseUrl?: string
/** Called on each rate-limited/overloaded upstream response, before any retry sleep. */
onRateLimit?: AiRateLimitCallback
}
ProcessEnvProcess Env interface.
interface ProcessEnv {
ANTHROPIC_API_KEY: string
/** Base URL override (for credential brokers / gateways). */
ANTHROPIC_BASE_URL?: string
}
createProvider(config)Creates an Anthropic Claude AI provider instance.
function createProvider(config?: AnthropicConfig): AIProvider
config — Anthropic-specific configuration (API key, model, max tokens, base URL).Returns: An AIProvider backed by the Anthropic Messages API.
aiAnthropicSecretDefinitionsSecret definitions required by the Anthropic AI bond.
const aiAnthropicSecretDefinitions: SecretDefinition[]
providerThe provider implementation.
const provider: AIProvider
Implements @molecule/api-ai interface.
Setup function to register this provider with the bond system:
import { bond } from '@molecule/api-bond'
import { provider } from '@molecule/api-ai-anthropic'
export function setupAiAnthropic(): void {
bond('ai', 'anthropic', provider)
}
Peer dependencies:
@molecule/api-ai ^1.0.1@molecule/api-bond ^1.0.1@molecule/api-i18n ^1.0.1@molecule/api-secrets ^1.0.1ANTHROPIC_API_KEY (required) — Anthropic API key
sk-ant-api03-...@molecule/api-ai@molecule/api-bond@molecule/api-i18n@molecule/api-secretsBond this as an AI provider (see @molecule/api-ai for the chat() streaming loop and the
key-server-side / never-blindly-trust-model-output rules). Config: ANTHROPIC_API_KEY
(SERVER-side only — never shipped to the browser) plus an optional default model id. Drive it
through the core chat() / requireProvider(), NOT the Anthropic SDK directly, so the app
stays provider-agnostic and can swap models/providers by changing only the bond.
Missing ANTHROPIC_API_KEY fails fast: the provider throws naming the exact env var on
first use (the exported provider is a lazy proxy, so this fires on the first chat() call,
not at bond/module-load time) — it never silently sends an empty key and surfaces only as a
generic 401 later.
Error message disambiguation: a non-OK response yields a sanitized error ChatEvent
(never a throw). A permanently-invalid request — a plain 400 that ISN'T a context-length
error (bad param, malformed tool schema) — gets its own non-retryable message ("AI request
was invalid — check the model and request parameters.") distinct from the generic "AI service
error. Please try again." used for retryable failures; don't retry a plain 400 as if it might
succeed on a second attempt.
Integration checklist — drive the real UI (live preview, no mocks), adapt each item to this app's actual chat/AI screens, and check every box off one by one. A box you can't check is an integration bug to fix — not a skip. The sandbox HAS an AI provider bonded, so the flow runs live end-to-end; AI output is NON-DETERMINISTIC, so assert on STRUCTURE/behavior, not exact text:
chat() yields text chunks then a final done; a single late
blob means the reply was awaited whole and streaming is broken.)messages history is sent, not just the last
line.Translation strings are provided by @molecule/api-locales-ai-anthropic.