claude-api-patterns
Skill4 starsChanged 7 months agoMITArchived repositoryView on GitHub
Load when working with: Anthropic SDK, anthropic package, Claude API, tool use, streaming responses, message batches, MessageCreate, @anthropic-ai/sdk. Never hardcode the API key. Always use environment variables validated at startup. Keep system prompts in separate .txt or .md files, not inline strings. Version them. Use streaming for: long responses, real-time UX, progress indication. Always define tool schemas with Pydantic — never write raw JSON schemas by hand. Log every call: - model, max_tokens, stop_reason - input_tokens, output_tokens (from response.usage) -…
- Reads credentials
In this repository23 other files it gives its agents.
- .claude/CLAUDE.mdCLAUDE.md
- llms.txtllms.txt
- critical-analysisSkill
- database-migration-safetySkill
- data-validationSkill
- design-doc-creatorSkill
- experiment-trackingSkill
- fastapi-patternsSkill
- github-actionsSkill
- htmx-frontendSkill
- infra-yandex-cloudSkill
- langgraph-patternsSkill
- ml-data-handlingSkill
- multimodal-routerSkill
- nlp-slm-patternsSkill
- predictive-analyticsSkill
- prompt-engineeringSkill
- python-project-standardsSkill
- rag-vector-dbSkill
- skill-developerSkill
- supply-chain-auditorSkill
- test-first-patternsSkill
- windows-developerSkill
The file
We split this skill into the rules it gives an agent
4 rules. About 0 are common, 3 are distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 15
Never hardcode the API key.
- Very specific: line 39
Keep system prompts in separate .txt or .md files, not inline strings.
- Distinctive: line 53
Use streaming for: long responses, real-time UX, progress indication.
- Distinctive: line 78
Always define tool schemas with Pydantic — never write raw JSON schemas by hand.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
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
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
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": prompt}],
)
return message.content[0].textSystem Prompt
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.
Streaming
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 textUse streaming for: long responses, real-time UX, progress indication.
Tool Use (Function Calling)
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.
Tool Result Loop
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})Observability
Log every call:
- model, max_tokens, stop_reason
- input_tokens, output_tokens (from
response.usage) - latency (wall time)
- cost estimate (use token counts × price per token)
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),
})Rate Limits and Retry
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.
Model Selection
| Task | Model |
| Code, architecture, tests | claude-sonnet-4-6 |
| Complex reasoning, long docs | claude-opus-4-7 |
| Fast classification, routing | claude-haiku-4-5-20251001 |
Routing must be explicit in code — never auto-escalate based on vague heuristics.
See Also
resources/streaming-patterns.md— advanced streaming with SSE + FastAPIresources/eval-design.md— building evals for prompt changes
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

