# Anthropic Sdk Guide

> >

- **Type:** Skill
- **Install:** `agentstack add skill-versoxbt-claude-initial-setup-anthropic-sdk-guide`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [VersoXBT](https://agentstack.voostack.com/s/versoxbt)
- **Installs:** 0
- **Category:** [AI & ML](https://agentstack.voostack.com/c/ai-and-ml)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [VersoXBT](https://github.com/VersoXBT)
- **Source:** https://github.com/VersoXBT/claude-initial-setup/tree/main/skills/claude-api/anthropic-sdk-guide
- **Website:** https://github.com/VersoXBT/claude-initial-setup#installation

## Install

```sh
agentstack add skill-versoxbt-claude-initial-setup-anthropic-sdk-guide
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# Anthropic SDK Guide

Comprehensive reference for building with the Anthropic Python and TypeScript SDKs.
Covers initialization, messages API, streaming, prompt caching, batches, and error handling.

## When to Use
- User is setting up Anthropic SDK (Python or TypeScript)
- User is calling the Messages API or needs streaming responses
- User wants prompt caching, batch processing, or token counting
- User is debugging API errors or rate limits
- User asks about Claude model IDs or API configuration

## Core Patterns

### SDK Installation and Setup

```bash
# Python
pip install anthropic

# TypeScript
npm install @anthropic-ai/sdk
```

```python
# Python - client initialization
import anthropic

client = anthropic.Anthropic()  # Uses ANTHROPIC_API_KEY env var
# Or explicit: anthropic.Anthropic(api_key="sk-ant-...")
```

```typescript
// TypeScript - client initialization
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic();  // Uses ANTHROPIC_API_KEY env var
```

### Messages API - Basic Usage

```python
# Python - simple message
message = client.messages.create(
    model="claude-sonnet-4-6-20250514",
    max_tokens=1024,
    system="You are a helpful coding assistant.",
    messages=[
        {"role": "user", "content": "Explain async/await in Python."}
    ]
)
print(message.content[0].text)
```

```typescript
// TypeScript - simple message
const message = await client.messages.create({
  model: "claude-sonnet-4-6-20250514",
  max_tokens: 1024,
  system: "You are a helpful coding assistant.",
  messages: [
    { role: "user", content: "Explain async/await in Python." }
  ],
});
console.log(message.content[0].text);
```

### Streaming Responses

```python
# Python - streaming with context manager
with client.messages.stream(
    model="claude-sonnet-4-6-20250514",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a short story."}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

# Access final message after stream completes
final_message = stream.get_final_message()
```

```typescript
// TypeScript - streaming
const stream = client.messages.stream({
  model: "claude-sonnet-4-6-20250514",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Write a short story." }],
});

stream.on("text", (text) => process.stdout.write(text));

const finalMessage = await stream.finalMessage();
```

### Prompt Caching

Use cache_control to cache large system prompts, tool definitions, or conversation prefixes.
Cached content costs 90% less on cache hits.

```python
# Python - prompt caching with large system prompt
message = client.messages.create(
    model="claude-sonnet-4-6-20250514",
    max_tokens=1024,
    system=[
        {
            "type": "text",
            "text": "You are an expert on this large codebase..." + large_context,
            "cache_control": {"type": "ephemeral"}
        }
    ],
    messages=[{"role": "user", "content": "What does the auth module do?"}]
)
# Check cache performance
print(f"Cache read: {message.usage.cache_read_input_tokens}")
print(f"Cache creation: {message.usage.cache_creation_input_tokens}")
```

### Batch Processing

Process up to 10,000 requests at 50% cost with 24-hour turnaround.

```python
# Python - create a batch
batch = client.messages.batches.create(
    requests=[
        {
            "custom_id": f"request-{i}",
            "params": {
                "model": "claude-sonnet-4-6-20250514",
                "max_tokens": 1024,
                "messages": [{"role": "user", "content": prompt}]
            }
        }
        for i, prompt in enumerate(prompts)
    ]
)

# Poll for completion
import time
while True:
    status = client.messages.batches.retrieve(batch.id)
    if status.processing_status == "ended":
        break
    time.sleep(60)

# Stream results
for result in client.messages.batches.results(batch.id):
    if result.result.type == "succeeded":
        print(result.custom_id, result.result.message.content[0].text)
```

### Token Counting

```python
# Python - count tokens before sending
count = client.messages.count_tokens(
    model="claude-sonnet-4-6-20250514",
    system="You are a helpful assistant.",
    messages=[{"role": "user", "content": long_document}]
)
print(f"Input tokens: {count.input_tokens}")
```

### Error Handling

```python
import anthropic

try:
    message = client.messages.create(
        model="claude-sonnet-4-6-20250514",
        max_tokens=1024,
        messages=[{"role": "user", "content": "Hello"}]
    )
except anthropic.RateLimitError:
    # Back off and retry - SDK has built-in retries (2 by default)
    print("Rate limited. The SDK will auto-retry.")
except anthropic.APIStatusError as e:
    print(f"API error {e.status_code}: {e.message}")
except anthropic.APIConnectionError:
    print("Network connection failed.")
```

## Anti-Patterns
- Hardcoding API keys instead of using environment variables
- Not setting max_tokens (it is required, not optional)
- Ignoring the stop_reason field (could be "end_turn", "max_tokens", or "tool_use")
- Polling batch status too frequently (use 60-second intervals minimum)
- Not using streaming for user-facing applications (causes perceived latency)
- Setting temperature > 0 for deterministic tasks like classification or extraction

## Quick Reference

| Model ID | Best For |
|-----------|----------|
| claude-opus-4-6-20250514 | Deep reasoning, complex tasks |
| claude-sonnet-4-6-20250514 | Best balance of speed and capability |
| claude-haiku-4-5-20251001 | Fast, lightweight tasks |

| Feature | Endpoint / Method |
|---------|-------------------|
| Messages | `client.messages.create()` |
| Streaming | `client.messages.stream()` |
| Batches | `client.messages.batches.create()` |
| Token count | `client.messages.count_tokens()` |
| Prompt caching | `cache_control: {"type": "ephemeral"}` |

Default retries: 2 (configurable via `max_retries` on client).

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [VersoXBT](https://github.com/VersoXBT)
- **Source:** [VersoXBT/claude-initial-setup](https://github.com/VersoXBT/claude-initial-setup)
- **License:** MIT
- **Homepage:** https://github.com/VersoXBT/claude-initial-setup#installation

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-versoxbt-claude-initial-setup-anthropic-sdk-guide
- Seller: https://agentstack.voostack.com/s/versoxbt
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
