xiaohongshu
xiaohongshu
claude-sdk-integration-patterns
Expert integration patterns for Claude API and TypeScript SDK covering Messages API, streaming responses, tool use, error handling, token optimization, and production-ready implementations for building AI-powered applications
Full skill instructions
Production-ready patterns for integrating Claude API and TypeScript SDK into applications. Master streaming responses, tool execution, error handling, and optimization strategies for AI-powered features.
Use this skill when you need to:
The Claude Messages API is the primary interface for conversational AI:
Key Components:
The TypeScript SDK provides:
# Install the SDK
npm install @anthropic-ai/sdk
# Or with yarn
yarn add @anthropic-ai/sdk
import Anthropic from '@anthropic-ai/sdk';
// Initialize client
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
const message = await anthropic.messages.create({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages: [
{ role: 'user', content: 'Hello, Claude!' }
],
});
console.log(message.content);
When to use:
const messages = [
{ role: 'user', content: 'What is TypeScript?' },
{ role: 'assistant', content: 'TypeScript is a typed superset of JavaScript...' },
{ role: 'user', content: 'Give me an example' },
];
const response = await anthropic.messages.create({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages,
});
When to use:
const response = await anthropic.messages.create({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
system: 'You are a helpful Python programming assistant. Provide concise, tested code examples.',
messages: [
{ role: 'user', content: 'How do I read a CSV file?' }
],
});
When to use:
const stream = await anthropic.messages.create({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Write a story' }],
stream: true,
});
for await (const event of stream) {
if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
process.stdout.write(event.delta.text);
}
}
When to use:
const stream = anthropic.messages.stream({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Explain quantum computing' }],
})
.on('text', (text) => {
console.log(text);
})
.on('message', (message) => {
console.log('Complete message:', message);
})
.on('error', (error) => {
console.error('Stream error:', error);
});
const finalMessage = await stream.finalMessage();
When to use:
const stream = anthropic.messages.stream({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Long task...' }],
});
// Abort after timeout
setTimeout(() => stream.abort(), 5000);
try {
await stream.done();
} catch (error) {
if (error instanceof Anthropic.APIUserAbortError) {
console.log('Stream aborted by user');
}
}
When to use:
import { betaZodTool } from '@anthropic-ai/sdk/helpers/zod';
import { z } from 'zod';
const weatherTool = betaZodTool({
name: 'get_weather',
inputSchema: z.object({
location: z.string(),
unit: z.enum(['celsius', 'fahrenheit']).default('fahrenheit'),
}),
description: 'Get current weather for a location',
run: async (input) => {
// Call weather API
return `Weather in ${input.location}: 72°F, sunny`;
},
});
When to use:
const finalMessage = await anthropic.beta.messages.toolRunner({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1000,
messages: [
{ role: 'user', content: 'What\'s the weather in San Francisco?' }
],
tools: [weatherTool],
});
console.log(finalMessage.content);
When to use:
const runner = anthropic.beta.messages.toolRunner({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1000,
messages: [{ role: 'user', content: 'Check weather and calculate travel time' }],
tools: [weatherTool, travelTimeTool],
stream: true,
});
for await (const messageStream of runner) {
for await (const event of messageStream) {
console.log('Event:', event);
}
console.log('Message:', await messageStream.finalMessage());
}
When to use:
async function createMessage(prompt: string) {
try {
const message = await anthropic.messages.create({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages: [{ role: 'user', content: prompt }],
});
return message;
} catch (error) {
if (error instanceof Anthropic.APIError) {
console.error('API Error:', {
status: error.status,
name: error.name,
message: error.message,
headers: error.headers,
});
// Handle specific errors
if (error.status === 429) {
// Rate limit - implement backoff
console.log('Rate limited, waiting...');
await new Promise(resolve => setTimeout(resolve, 60000));
return createMessage(prompt); // Retry
} else if (error.status === 401) {
throw new Error('Invalid API key');
} else if (error.status === 400) {
throw new Error(`Bad request: ${error.message}`);
}
}
throw error;
}
}
When to use:
async function createWithRetry(
params: Anthropic.MessageCreateParams,
maxRetries = 3,
baseDelay = 1000
): Promise<Anthropic.Message> {
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
return await anthropic.messages.create(params);
} catch (error) {
if (error instanceof Anthropic.APIError && error.status === 429) {
// Rate limit - exponential backoff
const delay = baseDelay * Math.pow(2, attempt);
console.log(`Retry attempt ${attempt + 1} after ${delay}ms`);
await new Promise(resolve => setTimeout(resolve, delay));
} else {
throw error;
}
}
}
throw new Error('Max retries exceeded');
}
When to use:
import { encode } from 'gpt-tokenizer';
function estimateTokens(text: string): number {
// Approximate token count (Claude uses similar tokenization to GPT)
return encode(text).length;
}
function optimizePrompt(userMessage: string, maxTokens: number = 1024): string {
const tokens = estimateTokens(userMessage);
if (tokens > maxTokens) {
// Truncate or summarize
return userMessage.slice(0, maxTokens * 4); // Rough char estimate
}
return userMessage;
}
When to use:
interface ConversationMessage {
role: 'user' | 'assistant';
content: string;
tokens?: number;
}
class ConversationManager {
private messages: ConversationMessage[] = [];
private maxContextTokens = 100000;
addMessage(role: 'user' | 'assistant', content: string) {
const tokens = estimateTokens(content);
this.messages.push({ role, content, tokens });
this.pruneIfNeeded();
}
private pruneIfNeeded() {
const totalTokens = this.messages.reduce((sum, msg) => sum + (msg.tokens || 0), 0);
if (totalTokens > this.maxContextTokens) {
// Remove oldest messages (keep system prompt if present)
this.messages = this.messages.slice(-10); // Keep last 10 messages
}
}
getMessages() {
return this.messages.map(({ role, content }) => ({ role, content }));
}
}
When to use:
const batchResult = await anthropic.messages.batches.create({
requests: [
{
custom_id: 'request-1',
params: {
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Summarize this article...' }],
},
},
{
custom_id: 'request-2',
params: {
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Translate this text...' }],
},
},
],
});
// Poll for results
const batch = await anthropic.messages.batches.retrieve(batchResult.id);
When to use:
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
timeout: 60000, // 60 seconds
maxRetries: 3,
});
When to use:
const message = await anthropic.messages.create(
{
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello' }],
},
{
headers: {
'anthropic-beta': 'max-tokens-3-5-sonnet-2024-07-15',
},
}
);
When to use:
Never hardcode API keys
// ❌ Bad
const anthropic = new Anthropic({ apiKey: 'sk-ant-...' });
// ✅ Good
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
Use environment variables
# .env file
ANTHROPIC_API_KEY=sk-ant-your-key-here
Choose appropriate model for task:
Use latest model versions:
model: 'claude-sonnet-4-5-20250929' // Latest
Always use streaming for:
Avoid streaming for:
Solution:
Solution:
Solution:
sk-ant-)Solution:
Skill Version: 1.0.0 Last Updated: October 2025 Compatibility: @anthropic-ai/sdk 0.30.0+ Research Source: Context7 - Anthropic TypeScript SDK (106 code snippets, Trust Score 8.8)
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience