$npx -y skills add tranhieutt/software_development_department --skill claude-apiProvides code patterns for the Anthropic Claude API including streaming, tool use, and prompt caching. Use when working with Anthropic SDK files or when the user mentions Claude API, Anthropic client, or LLM integration.
| 1 | # Claude API |
| 2 | |
| 3 | Build applications with the Anthropic Claude API and SDKs. |
| 4 | |
| 5 | ## When to Activate |
| 6 | |
| 7 | - Building applications that call the Claude API |
| 8 | - Code imports `anthropic` (Python) or `@anthropic-ai/sdk` (TypeScript) |
| 9 | - User asks about Claude API patterns, tool use, streaming, or vision |
| 10 | - Implementing agent workflows with Claude Agent SDK |
| 11 | - Optimizing API costs, token usage, or latency |
| 12 | |
| 13 | ## Model Selection |
| 14 | |
| 15 | | Model | ID | Best For | |
| 16 | |-------|-----|----------| |
| 17 | | Opus 4.1 | `claude-opus-4-1` | Complex reasoning, architecture, research | |
| 18 | | Sonnet 4 | `claude-sonnet-4-0` | Balanced coding, most development tasks | |
| 19 | | Haiku 3.5 | `claude-3-5-haiku-latest` | Fast responses, high-volume, cost-sensitive | |
| 20 | |
| 21 | Default to Sonnet 4 unless the task requires deep reasoning (Opus) or speed/cost optimization (Haiku). For production, prefer pinned snapshot IDs over aliases. |
| 22 | |
| 23 | ## Python SDK |
| 24 | |
| 25 | ### Installation |
| 26 | |
| 27 | ```bash |
| 28 | pip install anthropic |
| 29 | ``` |
| 30 | |
| 31 | ### Basic Message |
| 32 | |
| 33 | ```python |
| 34 | import anthropic |
| 35 | |
| 36 | client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from env |
| 37 | |
| 38 | message = client.messages.create( |
| 39 | model="claude-sonnet-4-0", |
| 40 | max_tokens=1024, |
| 41 | messages=[ |
| 42 | {"role": "user", "content": "Explain async/await in Python"} |
| 43 | ] |
| 44 | ) |
| 45 | print(message.content[0].text) |
| 46 | ``` |
| 47 | |
| 48 | ### Streaming |
| 49 | |
| 50 | ```python |
| 51 | with client.messages.stream( |
| 52 | model="claude-sonnet-4-0", |
| 53 | max_tokens=1024, |
| 54 | messages=[{"role": "user", "content": "Write a haiku about coding"}] |
| 55 | ) as stream: |
| 56 | for text in stream.text_stream: |
| 57 | print(text, end="", flush=True) |
| 58 | ``` |
| 59 | |
| 60 | ### System Prompt |
| 61 | |
| 62 | ```python |
| 63 | message = client.messages.create( |
| 64 | model="claude-sonnet-4-0", |
| 65 | max_tokens=1024, |
| 66 | system="You are a senior Python developer. Be concise.", |
| 67 | messages=[{"role": "user", "content": "Review this function"}] |
| 68 | ) |
| 69 | ``` |
| 70 | |
| 71 | ## TypeScript SDK |
| 72 | |
| 73 | ### Installation |
| 74 | |
| 75 | ```bash |
| 76 | npm install @anthropic-ai/sdk |
| 77 | ``` |
| 78 | |
| 79 | ### Basic Message |
| 80 | |
| 81 | ```typescript |
| 82 | import Anthropic from "@anthropic-ai/sdk"; |
| 83 | |
| 84 | const client = new Anthropic(); // reads ANTHROPIC_API_KEY from env |
| 85 | |
| 86 | const message = await client.messages.create({ |
| 87 | model: "claude-sonnet-4-0", |
| 88 | max_tokens: 1024, |
| 89 | messages: [ |
| 90 | { role: "user", content: "Explain async/await in TypeScript" } |
| 91 | ], |
| 92 | }); |
| 93 | console.log(message.content[0].text); |
| 94 | ``` |
| 95 | |
| 96 | ### Streaming |
| 97 | |
| 98 | ```typescript |
| 99 | const stream = client.messages.stream({ |
| 100 | model: "claude-sonnet-4-0", |
| 101 | max_tokens: 1024, |
| 102 | messages: [{ role: "user", content: "Write a haiku" }], |
| 103 | }); |
| 104 | |
| 105 | for await (const event of stream) { |
| 106 | if (event.type === "content_block_delta" && event.delta.type === "text_delta") { |
| 107 | process.stdout.write(event.delta.text); |
| 108 | } |
| 109 | } |
| 110 | ``` |
| 111 | |
| 112 | ## Tool Use |
| 113 | |
| 114 | Define tools and let Claude call them: |
| 115 | |
| 116 | ```python |
| 117 | tools = [ |
| 118 | { |
| 119 | "name": "get_weather", |
| 120 | "description": "Get current weather for a location", |
| 121 | "input_schema": { |
| 122 | "type": "object", |
| 123 | "properties": { |
| 124 | "location": {"type": "string", "description": "City name"}, |
| 125 | "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]} |
| 126 | }, |
| 127 | "required": ["location"] |
| 128 | } |
| 129 | } |
| 130 | ] |
| 131 | |
| 132 | message = client.messages.create( |
| 133 | model="claude-sonnet-4-0", |
| 134 | max_tokens=1024, |
| 135 | tools=tools, |
| 136 | messages=[{"role": "user", "content": "What's the weather in SF?"}] |
| 137 | ) |
| 138 | |
| 139 | # Handle tool use response |
| 140 | for block in message.content: |
| 141 | if block.type == "tool_use": |
| 142 | # Execute the tool with block.input |
| 143 | result = get_weather(**block.input) |
| 144 | # Send result back |
| 145 | follow_up = client.messages.create( |
| 146 | model="claude-sonnet-4-0", |
| 147 | max_tokens=1024, |
| 148 | tools=tools, |
| 149 | messages=[ |
| 150 | {"role": "user", "content": "What's the weather in SF?"}, |
| 151 | {"role": "assistant", "content": message.content}, |
| 152 | {"role": "user", "content": [ |
| 153 | {"type": "tool_result", "tool_use_id": block.id, "content": str(result)} |
| 154 | ]} |
| 155 | ] |
| 156 | ) |
| 157 | ``` |
| 158 | |
| 159 | ## Vision |
| 160 | |
| 161 | Send images for analysis: |
| 162 | |
| 163 | ```python |
| 164 | import base64 |
| 165 | |
| 166 | with open("diagram.png", "rb") as f: |
| 167 | image_data = base64.standard_b64encode(f.read()).decode("utf-8") |
| 168 | |
| 169 | message = client.messages.create( |
| 170 | model="claude-sonnet-4-0", |
| 171 | max_tokens=1024, |
| 172 | messages=[{ |
| 173 | "role": "user", |
| 174 | "content": [ |
| 175 | {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": image_data}}, |
| 176 | {"type": "text", "text": "Describe this diagram"} |
| 177 | ] |