opencode-skills-antigravity 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundled-skills/ad-creative/SKILL.md +371 -0
- package/bundled-skills/ad-creative/evals/evals.json +90 -0
- package/bundled-skills/ad-creative/references/generative-tools.md +637 -0
- package/bundled-skills/ad-creative/references/platform-specs.md +213 -0
- package/bundled-skills/ai-seo/SKILL.md +407 -0
- package/bundled-skills/ai-seo/evals/evals.json +90 -0
- package/bundled-skills/ai-seo/references/content-patterns.md +285 -0
- package/bundled-skills/ai-seo/references/platform-ranking-factors.md +152 -0
- package/bundled-skills/backend-dev-guidelines/SKILL.md +1 -1
- package/bundled-skills/cc-skill-security-review/SKILL.md +1 -1
- package/bundled-skills/churn-prevention/SKILL.md +433 -0
- package/bundled-skills/churn-prevention/evals/evals.json +93 -0
- package/bundled-skills/churn-prevention/references/cancel-flow-patterns.md +316 -0
- package/bundled-skills/churn-prevention/references/dunning-playbook.md +408 -0
- package/bundled-skills/claude-api/LICENSE.txt +202 -0
- package/bundled-skills/claude-api/SKILL.md +252 -0
- package/bundled-skills/claude-api/csharp/claude-api.md +70 -0
- package/bundled-skills/claude-api/curl/examples.md +164 -0
- package/bundled-skills/claude-api/go/claude-api.md +146 -0
- package/bundled-skills/claude-api/java/claude-api.md +128 -0
- package/bundled-skills/claude-api/php/claude-api.md +88 -0
- package/bundled-skills/claude-api/python/agent-sdk/README.md +269 -0
- package/bundled-skills/claude-api/python/agent-sdk/patterns.md +319 -0
- package/bundled-skills/claude-api/python/claude-api/README.md +404 -0
- package/bundled-skills/claude-api/python/claude-api/batches.md +182 -0
- package/bundled-skills/claude-api/python/claude-api/files-api.md +162 -0
- package/bundled-skills/claude-api/python/claude-api/streaming.md +162 -0
- package/bundled-skills/claude-api/python/claude-api/tool-use.md +587 -0
- package/bundled-skills/claude-api/ruby/claude-api.md +87 -0
- package/bundled-skills/claude-api/shared/error-codes.md +205 -0
- package/bundled-skills/claude-api/shared/live-sources.md +121 -0
- package/bundled-skills/claude-api/shared/models.md +68 -0
- package/bundled-skills/claude-api/shared/tool-use-concepts.md +305 -0
- package/bundled-skills/claude-api/typescript/agent-sdk/README.md +220 -0
- package/bundled-skills/claude-api/typescript/agent-sdk/patterns.md +150 -0
- package/bundled-skills/claude-api/typescript/claude-api/README.md +313 -0
- package/bundled-skills/claude-api/typescript/claude-api/batches.md +106 -0
- package/bundled-skills/claude-api/typescript/claude-api/files-api.md +98 -0
- package/bundled-skills/claude-api/typescript/claude-api/streaming.md +178 -0
- package/bundled-skills/claude-api/typescript/claude-api/tool-use.md +477 -0
- package/bundled-skills/codex-review/SKILL.md +1 -1
- package/bundled-skills/cold-email/SKILL.md +167 -0
- package/bundled-skills/cold-email/evals/evals.json +94 -0
- package/bundled-skills/cold-email/references/benchmarks.md +83 -0
- package/bundled-skills/cold-email/references/follow-up-sequences.md +81 -0
- package/bundled-skills/cold-email/references/frameworks.md +90 -0
- package/bundled-skills/cold-email/references/personalization.md +79 -0
- package/bundled-skills/cold-email/references/subject-lines.md +53 -0
- package/bundled-skills/content-strategy/SKILL.md +374 -0
- package/bundled-skills/content-strategy/evals/evals.json +90 -0
- package/bundled-skills/content-strategy/references/headless-cms.md +194 -0
- package/bundled-skills/context7-auto-research/SKILL.md +1 -1
- package/bundled-skills/dbos-golang/SKILL.md +1 -1
- package/bundled-skills/dbos-python/SKILL.md +1 -1
- package/bundled-skills/dbos-typescript/SKILL.md +1 -1
- package/bundled-skills/debug-buttercup/SKILL.md +1 -1
- package/bundled-skills/defuddle/SKILL.md +50 -0
- package/bundled-skills/docs/integrations/jetski-cortex.md +3 -3
- package/bundled-skills/docs/integrations/jetski-gemini-loader/README.md +1 -1
- package/bundled-skills/docs/integrations/jetski-gemini-loader/package.json +1 -0
- package/bundled-skills/docs/maintainers/repo-growth-seo.md +3 -3
- package/bundled-skills/docs/maintainers/skills-import-2026-03-21.md +81 -0
- package/bundled-skills/docs/maintainers/skills-update-guide.md +1 -1
- package/bundled-skills/docs/users/bundles.md +1 -1
- package/bundled-skills/docs/users/claude-code-skills.md +1 -1
- package/bundled-skills/docs/users/gemini-cli-skills.md +1 -1
- package/bundled-skills/docs/users/getting-started.md +1 -1
- package/bundled-skills/docs/users/kiro-integration.md +1 -1
- package/bundled-skills/docs/users/usage.md +4 -4
- package/bundled-skills/docs/users/visual-guide.md +4 -4
- package/bundled-skills/evaluation/SKILL.md +1 -1
- package/bundled-skills/exa-search/SKILL.md +1 -1
- package/bundled-skills/firecrawl-scraper/SKILL.md +1 -1
- package/bundled-skills/frontend-dev-guidelines/SKILL.md +1 -1
- package/bundled-skills/gha-security-review/SKILL.md +2 -1
- package/bundled-skills/git-pushing/SKILL.md +1 -1
- package/bundled-skills/internal-comms/LICENSE.txt +202 -0
- package/bundled-skills/internal-comms/SKILL.md +35 -0
- package/bundled-skills/internal-comms/examples/3p-updates.md +47 -0
- package/bundled-skills/internal-comms/examples/company-newsletter.md +65 -0
- package/bundled-skills/internal-comms/examples/faq-answers.md +30 -0
- package/bundled-skills/internal-comms/examples/general-comms.md +16 -0
- package/bundled-skills/json-canvas/SKILL.md +253 -0
- package/bundled-skills/json-canvas/references/EXAMPLES.md +329 -0
- package/bundled-skills/lead-magnets/SKILL.md +319 -0
- package/bundled-skills/lead-magnets/references/benchmarks.md +129 -0
- package/bundled-skills/lead-magnets/references/format-guide.md +196 -0
- package/bundled-skills/memory-systems/SKILL.md +1 -1
- package/bundled-skills/obsidian-bases/SKILL.md +506 -0
- package/bundled-skills/obsidian-bases/references/FUNCTIONS_REFERENCE.md +173 -0
- package/bundled-skills/obsidian-cli/SKILL.md +115 -0
- package/bundled-skills/obsidian-markdown/SKILL.md +205 -0
- package/bundled-skills/obsidian-markdown/references/CALLOUTS.md +58 -0
- package/bundled-skills/obsidian-markdown/references/EMBEDS.md +63 -0
- package/bundled-skills/obsidian-markdown/references/PROPERTIES.md +61 -0
- package/bundled-skills/product-marketing-context/SKILL.md +250 -0
- package/bundled-skills/product-marketing-context/evals/evals.json +85 -0
- package/bundled-skills/react-best-practices/SKILL.md +1 -1
- package/bundled-skills/revops/SKILL.md +352 -0
- package/bundled-skills/revops/evals/evals.json +91 -0
- package/bundled-skills/revops/references/automation-playbooks.md +290 -0
- package/bundled-skills/revops/references/lifecycle-definitions.md +278 -0
- package/bundled-skills/revops/references/routing-rules.md +203 -0
- package/bundled-skills/revops/references/scoring-models.md +247 -0
- package/bundled-skills/sales-enablement/SKILL.md +358 -0
- package/bundled-skills/sales-enablement/evals/evals.json +91 -0
- package/bundled-skills/sales-enablement/references/deck-frameworks.md +263 -0
- package/bundled-skills/sales-enablement/references/demo-scripts.md +355 -0
- package/bundled-skills/sales-enablement/references/objection-library.md +270 -0
- package/bundled-skills/sales-enablement/references/one-pager-templates.md +208 -0
- package/bundled-skills/seo/SKILL.md +139 -0
- package/bundled-skills/seo/references/cwv-thresholds.md +108 -0
- package/bundled-skills/seo/references/eeat-framework.md +214 -0
- package/bundled-skills/seo/references/quality-gates.md +155 -0
- package/bundled-skills/seo/references/schema-types.md +118 -0
- package/bundled-skills/seo-competitor-pages/SKILL.md +229 -0
- package/bundled-skills/seo-content/SKILL.md +186 -0
- package/bundled-skills/seo-dataforseo/SKILL.md +395 -0
- package/bundled-skills/seo-geo/SKILL.md +254 -0
- package/bundled-skills/seo-hreflang/SKILL.md +209 -0
- package/bundled-skills/seo-image-gen/SKILL.md +183 -0
- package/bundled-skills/seo-images/SKILL.md +193 -0
- package/bundled-skills/seo-page/SKILL.md +103 -0
- package/bundled-skills/seo-plan/SKILL.md +136 -0
- package/bundled-skills/seo-plan/assets/agency.md +175 -0
- package/bundled-skills/seo-plan/assets/ecommerce.md +167 -0
- package/bundled-skills/seo-plan/assets/generic.md +144 -0
- package/bundled-skills/seo-plan/assets/local-service.md +160 -0
- package/bundled-skills/seo-plan/assets/publisher.md +153 -0
- package/bundled-skills/seo-plan/assets/saas.md +135 -0
- package/bundled-skills/seo-programmatic/SKILL.md +184 -0
- package/bundled-skills/seo-schema/SKILL.md +178 -0
- package/bundled-skills/seo-sitemap/SKILL.md +129 -0
- package/bundled-skills/seo-technical/SKILL.md +175 -0
- package/bundled-skills/site-architecture/SKILL.md +366 -0
- package/bundled-skills/site-architecture/evals/evals.json +88 -0
- package/bundled-skills/site-architecture/references/mermaid-templates.md +216 -0
- package/bundled-skills/site-architecture/references/navigation-patterns.md +305 -0
- package/bundled-skills/site-architecture/references/site-type-templates.md +293 -0
- package/bundled-skills/skill-improver/SKILL.md +1 -1
- package/bundled-skills/tavily-web/SKILL.md +1 -1
- package/bundled-skills/test-fixing/SKILL.md +1 -1
- package/bundled-skills/tool-design/SKILL.md +1 -1
- package/bundled-skills/ui-ux-pro-max/SKILL.md +1 -1
- package/bundled-skills/verification-before-completion/SKILL.md +1 -1
- package/bundled-skills/wiki-changelog/SKILL.md +1 -1
- package/bundled-skills/wiki-onboarding/SKILL.md +1 -1
- package/bundled-skills/wiki-qa/SKILL.md +1 -1
- package/bundled-skills/wiki-researcher/SKILL.md +1 -1
- package/bundled-skills/wiki-vitepress/SKILL.md +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# Message Batches API — Python
|
|
2
|
+
|
|
3
|
+
The Batches API (`POST /v1/messages/batches`) processes Messages API requests asynchronously at 50% of standard prices.
|
|
4
|
+
|
|
5
|
+
## Key Facts
|
|
6
|
+
|
|
7
|
+
- Up to 100,000 requests or 256 MB per batch
|
|
8
|
+
- Most batches complete within 1 hour; maximum 24 hours
|
|
9
|
+
- Results available for 29 days after creation
|
|
10
|
+
- 50% cost reduction on all token usage
|
|
11
|
+
- All Messages API features supported (vision, tools, caching, etc.)
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Create a Batch
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
import anthropic
|
|
19
|
+
from anthropic.types.message_create_params import MessageCreateParamsNonStreaming
|
|
20
|
+
from anthropic.types.messages.batch_create_params import Request
|
|
21
|
+
|
|
22
|
+
client = anthropic.Anthropic()
|
|
23
|
+
|
|
24
|
+
message_batch = client.messages.batches.create(
|
|
25
|
+
requests=[
|
|
26
|
+
Request(
|
|
27
|
+
custom_id="request-1",
|
|
28
|
+
params=MessageCreateParamsNonStreaming(
|
|
29
|
+
model="claude-opus-4-6",
|
|
30
|
+
max_tokens=1024,
|
|
31
|
+
messages=[{"role": "user", "content": "Summarize climate change impacts"}]
|
|
32
|
+
)
|
|
33
|
+
),
|
|
34
|
+
Request(
|
|
35
|
+
custom_id="request-2",
|
|
36
|
+
params=MessageCreateParamsNonStreaming(
|
|
37
|
+
model="claude-opus-4-6",
|
|
38
|
+
max_tokens=1024,
|
|
39
|
+
messages=[{"role": "user", "content": "Explain quantum computing basics"}]
|
|
40
|
+
)
|
|
41
|
+
),
|
|
42
|
+
]
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
print(f"Batch ID: {message_batch.id}")
|
|
46
|
+
print(f"Status: {message_batch.processing_status}")
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## Poll for Completion
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
import time
|
|
55
|
+
|
|
56
|
+
while True:
|
|
57
|
+
batch = client.messages.batches.retrieve(message_batch.id)
|
|
58
|
+
if batch.processing_status == "ended":
|
|
59
|
+
break
|
|
60
|
+
print(f"Status: {batch.processing_status}, processing: {batch.request_counts.processing}")
|
|
61
|
+
time.sleep(60)
|
|
62
|
+
|
|
63
|
+
print("Batch complete!")
|
|
64
|
+
print(f"Succeeded: {batch.request_counts.succeeded}")
|
|
65
|
+
print(f"Errored: {batch.request_counts.errored}")
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## Retrieve Results
|
|
71
|
+
|
|
72
|
+
> **Note:** Examples below use `match/case` syntax, requiring Python 3.10+. For earlier versions, use `if/elif` chains instead.
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
for result in client.messages.batches.results(message_batch.id):
|
|
76
|
+
match result.result.type:
|
|
77
|
+
case "succeeded":
|
|
78
|
+
print(f"[{result.custom_id}] {result.result.message.content[0].text[:100]}")
|
|
79
|
+
case "errored":
|
|
80
|
+
if result.result.error.type == "invalid_request":
|
|
81
|
+
print(f"[{result.custom_id}] Validation error - fix request and retry")
|
|
82
|
+
else:
|
|
83
|
+
print(f"[{result.custom_id}] Server error - safe to retry")
|
|
84
|
+
case "canceled":
|
|
85
|
+
print(f"[{result.custom_id}] Canceled")
|
|
86
|
+
case "expired":
|
|
87
|
+
print(f"[{result.custom_id}] Expired - resubmit")
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## Cancel a Batch
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
cancelled = client.messages.batches.cancel(message_batch.id)
|
|
96
|
+
print(f"Status: {cancelled.processing_status}") # "canceling"
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
---
|
|
100
|
+
|
|
101
|
+
## Batch with Prompt Caching
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
shared_system = [
|
|
105
|
+
{"type": "text", "text": "You are a literary analyst."},
|
|
106
|
+
{
|
|
107
|
+
"type": "text",
|
|
108
|
+
"text": large_document_text, # Shared across all requests
|
|
109
|
+
"cache_control": {"type": "ephemeral"}
|
|
110
|
+
}
|
|
111
|
+
]
|
|
112
|
+
|
|
113
|
+
message_batch = client.messages.batches.create(
|
|
114
|
+
requests=[
|
|
115
|
+
Request(
|
|
116
|
+
custom_id=f"analysis-{i}",
|
|
117
|
+
params=MessageCreateParamsNonStreaming(
|
|
118
|
+
model="claude-opus-4-6",
|
|
119
|
+
max_tokens=1024,
|
|
120
|
+
system=shared_system,
|
|
121
|
+
messages=[{"role": "user", "content": question}]
|
|
122
|
+
)
|
|
123
|
+
)
|
|
124
|
+
for i, question in enumerate(questions)
|
|
125
|
+
]
|
|
126
|
+
)
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
## Full End-to-End Example
|
|
132
|
+
|
|
133
|
+
```python
|
|
134
|
+
import anthropic
|
|
135
|
+
import time
|
|
136
|
+
from anthropic.types.message_create_params import MessageCreateParamsNonStreaming
|
|
137
|
+
from anthropic.types.messages.batch_create_params import Request
|
|
138
|
+
|
|
139
|
+
client = anthropic.Anthropic()
|
|
140
|
+
|
|
141
|
+
# 1. Prepare requests
|
|
142
|
+
items_to_classify = [
|
|
143
|
+
"The product quality is excellent!",
|
|
144
|
+
"Terrible customer service, never again.",
|
|
145
|
+
"It's okay, nothing special.",
|
|
146
|
+
]
|
|
147
|
+
|
|
148
|
+
requests = [
|
|
149
|
+
Request(
|
|
150
|
+
custom_id=f"classify-{i}",
|
|
151
|
+
params=MessageCreateParamsNonStreaming(
|
|
152
|
+
model="claude-haiku-4-5",
|
|
153
|
+
max_tokens=50,
|
|
154
|
+
messages=[{
|
|
155
|
+
"role": "user",
|
|
156
|
+
"content": f"Classify as positive/negative/neutral (one word): {text}"
|
|
157
|
+
}]
|
|
158
|
+
)
|
|
159
|
+
)
|
|
160
|
+
for i, text in enumerate(items_to_classify)
|
|
161
|
+
]
|
|
162
|
+
|
|
163
|
+
# 2. Create batch
|
|
164
|
+
batch = client.messages.batches.create(requests=requests)
|
|
165
|
+
print(f"Created batch: {batch.id}")
|
|
166
|
+
|
|
167
|
+
# 3. Wait for completion
|
|
168
|
+
while True:
|
|
169
|
+
batch = client.messages.batches.retrieve(batch.id)
|
|
170
|
+
if batch.processing_status == "ended":
|
|
171
|
+
break
|
|
172
|
+
time.sleep(10)
|
|
173
|
+
|
|
174
|
+
# 4. Collect results
|
|
175
|
+
results = {}
|
|
176
|
+
for result in client.messages.batches.results(batch.id):
|
|
177
|
+
if result.result.type == "succeeded":
|
|
178
|
+
results[result.custom_id] = result.result.message.content[0].text
|
|
179
|
+
|
|
180
|
+
for custom_id, classification in sorted(results.items()):
|
|
181
|
+
print(f"{custom_id}: {classification}")
|
|
182
|
+
```
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# Files API — Python
|
|
2
|
+
|
|
3
|
+
The Files API uploads files for use in Messages API requests. Reference files via `file_id` in content blocks, avoiding re-uploads across multiple API calls.
|
|
4
|
+
|
|
5
|
+
**Beta:** Pass `betas=["files-api-2025-04-14"]` in your API calls (the SDK sets the required header automatically).
|
|
6
|
+
|
|
7
|
+
## Key Facts
|
|
8
|
+
|
|
9
|
+
- Maximum file size: 500 MB
|
|
10
|
+
- Total storage: 100 GB per organization
|
|
11
|
+
- Files persist until deleted
|
|
12
|
+
- File operations (upload, list, delete) are free; content used in messages is billed as input tokens
|
|
13
|
+
- Not available on Amazon Bedrock or Google Vertex AI
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Upload a File
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
import anthropic
|
|
21
|
+
|
|
22
|
+
client = anthropic.Anthropic()
|
|
23
|
+
|
|
24
|
+
uploaded = client.beta.files.upload(
|
|
25
|
+
file=("report.pdf", open("report.pdf", "rb"), "application/pdf"),
|
|
26
|
+
)
|
|
27
|
+
print(f"File ID: {uploaded.id}")
|
|
28
|
+
print(f"Size: {uploaded.size_bytes} bytes")
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Use a File in Messages
|
|
34
|
+
|
|
35
|
+
### PDF / Text Document
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
response = client.beta.messages.create(
|
|
39
|
+
model="claude-opus-4-6",
|
|
40
|
+
max_tokens=1024,
|
|
41
|
+
messages=[{
|
|
42
|
+
"role": "user",
|
|
43
|
+
"content": [
|
|
44
|
+
{"type": "text", "text": "Summarize the key findings in this report."},
|
|
45
|
+
{
|
|
46
|
+
"type": "document",
|
|
47
|
+
"source": {"type": "file", "file_id": uploaded.id},
|
|
48
|
+
"title": "Q4 Report", # optional
|
|
49
|
+
"citations": {"enabled": True} # optional, enables citations
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
}],
|
|
53
|
+
betas=["files-api-2025-04-14"],
|
|
54
|
+
)
|
|
55
|
+
print(response.content[0].text)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Image
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
image_file = client.beta.files.upload(
|
|
62
|
+
file=("photo.png", open("photo.png", "rb"), "image/png"),
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
response = client.beta.messages.create(
|
|
66
|
+
model="claude-opus-4-6",
|
|
67
|
+
max_tokens=1024,
|
|
68
|
+
messages=[{
|
|
69
|
+
"role": "user",
|
|
70
|
+
"content": [
|
|
71
|
+
{"type": "text", "text": "What's in this image?"},
|
|
72
|
+
{
|
|
73
|
+
"type": "image",
|
|
74
|
+
"source": {"type": "file", "file_id": image_file.id}
|
|
75
|
+
}
|
|
76
|
+
]
|
|
77
|
+
}],
|
|
78
|
+
betas=["files-api-2025-04-14"],
|
|
79
|
+
)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## Manage Files
|
|
85
|
+
|
|
86
|
+
### List Files
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
files = client.beta.files.list()
|
|
90
|
+
for f in files.data:
|
|
91
|
+
print(f"{f.id}: {f.filename} ({f.size_bytes} bytes)")
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Get File Metadata
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
file_info = client.beta.files.retrieve_metadata("file_011CNha8iCJcU1wXNR6q4V8w")
|
|
98
|
+
print(f"Filename: {file_info.filename}")
|
|
99
|
+
print(f"MIME type: {file_info.mime_type}")
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Delete a File
|
|
103
|
+
|
|
104
|
+
```python
|
|
105
|
+
client.beta.files.delete("file_011CNha8iCJcU1wXNR6q4V8w")
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Download a File
|
|
109
|
+
|
|
110
|
+
Only files created by the code execution tool or skills can be downloaded (not user-uploaded files).
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
file_content = client.beta.files.download("file_011CNha8iCJcU1wXNR6q4V8w")
|
|
114
|
+
file_content.write_to_file("output.txt")
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## Full End-to-End Example
|
|
120
|
+
|
|
121
|
+
Upload a document once, ask multiple questions about it:
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
import anthropic
|
|
125
|
+
|
|
126
|
+
client = anthropic.Anthropic()
|
|
127
|
+
|
|
128
|
+
# 1. Upload once
|
|
129
|
+
uploaded = client.beta.files.upload(
|
|
130
|
+
file=("contract.pdf", open("contract.pdf", "rb"), "application/pdf"),
|
|
131
|
+
)
|
|
132
|
+
print(f"Uploaded: {uploaded.id}")
|
|
133
|
+
|
|
134
|
+
# 2. Ask multiple questions using the same file_id
|
|
135
|
+
questions = [
|
|
136
|
+
"What are the key terms and conditions?",
|
|
137
|
+
"What is the termination clause?",
|
|
138
|
+
"Summarize the payment schedule.",
|
|
139
|
+
]
|
|
140
|
+
|
|
141
|
+
for question in questions:
|
|
142
|
+
response = client.beta.messages.create(
|
|
143
|
+
model="claude-opus-4-6",
|
|
144
|
+
max_tokens=1024,
|
|
145
|
+
messages=[{
|
|
146
|
+
"role": "user",
|
|
147
|
+
"content": [
|
|
148
|
+
{"type": "text", "text": question},
|
|
149
|
+
{
|
|
150
|
+
"type": "document",
|
|
151
|
+
"source": {"type": "file", "file_id": uploaded.id}
|
|
152
|
+
}
|
|
153
|
+
]
|
|
154
|
+
}],
|
|
155
|
+
betas=["files-api-2025-04-14"],
|
|
156
|
+
)
|
|
157
|
+
print(f"\nQ: {question}")
|
|
158
|
+
print(f"A: {response.content[0].text[:200]}")
|
|
159
|
+
|
|
160
|
+
# 3. Clean up when done
|
|
161
|
+
client.beta.files.delete(uploaded.id)
|
|
162
|
+
```
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# Streaming — Python
|
|
2
|
+
|
|
3
|
+
## Quick Start
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
with client.messages.stream(
|
|
7
|
+
model="claude-opus-4-6",
|
|
8
|
+
max_tokens=1024,
|
|
9
|
+
messages=[{"role": "user", "content": "Write a story"}]
|
|
10
|
+
) as stream:
|
|
11
|
+
for text in stream.text_stream:
|
|
12
|
+
print(text, end="", flush=True)
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Async
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
async with async_client.messages.stream(
|
|
19
|
+
model="claude-opus-4-6",
|
|
20
|
+
max_tokens=1024,
|
|
21
|
+
messages=[{"role": "user", "content": "Write a story"}]
|
|
22
|
+
) as stream:
|
|
23
|
+
async for text in stream.text_stream:
|
|
24
|
+
print(text, end="", flush=True)
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Handling Different Content Types
|
|
30
|
+
|
|
31
|
+
Claude may return text, thinking blocks, or tool use. Handle each appropriately:
|
|
32
|
+
|
|
33
|
+
> **Opus 4.6:** Use `thinking: {type: "adaptive"}`. On older models, use `thinking: {type: "enabled", budget_tokens: N}` instead.
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
with client.messages.stream(
|
|
37
|
+
model="claude-opus-4-6",
|
|
38
|
+
max_tokens=16000,
|
|
39
|
+
thinking={"type": "adaptive"},
|
|
40
|
+
messages=[{"role": "user", "content": "Analyze this problem"}]
|
|
41
|
+
) as stream:
|
|
42
|
+
for event in stream:
|
|
43
|
+
if event.type == "content_block_start":
|
|
44
|
+
if event.content_block.type == "thinking":
|
|
45
|
+
print("\n[Thinking...]")
|
|
46
|
+
elif event.content_block.type == "text":
|
|
47
|
+
print("\n[Response:]")
|
|
48
|
+
|
|
49
|
+
elif event.type == "content_block_delta":
|
|
50
|
+
if event.delta.type == "thinking_delta":
|
|
51
|
+
print(event.delta.thinking, end="", flush=True)
|
|
52
|
+
elif event.delta.type == "text_delta":
|
|
53
|
+
print(event.delta.text, end="", flush=True)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## Streaming with Tool Use
|
|
59
|
+
|
|
60
|
+
The Python tool runner currently returns complete messages. Use streaming for individual API calls within a manual loop if you need per-token streaming with tools:
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
with client.messages.stream(
|
|
64
|
+
model="claude-opus-4-6",
|
|
65
|
+
max_tokens=4096,
|
|
66
|
+
tools=tools,
|
|
67
|
+
messages=messages
|
|
68
|
+
) as stream:
|
|
69
|
+
for text in stream.text_stream:
|
|
70
|
+
print(text, end="", flush=True)
|
|
71
|
+
|
|
72
|
+
response = stream.get_final_message()
|
|
73
|
+
# Continue with tool execution if response.stop_reason == "tool_use"
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
## Getting the Final Message
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
with client.messages.stream(
|
|
82
|
+
model="claude-opus-4-6",
|
|
83
|
+
max_tokens=1024,
|
|
84
|
+
messages=[{"role": "user", "content": "Hello"}]
|
|
85
|
+
) as stream:
|
|
86
|
+
for text in stream.text_stream:
|
|
87
|
+
print(text, end="", flush=True)
|
|
88
|
+
|
|
89
|
+
# Get full message after streaming
|
|
90
|
+
final_message = stream.get_final_message()
|
|
91
|
+
print(f"\n\nTokens used: {final_message.usage.output_tokens}")
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
## Streaming with Progress Updates
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
def stream_with_progress(client, **kwargs):
|
|
100
|
+
"""Stream a response with progress updates."""
|
|
101
|
+
total_tokens = 0
|
|
102
|
+
content_parts = []
|
|
103
|
+
|
|
104
|
+
with client.messages.stream(**kwargs) as stream:
|
|
105
|
+
for event in stream:
|
|
106
|
+
if event.type == "content_block_delta":
|
|
107
|
+
if event.delta.type == "text_delta":
|
|
108
|
+
text = event.delta.text
|
|
109
|
+
content_parts.append(text)
|
|
110
|
+
print(text, end="", flush=True)
|
|
111
|
+
|
|
112
|
+
elif event.type == "message_delta":
|
|
113
|
+
if event.usage and event.usage.output_tokens is not None:
|
|
114
|
+
total_tokens = event.usage.output_tokens
|
|
115
|
+
|
|
116
|
+
final_message = stream.get_final_message()
|
|
117
|
+
|
|
118
|
+
print(f"\n\n[Tokens used: {total_tokens}]")
|
|
119
|
+
return "".join(content_parts)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
## Error Handling in Streams
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
try:
|
|
128
|
+
with client.messages.stream(
|
|
129
|
+
model="claude-opus-4-6",
|
|
130
|
+
max_tokens=1024,
|
|
131
|
+
messages=[{"role": "user", "content": "Write a story"}]
|
|
132
|
+
) as stream:
|
|
133
|
+
for text in stream.text_stream:
|
|
134
|
+
print(text, end="", flush=True)
|
|
135
|
+
except anthropic.APIConnectionError:
|
|
136
|
+
print("\nConnection lost. Please retry.")
|
|
137
|
+
except anthropic.RateLimitError:
|
|
138
|
+
print("\nRate limited. Please wait and retry.")
|
|
139
|
+
except anthropic.APIStatusError as e:
|
|
140
|
+
print(f"\nAPI error: {e.status_code}")
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## Stream Event Types
|
|
146
|
+
|
|
147
|
+
| Event Type | Description | When it fires |
|
|
148
|
+
| --------------------- | --------------------------- | --------------------------------- |
|
|
149
|
+
| `message_start` | Contains message metadata | Once at the beginning |
|
|
150
|
+
| `content_block_start` | New content block beginning | When a text/tool_use block starts |
|
|
151
|
+
| `content_block_delta` | Incremental content update | For each token/chunk |
|
|
152
|
+
| `content_block_stop` | Content block complete | When a block finishes |
|
|
153
|
+
| `message_delta` | Message-level updates | Contains `stop_reason`, usage |
|
|
154
|
+
| `message_stop` | Message complete | Once at the end |
|
|
155
|
+
|
|
156
|
+
## Best Practices
|
|
157
|
+
|
|
158
|
+
1. **Always flush output** — Use `flush=True` to show tokens immediately
|
|
159
|
+
2. **Handle partial responses** — If the stream is interrupted, you may have incomplete content
|
|
160
|
+
3. **Track token usage** — The `message_delta` event contains usage information
|
|
161
|
+
4. **Use timeouts** — Set appropriate timeouts for your application
|
|
162
|
+
5. **Default to streaming** — Use `.get_final_message()` to get the complete response even when streaming, giving you timeout protection without needing to handle individual events
|