@anthropic-ai/sdk
| Property | Value |
|---|---|
| Package | @anthropic-ai/sdk |
| Versions Covered | >=0.18.0 <1.0.0 |
| Contract Version | 1.0.0 |
| Status | production |
| Last Verified | 2026-02-24 |
| Maintainer | corpus-team |
Installationâ
npm install @anthropic-ai/sdk
Covered Functionsâ
This contract covers 3 function(s):
create()â
Creates a message completion using Claude models
Import:
import { create } from '@anthropic-ai/sdk';
Postconditionsâ
What happens after calling this function:
đ´ ERROR - messages-create-no-try-catch
Condition: messages.create() called without try-catch or .catch() handler
Throws: APIError with error.status and error.message
Required Handling:
MUST wrap await client.messages.create() in try-catch block. Catch block should check error instanceof Anthropic.APIError and handle specific error types (RateLimitError, AuthenticationError, etc.) appropriately.
đ Source
â ī¸ WARNING - messages-create-generic-catch
Condition: messages.create() in try-catch but doesn't check error types
Required Handling:
SHOULD check error type using instanceof or error.status code. Handle RateLimitError with retry logic (using retry-after header), AuthenticationError by validating API key, and server errors (500/529) with appropriate backoff.
đ Source
stream()â
Creates a streaming message completion using Claude models
Import:
import { stream } from '@anthropic-ai/sdk';
Postconditionsâ
What happens after calling this function:
đ´ ERROR - messages-stream-no-try-catch
Condition: messages.stream() called without try-catch or .catch() handler
Throws: APIError, can occur mid-stream after initial 200 response
Required Handling:
MUST wrap await client.messages.stream() in try-catch block. Should also wrap stream iteration in try-catch to handle mid-stream errors. Check error types for proper recovery.
đ Source
âšī¸ INFO - stream-abort-not-handled
Condition: Stream created but no abort/cleanup handling
Required Handling:
SHOULD implement cancellation handling with stream.controller.abort() or break from stream iteration. Use finally blocks for cleanup.
đ Source
Anthropic()â
Initializes Anthropic API client
Import:
import { Anthropic } from '@anthropic-ai/sdk';
Postconditionsâ
What happens after calling this function:
â ī¸ WARNING - api-key-not-validated
Condition: Anthropic client created without validating API key exists
Required Handling:
SHOULD validate API key exists before client creation: if (!process.env.ANTHROPIC_API_KEY) throw new Error('Missing ANTHROPIC_API_KEY'). Consider using default apiKey validation from SDK.
đ Source
âšī¸ INFO - rate-limit-no-retry-logic
Condition: API calls made without retry logic for 429 errors
Required Handling:
SHOULD implement retry logic for 429 errors. Check retry-after header value and implement exponential backoff. Example: if (error instanceof RateLimitError) wait for error.headers['retry-after'] seconds before retry.
đ Source
Example: Proper Error Handlingâ
import sdk from '@anthropic-ai/sdk';
async function example() {
try {
const result = await create(/* args */);
// Handle success
return result;
} catch (error) {
// Handle error according to contract postconditions
console.error('Error:', error);
throw error;
}
}