mcpskills.net
SkillsMCPsAgentsPrompts
mcpskills.net — A curated directory of AI agent Skills and MCP servers
TermsPrivacy
← Back to Skills
Development

Claude API

Guide for using the Claude API effectively. Includes best practices for prompts, streaming, tool use, and building applications with Claude.

by AnthropicRepository →Source →

Build applications with the Claude API using best practices for prompts, streaming, and tool use.

Getting Started

Installation

npm install @anthropic-ai/sdk

Basic Usage

import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic();

const message = await client.messages.create({
  model: 'claude-sonnet-4-20250514',
  max_tokens: 1024,
  messages: [
    { role: 'user', content: 'Hello, Claude' }
  ],
});

Prompt Engineering

System Prompts

const message = await client.messages.create({
  model: 'claude-sonnet-4-20250514',
  max_tokens: 1024,
  system: 'You are a helpful assistant.',
  messages: [
    { role: 'user', content: 'What is the capital of France?' }
  ],
});

Structured Outputs

const message = await client.messages.create({
  model: 'claude-sonnet-4-20250514',
  max_tokens: 1024,
  messages: [
    {
      role: 'user',
      content: 'Extract the name and age from: John is 25 years old.'
    }
  ],
});

Streaming

const stream = client.messages.stream({
  model: 'claude-sonnet-4-20250514',
  max_tokens: 1024,
  messages: [
    { role: 'user', content: 'Write a story' }
  ],
});

for await (const event of stream) {
  if (event.type === 'content_block_delta') {
    process.stdout.write(event.delta.text);
  }
}

Tool Use

Defining Tools

const tools = [
  {
    name: 'get_weather',
    description: 'Get the weather for a location',
    input_schema: {
      type: 'object',
      properties: {
        location: {
          type: 'string',
          description: 'City name'
        }
      },
      required: ['location']
    }
  }
];

Handling Tool Calls

const response = await client.messages.create({
  model: 'claude-sonnet-4-20250514',
  max_tokens: 1024,
  tools,
  messages: [
    { role: 'user', content: 'What is the weather in Paris?' }
  ],
});

// Check for tool use
if (response.content[0].type === 'tool_use') {
  const toolCall = response.content[0];
  // Execute tool and return result
}

Best Practices

  • Use streaming for better UX
  • Implement proper error handling
  • Set appropriate max_tokens
  • Use system prompts for consistent behavior
  • Validate tool inputs with Zod
  • Handle rate limiting gracefully
  • Cache responses when possible