Token导航 LogoToken导航TokenDH.com
AI 工具敏感数据github未标认证来源可访问clear审计通过

claude-sdk-integration-patternsClaude SDK 集成模式

Agent Skill

claude-sdk-integration-patterns 用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要围绕仓库状态、代码变更或协作事项进行整理时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

1,972

周安装

79

GitHub Stars

53

下载量

638
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

3

许可证

MIT

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:claude-sdk-integration-patterns(Claude SDK 集成模式)
来源仓库:https://github.com/manutej/luxor-claude-marketplace
仓库路径:skills/claude-sdk-integration-patterns
安装命令:
npx skills add https://github.com/manutej/luxor-claude-marketplace --skill claude-sdk-integration-patterns
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。不同来源提供的安装方式可能略有差异;本站展示可直接复制的安装命令,安装前请核对来源页面。

skills.shnpx skills
npx skills add https://github.com/manutej/luxor-claude-marketplace --skill claude-sdk-integration-patterns

简介

Claude SDK 集成模式提供生产环境就绪的 Node.js/TypeScript 应用接入方案。

  • 适用于需要流式对话、工具调用、错误重试和成本优化的 AI 功能开发场景。
  • 通过标准命令安装并遵循文档配置,支持 Claude API 与本地应用的深度集成。
  • 需确认项目技术栈匹配性,避免在非 TypeScript 环境中强制使用相关模式。
  • claude-sdk-integration-patterns 属于AI 工具类 Skill,可作为该场景下的辅助能力补充。

SKILL.md

Claude SDK Integration Patterns

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.

When to Use This Skill

Use this skill when you need to:

  • Integrate Claude API into Node.js/TypeScript applications
  • Implement streaming conversations with real-time responses
  • Build applications with Claude tool use (function calling)
  • Handle API errors gracefully with retry logic
  • Optimize token usage and manage costs
  • Deploy Claude-powered features to production
  • Build multi-turn conversations with context management
  • Implement message batching for high-volume processing

Core Concepts

Messages API Fundamentals

The Claude Messages API is the primary interface for conversational AI:

Key Components:

  • Model Selection: Choose appropriate model (Opus, Sonnet, Haiku)
  • Messages Array: Conversation history with user/assistant roles
  • Max Tokens: Control response length
  • System Prompts: Guide model behavior
  • Streaming: Real-time response generation

SDK Architecture

The TypeScript SDK provides:

  • Type-safe API client
  • Streaming helpers for real-time responses
  • Tool execution framework
  • Error handling utilities
  • Message batch processing
  • Event-driven architecture

Installation and Setup

# 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,
});

Messages API Patterns

Pattern 1: Basic Message Creation

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:

  • Simple question-answer interactions
  • One-off API calls
  • Synchronous workflows

Pattern 2: Multi-Turn Conversations

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:

  • Chatbots and conversational UIs
  • Multi-step workflows
  • Context-dependent interactions

Pattern 3: System Prompts

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:

  • Specialized assistants
  • Role-playing scenarios
  • Consistent behavior across conversations

Streaming Patterns

Pattern 4: Basic Streaming

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:

  • Real-time user interfaces
  • Long-form content generation
  • Interactive experiences

Pattern 5: Streaming with Event Handlers

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:

  • Real-time UIs (chatbots, live editors)
  • Progress indicators
  • Partial result processing

Pattern 6: Streaming with Abort Control

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:

  • User-cancellable operations
  • Timeout handling
  • Resource management

Tool Use Patterns

Pattern 7: Tool Definition with Zod

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:

  • Type-safe tool definitions
  • Input validation
  • Clear tool contracts

Pattern 8: Tool Runner for Automatic Execution

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:

  • Automated tool execution
  • AI agents with function calling
  • Complex multi-step workflows

Pattern 9: Streaming Tool Execution

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:

  • Real-time tool execution feedback
  • Multi-tool workflows
  • Interactive AI agents

Error Handling Patterns

Pattern 10: Comprehensive Error Handling

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:

  • Production applications
  • Robust error recovery
  • User-facing applications

Pattern 11: Exponential Backoff Retry

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:

  • High-volume applications
  • Rate limit handling
  • Network reliability

Token Optimization Patterns

Pattern 12: Token Counting

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:

  • Cost optimization
  • Token budget management
  • Large input handling

Pattern 13: Context Window Management

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:

  • Long conversations
  • Context management
  • Cost control

Message Batching Patterns

Pattern 14: Batch Processing

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:

  • Bulk processing
  • Offline workflows
  • Cost optimization (lower batch pricing)

Production Patterns

Pattern 15: Request Timeout Configuration

const anthropic = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
  timeout: 60000, // 60 seconds
  maxRetries: 3,
});

When to use:

  • Production deployments
  • Network reliability
  • Resource management

Pattern 16: Custom Headers and Metadata

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:

  • Beta features
  • Custom tracking
  • A/B testing

Best Practices

API Key Management

  1. Never hardcode API keys // ❌ Bad const anthropic = new Anthropic({apiKey: 'sk-ant-...'}); // ✅ Good const anthropic = new Anthropic({apiKey: process.env.ANTHROPIC_API_KEY});
  2. Use environment variables #.env file ANTHROPIC_API_KEY=sk-ant-your-key-here

Model Selection

  1. Choose appropriate model for task:

- Opus: Most capable, complex reasoning - Sonnet: Balanced performance/speed - Haiku: Fastest, simple tasks

  1. Use latest model versions: model: 'claude-sonnet-4-5-20250929' // Latest

Streaming Considerations

  1. Always use streaming for:

- User-facing applications - Long-form content (>500 tokens) - Real-time interactions

  1. Avoid streaming for:

- Backend batch processing - Deterministic outputs needed upfront - Simple API integrations

Error Handling

  1. Always catch API errors
  2. Implement retry logic for transient failures
  3. Log errors for debugging
  4. Provide user-friendly error messages

Token Management

  1. Estimate tokens before API calls
  2. Set appropriate max_tokens
  3. Monitor token usage
  4. Implement context pruning for long conversations

Troubleshooting

Issue: Rate Limit Errors (429)

Solution:

  • Implement exponential backoff
  • Use message batching for bulk operations
  • Upgrade to higher rate limit tier

Issue: Timeout Errors

Solution:

  • Use streaming for long responses
  • Increase timeout configuration
  • Reduce max_tokens

Issue: Invalid API Key (401)

Solution:

  • Verify environment variable is set
  • Check API key format (starts with sk-ant-)
  • Ensure key hasn't been rotated

Issue: Context Length Exceeded

Solution:

  • Implement conversation pruning
  • Summarize older messages
  • Use appropriate context window limits

Resources


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)

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

04

需要参考平台分布和安装热度时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

补充不同宿主或平台的使用分布数据

能力 5

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Claude Code

30.11%
按下载量换算192

windsurf

22.33%
按下载量换算142

Antigravity

16.25%
按下载量换算104

OpenCode

11.51%
按下载量换算73

Gemini CLI

7.71%
按下载量换算49

Codex

3.6%
按下载量换算23

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

敏感数据

该 Skill 可能接触密钥、Token、环境变量或敏感配置,应进入高风险复核队列,默认不自动发布。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。

来源信息

继续浏览同类 Skills