Claude API Integration Skill
Defines a Claude API integration skill with TypeScript code patterns for chat, streaming, image analysis, and tool calling.
What this file does
Defines a Claude API integration skill with TypeScript code patterns for chat, streaming, image analysis, and tool calling.
When to use it
- Building a TypeScript service that calls Claude models
- Implementing streaming responses or vision capabilities
- Structuring prompts with XML tags for clarity
- Adding tool/function calling to a Claude-powered app
Assumes this stack
Claude API Integration Skill
You are a Claude API expert with deep knowledge of Anthropic's Claude models, prompt engineering, and AI safety best practices.
Core Capabilities
- Integrate Claude Sonnet, Opus, and Haiku models
- Implement extended context conversations (200K tokens)
- Use tool calling for function execution
- Implement vision capabilities with Claude
- Create effective prompts with XML tags
- Handle streaming responses
- Implement conversation memory management
- Apply AI safety and content filtering
- Optimize for cost and performance
- Build agentic workflows with Claude
Best Practices
- Use XML tags to structure prompts clearly
- Leverage Claude's long context window effectively
- Implement streaming for better UX
- Use Haiku for simple tasks, Sonnet for balance, Opus for complex reasoning
- Always include safety guidelines in system prompts
- Handle rate limits with exponential backoff
- Cache frequently used context with prompt caching
- Monitor token usage and costs
- Test prompts thoroughly before production
- Implement proper error handling
Code Patterns
Basic Claude Integration
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
export class ClaudeService {
async chat(messages: Array<{ role: string; content: string }>) {
const response = await anthropic.messages.create({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages,
});
return response.content[0].text;
}
async *streamChat(messages: Array<{ role: string; content: string }>) {
const stream = await anthropic.messages.stream({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages,
});
for await (const chunk of stream) {
if (chunk.type === 'content_block_delta' &&
chunk.delta.type === 'text_delta') {
yield chunk.delta.text;
}
}
}
async analyzeImage(imageData: string, prompt: string) {
const response = await anthropic.messages.create({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
messages: [
{
role: 'user',
content: [
{
type: 'image',
source: {
type: 'base64',
media_type: 'image/jpeg',
data: imageData,
},
},
{
type: 'text',
text: prompt,
},
],
},
],
});
return response.content[0].text;
}
async useTools(userMessage: string) {
const tools = [
{
name: 'get_weather',
description: 'Get the current weather for a location',
input_schema: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'The city and state, e.g. San Francisco, CA',
},
},
required: ['location'],
},
},
];
const response = await anthropic.messages.create({
model: 'claude-sonnet-4-5-20250929',
max_tokens: 1024,
tools,
messages: [{ role: 'user', content: userMessage }],
});
return response;
}
}
Structured Prompting with XML
function createStructuredPrompt(task: string, context: string, constraints: string[]) {
return `
<task>
${task}
</task>
<context>
${context}
</context>
<constraints>
${constraints.map(c => `<constraint>${c}</constraint>`).join('\n')}
</constraints>
Please analyze the task carefully and provide a detailed response.
`;
}
// Usage
const prompt = createStructuredPrompt(
'Analyze customer feedback',
'Customer reviews from Q4 2024',
[
'Focus on product quality issues',
'Identify top 3 concerns',
'Provide actionable recommendations'
]
);
Resources
What's inside
1 skill definition, 10 core capabilities, 10 best practices, 4 code examples, 3 resource links
Change this for your project
- Replace
'claude-sonnet-4-5-20250929'with your desired model ID - Replace
process.env.ANTHROPIC_API_KEYwith your own API key source
Where it goes
Save as SKILL.md inside a skill folder. Loaded when the agent selects that skill.
Worth borrowing
- Using XML tags inside prompt strings to separate task, context, and constraints
- Exposing a streaming generator function that yields text deltas
Related Documents
Video Script
Guides an AI to write ready-to-film two-column AV scripts with hooks, segment breakdowns, B-roll cues, text overlays, timing estimates, and shot lists.
Image Generation Skill
Provides Python functions for generating, editing, and transforming images via DALL-E 3, FLUX (Replicate/fal.ai), and local Stable Diffusion, plus utilities and prompt engineering helpers.
AI Artist — Engineering Specification
Defines a 21-section engineering specification for a prompt construction skill, covering contracts, deterministic design, error taxonomy, and production readiness.
Karis
Turns an AI agent into a marketing CMO with brand analysis, SEO audits, Reddit monitoring, and viral tweet optimization via CLI or API.