Agent skill · Backend & API

api-ai-anthropic-sdk

Official Anthropic SDK patterns for TypeScript/Node.js — client setup, Messages API, streaming, tool use, vision, extended thinking, structured outputs, prompt caching, batch API, and production best practices

majiayu000github.com/majiayu000GitHub ↗
claude-codeMIT
Install
npx skills add majiayu000/claude-skill-registry --skill api-ai-anthropic-sdk --agent claude-code

Same command for any agent — swap --agent for codex, cursor, copilot.

Facts
Files in the skill folder: 2
SKILL.md size: 24 KB
Bundled scripts: none
Path: skills/ai-llm/api-ai-anthropic-sdk/SKILL.md
Open the folder on GitHub →
Where it comes from
Stars: 534
Language: HTML

Weekly change comes from our own snapshots, not the repository page — it measures attention, not adoption.

Review
written from the skill's own SKILL.md · Aug 5, 2026

What it does

Guides an AI agent to interact with Claude models using the official @anthropic-ai/sdk. It specifies how to initialize the client, how to call the Messages API for single-turn and multi-turn conversations, how to enable streaming, and how to handle tool use loops, vision blocks, extended thinking, and structured outputs. It also covers prompt caching, batch API usage, token counting, and production-ready error handling patterns.

How it works

  • Client Setup: Initialize the Anthropic client, with optional production config (timeouts, maxRetries) and environment-based API key management.
  • Messages API: Use client.messages.create() for interactions; always provide max_tokens; include system prompts and a list of user messages; iterate response.content blocks instead of assuming a single text block.
  • Streaming: Use .stream() for event-based streaming, or stream: true with SSE, and accumulate text via streaming helpers; obtain finalMessage() when complete.
  • Tool Use / Function Calling: Define tools with name, description, and input_schema; Claude may emit tool_use blocks; execute the tool and respond with tool_result blocks to continue.
  • Vision & Documents: Send images or PDFs as content blocks alongside text within messages.
  • Extended Thinking: Enable thinking blocks (adaptive or explicit budget tokens) and handle thinking blocks in the response content.
  • Structured Outputs: Use zodOutputFormat and messages.parse with output_config to enforce typed, structured results.
  • Prompt Caching: Use cache_control with ephemeral type to cache large prompts for cost savings; inspect usage tokens for cache read/write.
  • Batch API: Supports messages.batches.create for high-volume processing.
  • Token Counting: Access token counts via messages.countTokens.
  • Error Handling: Catch Anthropic.APIError and related subclasses; not bare catch blocks; rethrow unexpected errors.

When to use it

  • When building applications that call Claude models directly (Opus, Sonnet, Haiku families)
  • When you need streaming chat responses with event-based accumulation
  • When you require tool use / function calling with Claude decision points
  • When processing images, PDFs, or documents alongside prompts
  • When extended thinking is needed for complex reasoning
  • When structured data extraction with type validation is desired
  • When caching large system prompts can reduce costs
  • When performing high-volume batch processing
  • When estimating costs via token counting before requests

What it can touch

  • Uses the official SDK commands and concepts: client, client.messages.create, client.messages.stream, client.messages.parse, zodOutputFormat, tool_use blocks, tool_result blocks, thinking blocks, and content blocks (TextBlock, ToolUseBlock, ThinkingBlock).
  • Tools: tools array for tool definitions used in tool loops.
  • Environment: ANTHROPIC_API_KEY via process.env.

Caveats

  • All code must follow project conventions (kebab-case, named exports, import ordering, import type, named constants).
  • max_tokens is mandatory for both messages.create() and messages.stream(); there is no default.
  • stop_reason must be checked to handle tool_use and to continue the loop with tool_result.
  • Iterate over response.content blocks; responses may include text, tool_use, and thinking blocks.
  • Handle errors using Anthropic.APIError and its subclasses; avoid bare catch blocks.
  • Do not hardcode API keys; rely on ANTHROPIC_API_KEY from environment variables.
From the SKILL.md

# Anthropic SDK Patterns > **Quick Guide:** Use the official `@anthropic-ai/sdk` package to interact with Claude models directly. Use `client.messages.create()` for single-turn and multi-turn conversations. Use `client.messages.stream()` for streaming with event-based consumption. `max_tokens` is always required. Content blocks are typed unions (`text`, `tool_use`, `thinking`). Use `client.messages.parse()` with `zodOutputFormat()` for structured outputs. Tool use requires a tool-result loop -- Claude returns `tool_use` blocks, you execute the tool and send back `tool_result` blocks. Extended thinking adds `thinking` content blocks before the response. --- <critical_requirements> ## CRITICAL: Before Using This Skill > **All code must follow project conventions in CLAUDE.md** (kebab-case, named exports, import ordering, `import type`, named constants) **(You MUST always provide `max_tokens` in every `messages.create()` / `messages.stream()` call -- it is required and has no default)** **(You MUST handle the `stop_reason` field to detect `end_turn`, `max_tokens`, `tool_use`, and `stop_sequence` -- ignoring it causes silent truncation or broken tool loops)** **(You MUST iterate over `

What's inside
Steps it walks through
  1. CRITICAL: Before Using This Skill
  2. Examples Index
  3. Philosophy
  4. Core Patterns
  5. Pattern 1: Client Setup
  6. Pattern 2: Messages API
  7. Pattern 3: Streaming
  8. Pattern 4: Tool Use / Function Calling
  9. Pattern 5: Vision & Documents
  10. Pattern 6: Extended Thinking
  11. Pattern 7: Structured Outputs
  12. Pattern 8: Prompt Caching
  13. Pattern 9: Error Handling
  14. Performance Optimization
Ships with 1 file
  • metadata.json
More from claude-skill-registry
All skills →
About this skill
What does the api-ai-anthropic-sdk skill do?

Official Anthropic SDK patterns for TypeScript/Node.js — client setup, Messages API, streaming, tool use, vision, extended thinking, structured outputs, prompt caching, batch API, and production best practices

How do I install it?

Run `npx skills add majiayu000/claude-skill-registry --skill api-ai-anthropic-sdk --agent claude-code` — it drops the skill into your project so the agent can pick it up. Swap the --agent value for codex, cursor or copilot if you use one of those.

Where does this skill come from?

From majiayu000/claude-skill-registry, a repository with 534 stars. We read it straight from the repository tree rather than a submitted listing, so what you see here is what is actually published.

Is a popular skill a good skill?

Not necessarily. Stars measure attention, not adoption — a repository can trend for a week and be abandoned. That is why we show the weekly change from our own snapshots next to the total, instead of a single flattering number.

Keep going