מתי להשתמש
"Anthropic API", "Claude API", "Build with Claude", "API integration".
הוראות עבודה
1. Setup
import anthropic
client = anthropic.Anthropic(api_key="sk-...")
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello!"}
]
)
print(response.content[0].text)
2. Models 2026
claude-opus-4— Best, expensive ($15/$75 per 1M).claude-sonnet-4-6— Balanced ($3/$15).claude-haiku-4-5— Cheap + fast ($0.25/$1.25).
3. Key Features
System Prompts
response = client.messages.create(
model="claude-sonnet-4-6",
system="You are a helpful Hebrew translator.",
messages=[{"role": "user", "content": "Translate: Hello world"}],
max_tokens=100
)
Streaming
with client.messages.stream(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "Tell me a story"}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
Tool Use (Function Calling)
tools = [{
"name": "get_weather",
"description": "Get current weather",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string"}
}
}
}]
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "Weather in Tel Aviv?"}]
)
# Claude responds with tool_use, you execute, send back result
4. Prompt Caching — חיסכון 90%
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
system=[
{
"type": "text",
"text": "[Long system prompt with examples...]",
"cache_control": {"type": "ephemeral"}
}
],
messages=[{"role": "user", "content": "Question?"}]
)
Pricing
- Cache write: 1.25x normal.
- Cache read: 0.1x normal (10% cost).
- Lifetime: 5 minutes (refreshed on use).
When
- Same system prompt across many calls.
- RAG context reused.
- Few-shot examples.
5. Batch API — חיסכון 50%
# Submit batch of 10K requests
batch = client.messages.batches.create(
requests=[
{"custom_id": "1", "params": {...}},
{"custom_id": "2", "params": {...}},
# ... up to 10K
]
)
# Process within 24 hours, 50% cost
Use Cases
- Bulk classification.
- Mass content generation.
- Migration / re-processing.
6. Vision
response = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{
"role": "user",
"content": [
{
"type": "image",
"source": {"type": "base64", "media_type": "image/jpeg", "data": "..."}
},
{"type": "text", "text": "Describe this image"}
]
}]
)
7. Best Practices
Performance
- Stream for UX (perceived speed).
- Parallel requests where possible.
- Cache common prompts.
- Batch for async work.
Cost
- Use Haiku for 80% of tasks.
- Limit max_tokens.
- Prompt caching mandatory.
- Monitor usage in console.
Reliability
- Retry logic with exponential backoff.
- Fallback to cheaper model on errors.
- Validate output before downstream actions.
Security
- API keys in env variables, not code.
- Rate limiting on your end.
- Don't log sensitive prompts/responses.
8. Error Handling
import anthropic
try:
response = client.messages.create(...)
except anthropic.RateLimitError:
# Wait and retry
pass
except anthropic.APIError as e:
# Log + alert
pass
9. SDK Languages
- Python (official).
- TypeScript/JavaScript (official).
- Ruby, Go, Java (community).
- HTTP REST for any language.
10. Rate Limits
Tier 1 (default)
- 60 requests/min.
- 50K input tokens/min.
Higher tiers
- Apply via console.
- Up to 4K rpm.
11. Israel Specifics
- Hebrew prompts native quality.
- Privacy: Anthropic doesn't train on API data.
- Pricing: USD (₪3.7-3.8 = $1).
12. אסיים בהמלצה.
פרומפט לדוגמה
Build classifier with Claude API. Python.
Cost optimization for 1M API calls/month.
Tool use for agent. Code example.
© 2026 AI Expert Pro | גרסה 1.0.0