claude-api
Anthropic Messages API (Claude API) for integrations, streaming, prompt caching, tool use, vision. Use for chatbots, assistants, or encountering rate limits, 429 errors.
git clone --depth 1 https://github.com/secondsky/claude-skills /tmp/claude-api && cp -r /tmp/claude-api/plugins/claude-api/skills/claude-api ~/.claude/skills/claude-apiSKILL.md
# Claude API (Anthropic Messages API)
**Status**: Production Ready | **SDK**: @anthropic-ai/sdk@0.70.1
---
## Quick Start (5 Minutes)
### Node.js
```typescript
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
const message = await client.messages.create({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages: [
{ role: 'user', content: 'Hello, Claude!' },
],
});
console.log(message.content[0].text);
```
### Cloudflare Workers
```typescript
const response = await fetch('https://api.anthropic.com/v1/messages', {
method: 'POST',
headers: {
'x-api-key': env.ANTHROPIC_API_KEY,
'anthropic-version': '2023-06-01',
'content-type': 'application/json',
},
body: JSON.stringify({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello!' }],
}),
});
const data = await response.json();
console.log(data.content[0].text);
```
**Load `references/setup-guide.md` for complete setup with streaming, caching, and tools.**
---
## Critical Rules
### Always Do ✅
1. **Use environment variables** for API keys (NEVER hardcode)
2. **Set max_tokens** explicitly (required parameter)
3. **Pin model version** (`claude-sonnet-4-5-20250929`, not `claude-3-5-sonnet-latest`)
4. **Enable prompt caching** for repeated content (90% cost savings)
5. **Stream long responses** (`stream: true`) for better UX
6. **Handle errors** - Implement retry logic for 429, 529 errors
7. **Validate inputs** - Sanitize user messages before sending
8. **Monitor costs** - Track token usage
9. **Set timeouts** - Prevent hanging requests
10. **Use tool use properly** - Return tool_result in follow-up message
### Never Do ❌
1. **Never expose API key** in client-side code
2. **Never skip max_tokens** - API will error without it
3. **Never ignore stop_reason** - Check for `tool_use`, `end_turn`, `max_tokens`
4. **Never assume single content block** - `content` is an array
5. **Never use outdated models** - Pin to specific version
6. **Never skip error handling** - API calls can fail
7. **Never mix message roles** - Alternate user/assistant correctly
8. **Never ignore rate limits** - Implement exponential backoff
9. **Never store API keys** in logs or databases
10. **Never skip input validation** - Prevent injection attacks
---
## Top 3 Errors (Prevent 80% of Issues)
### Error #1: Rate Limit 429
**Symptom**: `429 Too Many Requests: Number of request tokens has exceeded your per-minute rate limit`
**Solution**: Implement exponential backoff with retry-after header
```typescript
async function handleRateLimit(requestFn, maxRetries = 3) {
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
return await requestFn();
} catch (error) {
if (error.status === 429) {
const retryAfter = error.response?.headers?.['retry-after'];
const delay = retryAfter ? parseInt(retryAfter) * 1000 : 1000 * Math.pow(2, attempt);
await new Promise(resolve => setTimeout(resolve, delay));
} else {
throw error;
}
}
}
}
```
**Prevention**: Monitor rate limit headers, upgrade tier, implement backoff
---
### Error #2: Prompt Caching Not Activating
**Symptom**: High costs despite `cache_control` blocks, `cache_read_input_tokens: 0`
**Solution**: Place `cache_control` on LAST block with >= 1024 tokens
```typescript
// ❌ Wrong - cache_control not at end
{
type: 'text',
text: DOCUMENT,
cache_control: { type: 'ephemeral' }, // Wrong position
},
{
type: 'text',
text: 'Additional text',
}
// ✅ Correct - cache_control at end
{
type: 'text',
text: DOCUMENT + '\n\nAdditional text',
cache_control: { type: 'ephemeral' }, // Correct position
}
```
**Prevention**: Ensure content >= 1024 tokens, keep cached content identical, monitor usage
**Load `references/prompt-caching-guide.md` for complete caching strategy.**
---
### Error #3: Tool Use Response Format Errors
**Symptom**: `invalid_request_error: tools[0].input_schema is invalid`
**Solution**: Valid tool schema with proper JSON Schema
```typescript
// ✅ Valid tool schema
{
name: 'get_weather',
description: 'Get current weather',
input_schema: {
type: 'object', // Must be 'object'
properties: {
location: {
type: 'string', // Valid JSON Schema types
description: 'City' // Optional but recommended
}
},
required: ['location'] // List required fields
}
}
// ✅ Valid tool result
{
type: 'tool_result',
tool_use_id: block.id, // Must match tool_use id
content: JSON.stringify(result) // Convert to string
}
```
**Prevention**: Validate schemas, match tool_use_id exactly, stringify results
**Load `references/tool-use-patterns.md` + `references/top-errors.md` for all 12 errors.**
---
## Common Use Cases (Quick Patterns)
### Streaming Responses
```typescript
const stream = await client.messages.stream({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Write a story.' }],
});
for await (const event of stream) {
if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
process.stdout.write(event.delta.text);
}
}
```
**Load**: `templates/streaming-chat.ts`
---
### Prompt Caching (90% Cost Savings)
```typescript
const message = await client.messages.create({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
system: [
{
type: 'text',
text: 'Long system prompt...',
cache_control: { type: 'ephemeral' },
},
],
messages: [{ role: 'user', content: 'Question?' }],
});
```
**Cache lasts 5 minutes, 90% savings on cached tokens**
**Load**: `references/prompt-caching-guide.md` + `templates/prompt-caching.ts`
---
### Tool Use (Function Calling)
```typescript
const message = await client.messages.create({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1Role-based access control (RBAC) with permissions and policies. Use for admin dashboards, enterprise access, multi-tenant apps, fine-grained authorization, or encountering permission hierarchies, role inheritance, policy conflicts.
100+ animated React components (Aceternity UI) for Next.js with Tailwind. Use for hero sections, parallax, 3D effects, or encountering animation, shadcn CLI integration errors.
shadcn/ui AI chat components for conversational interfaces. Use for streaming chat, tool/function displays, reasoning visualization, or encountering Next.js App Router setup, Tailwind v4 integration, AI SDK v5 migration errors.
Vercel AI SDK v5 for backend AI (text generation, structured output, tools, agents). Multi-provider. Use for server-side AI or encountering AI_APICallError, AI_NoObjectGeneratedError, streaming failures.
Vercel AI SDK v5 React hooks (useChat, useCompletion, useObject) for AI chat interfaces. Use for React/Next.js AI apps or encountering parse stream errors, no response, streaming issues.
Secure API authentication with JWT, OAuth 2.0, API keys. Use for authentication systems, third-party integrations, service-to-service communication, or encountering token management, security headers, auth flow errors.
Creates comprehensive API changelogs documenting breaking changes, deprecations, and migration strategies for API consumers. Use when managing API versions, communicating breaking changes, or creating upgrade guides.
Verifies API contracts between services using consumer-driven contracts, schema validation, and tools like Pact. Use when testing microservices communication, preventing breaking changes, or validating OpenAPI specifications.