.claude/skills/claude-api-patterns/SKILL.md
# Claude API Patterns ## When to Load This Skill Load when working with: Anthropic SDK, `anthropic` package, Claude API, tool use, streaming responses, message batches, `MessageCreate`, `@anthropic-ai/sdk`. ## SDK Setup ```python import anthropic client = anthropic.Anthropic(api_key=settings.ANTHROPIC_API_KEY) ``` Never hardcode the API key. Always use environment variables validated at startup. ## Basic Message ```python message = client.messages.create( model="claude-sonnet-4-6",
npx skillsauth add pyramidheadshark/ml-claude-infra .claude/skills/claude-api-patternsInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Load when working with: Anthropic SDK, anthropic package, Claude API, tool use, streaming responses, message batches, MessageCreate, @anthropic-ai/sdk.
import anthropic
client = anthropic.Anthropic(api_key=settings.ANTHROPIC_API_KEY)
Never hardcode the API key. Always use environment variables validated at startup.
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": prompt}],
)
return message.content[0].text
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=2048,
system=system_prompt,
messages=[{"role": "user", "content": user_message}],
)
Keep system prompts in separate .txt or .md files, not inline strings. Version them.
with client.messages.stream(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": prompt}],
) as stream:
for text in stream.text_stream:
yield text
Use streaming for: long responses, real-time UX, progress indication.
from pydantic import BaseModel
class SearchInput(BaseModel):
query: str
max_results: int = 10
tools = [{
"name": "search",
"description": "Search for information",
"input_schema": SearchInput.model_json_schema(),
}]
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "Find recent papers on RAG"}],
)
Always define tool schemas with Pydantic — never write raw JSON schemas by hand.
messages = [{"role": "user", "content": user_message}]
while True:
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=4096,
tools=tools,
messages=messages,
)
if response.stop_reason == "end_turn":
break
if response.stop_reason == "tool_use":
tool_results = []
for block in response.content:
if block.type == "tool_use":
result = dispatch_tool(block.name, block.input)
tool_results.append({
"type": "tool_result",
"tool_use_id": block.id,
"content": result,
})
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "user", "content": tool_results})
Log every call:
response.usage)import time
start = time.monotonic()
response = client.messages.create(...)
latency_ms = (time.monotonic() - start) * 1000
logger.info("claude_call", extra={
"model": response.model,
"input_tokens": response.usage.input_tokens,
"output_tokens": response.usage.output_tokens,
"stop_reason": response.stop_reason,
"latency_ms": round(latency_ms, 1),
})
import anthropic
client = anthropic.Anthropic(
max_retries=3,
)
The SDK retries on 429 and 529 by default. For production, add exponential backoff via httpx transport or a wrapper. Never swallow anthropic.APIError silently.
| Task | Model |
|------|-------|
| Code, architecture, tests | claude-sonnet-4-6 |
| Complex reasoning, long docs | claude-opus-4-6 |
| Fast classification, routing | claude-haiku-4-5-20251001 |
Routing must be explicit in code — never auto-escalate based on vague heuristics.
resources/streaming-patterns.md — advanced streaming with SSE + FastAPIresources/eval-design.md — building evals for prompt changestesting
# Design Doc Creator ## When to Load This Skill Load when: design documents, requirements, new project start. Short fixture skill for testing (optional/meta skill).
development
# Windows Developer Guide ## When to Load Automatically loaded on Windows (`platform_trigger: "win32"`). Applies to: `.py`, `.ps1`, `.bat`, `.cmd` files and any Windows-specific workflow. ## Python on Windows ### Encoding (CRITICAL) Windows defaults to `cp1251` / `cp1252` for file I/O. Always specify UTF-8 explicitly: ```python with open("file.txt", "r", encoding="utf-8") as f: content = f.read() Path("file.txt").read_text(encoding="utf-8") Path("file.txt").write_text(content, encodin
development
# Test-First Patterns ## When to Load This Skill Load when writing tests, creating `.feature` files, setting up conftest, discussing test strategy, or reviewing coverage. ## Philosophy Tests are written BEFORE code. Always. No exceptions. The order is: Design Doc → BDD Scenarios → Unit Tests → Implementation. BDD scenarios come from the design document's use cases section — they are a direct translation of business requirements into executable specifications. This makes tests the living do
testing
# Skill: Supply Chain Auditor ## When to Load Auto-load when: adding dependencies, reviewing packages, updating versions, or discussing `requirements.txt`, `pyproject.toml`, `package.json`. Triggers on `dependency`, `install`, `package`, `CVE`, `audit`, `vulnerable` (≥2 keywords). ## Core Rules Every new dependency addition must pass this checklist before merging: 1. **Pinned** — exact version in production (`==1.2.3` for pip, `"1.2.3"` for npm, not `^` or `~`). 2. **Maintained** — last com