opencode-skills-antigravity 1.0.5 → 1.0.7

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.
Files changed (125) hide show
  1. package/bundled-skills/ad-creative/SKILL.md +371 -0
  2. package/bundled-skills/ad-creative/evals/evals.json +90 -0
  3. package/bundled-skills/ad-creative/references/generative-tools.md +637 -0
  4. package/bundled-skills/ad-creative/references/platform-specs.md +213 -0
  5. package/bundled-skills/ai-seo/SKILL.md +407 -0
  6. package/bundled-skills/ai-seo/evals/evals.json +90 -0
  7. package/bundled-skills/ai-seo/references/content-patterns.md +285 -0
  8. package/bundled-skills/ai-seo/references/platform-ranking-factors.md +152 -0
  9. package/bundled-skills/churn-prevention/SKILL.md +433 -0
  10. package/bundled-skills/churn-prevention/evals/evals.json +93 -0
  11. package/bundled-skills/churn-prevention/references/cancel-flow-patterns.md +316 -0
  12. package/bundled-skills/churn-prevention/references/dunning-playbook.md +408 -0
  13. package/bundled-skills/claude-api/LICENSE.txt +202 -0
  14. package/bundled-skills/claude-api/SKILL.md +252 -0
  15. package/bundled-skills/claude-api/csharp/claude-api.md +70 -0
  16. package/bundled-skills/claude-api/curl/examples.md +164 -0
  17. package/bundled-skills/claude-api/go/claude-api.md +146 -0
  18. package/bundled-skills/claude-api/java/claude-api.md +128 -0
  19. package/bundled-skills/claude-api/php/claude-api.md +88 -0
  20. package/bundled-skills/claude-api/python/agent-sdk/README.md +269 -0
  21. package/bundled-skills/claude-api/python/agent-sdk/patterns.md +319 -0
  22. package/bundled-skills/claude-api/python/claude-api/README.md +404 -0
  23. package/bundled-skills/claude-api/python/claude-api/batches.md +182 -0
  24. package/bundled-skills/claude-api/python/claude-api/files-api.md +162 -0
  25. package/bundled-skills/claude-api/python/claude-api/streaming.md +162 -0
  26. package/bundled-skills/claude-api/python/claude-api/tool-use.md +587 -0
  27. package/bundled-skills/claude-api/ruby/claude-api.md +87 -0
  28. package/bundled-skills/claude-api/shared/error-codes.md +205 -0
  29. package/bundled-skills/claude-api/shared/live-sources.md +121 -0
  30. package/bundled-skills/claude-api/shared/models.md +68 -0
  31. package/bundled-skills/claude-api/shared/tool-use-concepts.md +305 -0
  32. package/bundled-skills/claude-api/typescript/agent-sdk/README.md +220 -0
  33. package/bundled-skills/claude-api/typescript/agent-sdk/patterns.md +150 -0
  34. package/bundled-skills/claude-api/typescript/claude-api/README.md +313 -0
  35. package/bundled-skills/claude-api/typescript/claude-api/batches.md +106 -0
  36. package/bundled-skills/claude-api/typescript/claude-api/files-api.md +98 -0
  37. package/bundled-skills/claude-api/typescript/claude-api/streaming.md +178 -0
  38. package/bundled-skills/claude-api/typescript/claude-api/tool-use.md +477 -0
  39. package/bundled-skills/cold-email/SKILL.md +167 -0
  40. package/bundled-skills/cold-email/evals/evals.json +94 -0
  41. package/bundled-skills/cold-email/references/benchmarks.md +83 -0
  42. package/bundled-skills/cold-email/references/follow-up-sequences.md +81 -0
  43. package/bundled-skills/cold-email/references/frameworks.md +90 -0
  44. package/bundled-skills/cold-email/references/personalization.md +79 -0
  45. package/bundled-skills/cold-email/references/subject-lines.md +53 -0
  46. package/bundled-skills/content-strategy/SKILL.md +374 -0
  47. package/bundled-skills/content-strategy/evals/evals.json +90 -0
  48. package/bundled-skills/content-strategy/references/headless-cms.md +194 -0
  49. package/bundled-skills/defuddle/SKILL.md +50 -0
  50. package/bundled-skills/docs/integrations/jetski-cortex.md +3 -3
  51. package/bundled-skills/docs/integrations/jetski-gemini-loader/README.md +1 -1
  52. package/bundled-skills/docs/maintainers/release-process.md +3 -0
  53. package/bundled-skills/docs/maintainers/repo-growth-seo.md +3 -3
  54. package/bundled-skills/docs/maintainers/skills-import-2026-03-21.md +81 -0
  55. package/bundled-skills/docs/maintainers/skills-update-guide.md +1 -1
  56. package/bundled-skills/docs/users/bundles.md +1 -1
  57. package/bundled-skills/docs/users/claude-code-skills.md +1 -1
  58. package/bundled-skills/docs/users/faq.md +27 -6
  59. package/bundled-skills/docs/users/gemini-cli-skills.md +1 -1
  60. package/bundled-skills/docs/users/getting-started.md +1 -1
  61. package/bundled-skills/docs/users/kiro-integration.md +1 -1
  62. package/bundled-skills/docs/users/usage.md +6 -6
  63. package/bundled-skills/docs/users/visual-guide.md +18 -18
  64. package/bundled-skills/internal-comms/LICENSE.txt +202 -0
  65. package/bundled-skills/internal-comms/SKILL.md +35 -0
  66. package/bundled-skills/internal-comms/examples/3p-updates.md +47 -0
  67. package/bundled-skills/internal-comms/examples/company-newsletter.md +65 -0
  68. package/bundled-skills/internal-comms/examples/faq-answers.md +30 -0
  69. package/bundled-skills/internal-comms/examples/general-comms.md +16 -0
  70. package/bundled-skills/json-canvas/SKILL.md +253 -0
  71. package/bundled-skills/json-canvas/references/EXAMPLES.md +329 -0
  72. package/bundled-skills/lead-magnets/SKILL.md +319 -0
  73. package/bundled-skills/lead-magnets/references/benchmarks.md +129 -0
  74. package/bundled-skills/lead-magnets/references/format-guide.md +196 -0
  75. package/bundled-skills/obsidian-bases/SKILL.md +506 -0
  76. package/bundled-skills/obsidian-bases/references/FUNCTIONS_REFERENCE.md +173 -0
  77. package/bundled-skills/obsidian-cli/SKILL.md +115 -0
  78. package/bundled-skills/obsidian-markdown/SKILL.md +205 -0
  79. package/bundled-skills/obsidian-markdown/references/CALLOUTS.md +58 -0
  80. package/bundled-skills/obsidian-markdown/references/EMBEDS.md +63 -0
  81. package/bundled-skills/obsidian-markdown/references/PROPERTIES.md +61 -0
  82. package/bundled-skills/product-marketing-context/SKILL.md +250 -0
  83. package/bundled-skills/product-marketing-context/evals/evals.json +85 -0
  84. package/bundled-skills/revops/SKILL.md +352 -0
  85. package/bundled-skills/revops/evals/evals.json +91 -0
  86. package/bundled-skills/revops/references/automation-playbooks.md +290 -0
  87. package/bundled-skills/revops/references/lifecycle-definitions.md +278 -0
  88. package/bundled-skills/revops/references/routing-rules.md +203 -0
  89. package/bundled-skills/revops/references/scoring-models.md +247 -0
  90. package/bundled-skills/sales-enablement/SKILL.md +358 -0
  91. package/bundled-skills/sales-enablement/evals/evals.json +91 -0
  92. package/bundled-skills/sales-enablement/references/deck-frameworks.md +263 -0
  93. package/bundled-skills/sales-enablement/references/demo-scripts.md +355 -0
  94. package/bundled-skills/sales-enablement/references/objection-library.md +270 -0
  95. package/bundled-skills/sales-enablement/references/one-pager-templates.md +208 -0
  96. package/bundled-skills/seo/SKILL.md +139 -0
  97. package/bundled-skills/seo/references/cwv-thresholds.md +108 -0
  98. package/bundled-skills/seo/references/eeat-framework.md +214 -0
  99. package/bundled-skills/seo/references/quality-gates.md +155 -0
  100. package/bundled-skills/seo/references/schema-types.md +118 -0
  101. package/bundled-skills/seo-competitor-pages/SKILL.md +229 -0
  102. package/bundled-skills/seo-content/SKILL.md +186 -0
  103. package/bundled-skills/seo-dataforseo/SKILL.md +395 -0
  104. package/bundled-skills/seo-geo/SKILL.md +254 -0
  105. package/bundled-skills/seo-hreflang/SKILL.md +209 -0
  106. package/bundled-skills/seo-image-gen/SKILL.md +183 -0
  107. package/bundled-skills/seo-images/SKILL.md +193 -0
  108. package/bundled-skills/seo-page/SKILL.md +103 -0
  109. package/bundled-skills/seo-plan/SKILL.md +136 -0
  110. package/bundled-skills/seo-plan/assets/agency.md +175 -0
  111. package/bundled-skills/seo-plan/assets/ecommerce.md +167 -0
  112. package/bundled-skills/seo-plan/assets/generic.md +144 -0
  113. package/bundled-skills/seo-plan/assets/local-service.md +160 -0
  114. package/bundled-skills/seo-plan/assets/publisher.md +153 -0
  115. package/bundled-skills/seo-plan/assets/saas.md +135 -0
  116. package/bundled-skills/seo-programmatic/SKILL.md +184 -0
  117. package/bundled-skills/seo-schema/SKILL.md +178 -0
  118. package/bundled-skills/seo-sitemap/SKILL.md +129 -0
  119. package/bundled-skills/seo-technical/SKILL.md +175 -0
  120. package/bundled-skills/site-architecture/SKILL.md +366 -0
  121. package/bundled-skills/site-architecture/evals/evals.json +88 -0
  122. package/bundled-skills/site-architecture/references/mermaid-templates.md +216 -0
  123. package/bundled-skills/site-architecture/references/navigation-patterns.md +305 -0
  124. package/bundled-skills/site-architecture/references/site-type-templates.md +293 -0
  125. package/package.json +1 -1
@@ -0,0 +1,313 @@
1
+ # Claude API — TypeScript
2
+
3
+ ## Installation
4
+
5
+ ```bash
6
+ npm install @anthropic-ai/sdk
7
+ ```
8
+
9
+ ## Client Initialization
10
+
11
+ ```typescript
12
+ import Anthropic from "@anthropic-ai/sdk";
13
+
14
+ // Default (uses ANTHROPIC_API_KEY env var)
15
+ const client = new Anthropic();
16
+
17
+ // Explicit API key
18
+ const client = new Anthropic({ apiKey: "your-api-key" });
19
+ ```
20
+
21
+ ---
22
+
23
+ ## Basic Message Request
24
+
25
+ ```typescript
26
+ const response = await client.messages.create({
27
+ model: "claude-opus-4-6",
28
+ max_tokens: 1024,
29
+ messages: [{ role: "user", content: "What is the capital of France?" }],
30
+ });
31
+ console.log(response.content[0].text);
32
+ ```
33
+
34
+ ---
35
+
36
+ ## System Prompts
37
+
38
+ ```typescript
39
+ const response = await client.messages.create({
40
+ model: "claude-opus-4-6",
41
+ max_tokens: 1024,
42
+ system:
43
+ "You are a helpful coding assistant. Always provide examples in Python.",
44
+ messages: [{ role: "user", content: "How do I read a JSON file?" }],
45
+ });
46
+ ```
47
+
48
+ ---
49
+
50
+ ## Vision (Images)
51
+
52
+ ### URL
53
+
54
+ ```typescript
55
+ const response = await client.messages.create({
56
+ model: "claude-opus-4-6",
57
+ max_tokens: 1024,
58
+ messages: [
59
+ {
60
+ role: "user",
61
+ content: [
62
+ {
63
+ type: "image",
64
+ source: { type: "url", url: "https://example.com/image.png" },
65
+ },
66
+ { type: "text", text: "Describe this image" },
67
+ ],
68
+ },
69
+ ],
70
+ });
71
+ ```
72
+
73
+ ### Base64
74
+
75
+ ```typescript
76
+ import fs from "fs";
77
+
78
+ const imageData = fs.readFileSync("image.png").toString("base64");
79
+
80
+ const response = await client.messages.create({
81
+ model: "claude-opus-4-6",
82
+ max_tokens: 1024,
83
+ messages: [
84
+ {
85
+ role: "user",
86
+ content: [
87
+ {
88
+ type: "image",
89
+ source: { type: "base64", media_type: "image/png", data: imageData },
90
+ },
91
+ { type: "text", text: "What's in this image?" },
92
+ ],
93
+ },
94
+ ],
95
+ });
96
+ ```
97
+
98
+ ---
99
+
100
+ ## Prompt Caching
101
+
102
+ ### Automatic Caching (Recommended)
103
+
104
+ Use top-level `cache_control` to automatically cache the last cacheable block in the request:
105
+
106
+ ```typescript
107
+ const response = await client.messages.create({
108
+ model: "claude-opus-4-6",
109
+ max_tokens: 1024,
110
+ cache_control: { type: "ephemeral" }, // auto-caches the last cacheable block
111
+ system: "You are an expert on this large document...",
112
+ messages: [{ role: "user", content: "Summarize the key points" }],
113
+ });
114
+ ```
115
+
116
+ ### Manual Cache Control
117
+
118
+ For fine-grained control, add `cache_control` to specific content blocks:
119
+
120
+ ```typescript
121
+ const response = await client.messages.create({
122
+ model: "claude-opus-4-6",
123
+ max_tokens: 1024,
124
+ system: [
125
+ {
126
+ type: "text",
127
+ text: "You are an expert on this large document...",
128
+ cache_control: { type: "ephemeral" }, // default TTL is 5 minutes
129
+ },
130
+ ],
131
+ messages: [{ role: "user", content: "Summarize the key points" }],
132
+ });
133
+
134
+ // With explicit TTL (time-to-live)
135
+ const response2 = await client.messages.create({
136
+ model: "claude-opus-4-6",
137
+ max_tokens: 1024,
138
+ system: [
139
+ {
140
+ type: "text",
141
+ text: "You are an expert on this large document...",
142
+ cache_control: { type: "ephemeral", ttl: "1h" }, // 1 hour TTL
143
+ },
144
+ ],
145
+ messages: [{ role: "user", content: "Summarize the key points" }],
146
+ });
147
+ ```
148
+
149
+ ---
150
+
151
+ ## Extended Thinking
152
+
153
+ > **Opus 4.6 and Sonnet 4.6:** Use adaptive thinking. `budget_tokens` is deprecated on both Opus 4.6 and Sonnet 4.6.
154
+ > **Older models:** Use `thinking: {type: "enabled", budget_tokens: N}` (must be < `max_tokens`, min 1024).
155
+
156
+ ```typescript
157
+ // Opus 4.6: adaptive thinking (recommended)
158
+ const response = await client.messages.create({
159
+ model: "claude-opus-4-6",
160
+ max_tokens: 16000,
161
+ thinking: { type: "adaptive" },
162
+ output_config: { effort: "high" }, // low | medium | high | max
163
+ messages: [
164
+ { role: "user", content: "Solve this math problem step by step..." },
165
+ ],
166
+ });
167
+
168
+ for (const block of response.content) {
169
+ if (block.type === "thinking") {
170
+ console.log("Thinking:", block.thinking);
171
+ } else if (block.type === "text") {
172
+ console.log("Response:", block.text);
173
+ }
174
+ }
175
+ ```
176
+
177
+ ---
178
+
179
+ ## Error Handling
180
+
181
+ Use the SDK's typed exception classes — never check error messages with string matching:
182
+
183
+ ```typescript
184
+ import Anthropic from "@anthropic-ai/sdk";
185
+
186
+ try {
187
+ const response = await client.messages.create({...});
188
+ } catch (error) {
189
+ if (error instanceof Anthropic.BadRequestError) {
190
+ console.error("Bad request:", error.message);
191
+ } else if (error instanceof Anthropic.AuthenticationError) {
192
+ console.error("Invalid API key");
193
+ } else if (error instanceof Anthropic.RateLimitError) {
194
+ console.error("Rate limited - retry later");
195
+ } else if (error instanceof Anthropic.APIError) {
196
+ console.error(`API error ${error.status}:`, error.message);
197
+ }
198
+ }
199
+ ```
200
+
201
+ All classes extend `Anthropic.APIError` with a typed `status` field. Check from most specific to least specific. See [shared/error-codes.md](../../shared/error-codes.md) for the full error code reference.
202
+
203
+ ---
204
+
205
+ ## Multi-Turn Conversations
206
+
207
+ The API is stateless — send the full conversation history each time. Use `Anthropic.MessageParam[]` to type the messages array:
208
+
209
+ ```typescript
210
+ const messages: Anthropic.MessageParam[] = [
211
+ { role: "user", content: "My name is Alice." },
212
+ { role: "assistant", content: "Hello Alice! Nice to meet you." },
213
+ { role: "user", content: "What's my name?" },
214
+ ];
215
+
216
+ const response = await client.messages.create({
217
+ model: "claude-opus-4-6",
218
+ max_tokens: 1024,
219
+ messages: messages,
220
+ });
221
+ ```
222
+
223
+ **Rules:**
224
+
225
+ - Messages must alternate between `user` and `assistant`
226
+ - First message must be `user`
227
+ - Use SDK types (`Anthropic.MessageParam`, `Anthropic.Message`, `Anthropic.Tool`, etc.) for all API data structures — don't redefine equivalent interfaces
228
+
229
+ ---
230
+
231
+ ### Compaction (long conversations)
232
+
233
+ > **Beta, Opus 4.6 only.** When conversations approach the 200K context window, compaction automatically summarizes earlier context server-side. The API returns a `compaction` block; you must pass it back on subsequent requests — append `response.content`, not just the text.
234
+
235
+ ```typescript
236
+ import Anthropic from "@anthropic-ai/sdk";
237
+
238
+ const client = new Anthropic();
239
+ const messages: Anthropic.Beta.BetaMessageParam[] = [];
240
+
241
+ async function chat(userMessage: string): Promise<string> {
242
+ messages.push({ role: "user", content: userMessage });
243
+
244
+ const response = await client.beta.messages.create({
245
+ betas: ["compact-2026-01-12"],
246
+ model: "claude-opus-4-6",
247
+ max_tokens: 4096,
248
+ messages,
249
+ context_management: {
250
+ edits: [{ type: "compact_20260112" }],
251
+ },
252
+ });
253
+
254
+ // Append full content — compaction blocks must be preserved
255
+ messages.push({ role: "assistant", content: response.content });
256
+
257
+ const textBlock = response.content.find((block) => block.type === "text");
258
+ return textBlock?.text ?? "";
259
+ }
260
+
261
+ // Compaction triggers automatically when context grows large
262
+ console.log(await chat("Help me build a Python web scraper"));
263
+ console.log(await chat("Add support for JavaScript-rendered pages"));
264
+ console.log(await chat("Now add rate limiting and error handling"));
265
+ ```
266
+
267
+ ---
268
+
269
+ ## Stop Reasons
270
+
271
+ The `stop_reason` field in the response indicates why the model stopped generating:
272
+
273
+ | Value | Meaning |
274
+ | --------------- | --------------------------------------------------------------- |
275
+ | `end_turn` | Claude finished its response naturally |
276
+ | `max_tokens` | Hit the `max_tokens` limit — increase it or use streaming |
277
+ | `stop_sequence` | Hit a custom stop sequence |
278
+ | `tool_use` | Claude wants to call a tool — execute it and continue |
279
+ | `pause_turn` | Model paused and can be resumed (agentic flows) |
280
+ | `refusal` | Claude refused for safety reasons — output may not match schema |
281
+
282
+ ---
283
+
284
+ ## Cost Optimization Strategies
285
+
286
+ ### 1. Use Prompt Caching for Repeated Context
287
+
288
+ ```typescript
289
+ // Automatic caching (simplest — caches the last cacheable block)
290
+ const response = await client.messages.create({
291
+ model: "claude-opus-4-6",
292
+ max_tokens: 1024,
293
+ cache_control: { type: "ephemeral" },
294
+ system: largeDocumentText, // e.g., 50KB of context
295
+ messages: [{ role: "user", content: "Summarize the key points" }],
296
+ });
297
+
298
+ // First request: full cost
299
+ // Subsequent requests: ~90% cheaper for cached portion
300
+ ```
301
+
302
+ ### 2. Use Token Counting Before Requests
303
+
304
+ ```typescript
305
+ const countResponse = await client.messages.countTokens({
306
+ model: "claude-opus-4-6",
307
+ messages: messages,
308
+ system: system,
309
+ });
310
+
311
+ const estimatedInputCost = countResponse.input_tokens * 0.000005; // $5/1M tokens
312
+ console.log(`Estimated input cost: $${estimatedInputCost.toFixed(4)}`);
313
+ ```
@@ -0,0 +1,106 @@
1
+ # Message Batches API — TypeScript
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
+ ```typescript
18
+ import Anthropic from "@anthropic-ai/sdk";
19
+
20
+ const client = new Anthropic();
21
+
22
+ const messageBatch = await client.messages.batches.create({
23
+ requests: [
24
+ {
25
+ custom_id: "request-1",
26
+ params: {
27
+ model: "claude-opus-4-6",
28
+ max_tokens: 1024,
29
+ messages: [
30
+ { role: "user", content: "Summarize climate change impacts" },
31
+ ],
32
+ },
33
+ },
34
+ {
35
+ custom_id: "request-2",
36
+ params: {
37
+ model: "claude-opus-4-6",
38
+ max_tokens: 1024,
39
+ messages: [
40
+ { role: "user", content: "Explain quantum computing basics" },
41
+ ],
42
+ },
43
+ },
44
+ ],
45
+ });
46
+
47
+ console.log(`Batch ID: ${messageBatch.id}`);
48
+ console.log(`Status: ${messageBatch.processing_status}`);
49
+ ```
50
+
51
+ ---
52
+
53
+ ## Poll for Completion
54
+
55
+ ```typescript
56
+ let batch;
57
+ while (true) {
58
+ batch = await client.messages.batches.retrieve(messageBatch.id);
59
+ if (batch.processing_status === "ended") break;
60
+ console.log(
61
+ `Status: ${batch.processing_status}, processing: ${batch.request_counts.processing}`,
62
+ );
63
+ await new Promise((resolve) => setTimeout(resolve, 60_000));
64
+ }
65
+
66
+ console.log("Batch complete!");
67
+ console.log(`Succeeded: ${batch.request_counts.succeeded}`);
68
+ console.log(`Errored: ${batch.request_counts.errored}`);
69
+ ```
70
+
71
+ ---
72
+
73
+ ## Retrieve Results
74
+
75
+ ```typescript
76
+ for await (const result of await client.messages.batches.results(
77
+ messageBatch.id,
78
+ )) {
79
+ switch (result.result.type) {
80
+ case "succeeded":
81
+ console.log(
82
+ `[${result.custom_id}] ${result.result.message.content[0].text.slice(0, 100)}`,
83
+ );
84
+ break;
85
+ case "errored":
86
+ if (result.result.error.type === "invalid_request") {
87
+ console.log(`[${result.custom_id}] Validation error - fix and retry`);
88
+ } else {
89
+ console.log(`[${result.custom_id}] Server error - safe to retry`);
90
+ }
91
+ break;
92
+ case "expired":
93
+ console.log(`[${result.custom_id}] Expired - resubmit`);
94
+ break;
95
+ }
96
+ }
97
+ ```
98
+
99
+ ---
100
+
101
+ ## Cancel a Batch
102
+
103
+ ```typescript
104
+ const cancelled = await client.messages.batches.cancel(messageBatch.id);
105
+ console.log(`Status: ${cancelled.processing_status}`); // "canceling"
106
+ ```
@@ -0,0 +1,98 @@
1
+ # Files API — TypeScript
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
+ ```typescript
20
+ import Anthropic, { toFile } from "@anthropic-ai/sdk";
21
+ import fs from "fs";
22
+
23
+ const client = new Anthropic();
24
+
25
+ const uploaded = await client.beta.files.upload({
26
+ file: await toFile(fs.createReadStream("report.pdf"), undefined, {
27
+ type: "application/pdf",
28
+ }),
29
+ betas: ["files-api-2025-04-14"],
30
+ });
31
+
32
+ console.log(`File ID: ${uploaded.id}`);
33
+ console.log(`Size: ${uploaded.size_bytes} bytes`);
34
+ ```
35
+
36
+ ---
37
+
38
+ ## Use a File in Messages
39
+
40
+ ### PDF / Text Document
41
+
42
+ ```typescript
43
+ const response = await client.beta.messages.create({
44
+ model: "claude-opus-4-6",
45
+ max_tokens: 1024,
46
+ messages: [
47
+ {
48
+ role: "user",
49
+ content: [
50
+ { type: "text", text: "Summarize the key findings in this report." },
51
+ {
52
+ type: "document",
53
+ source: { type: "file", file_id: uploaded.id },
54
+ title: "Q4 Report",
55
+ citations: { enabled: true },
56
+ },
57
+ ],
58
+ },
59
+ ],
60
+ betas: ["files-api-2025-04-14"],
61
+ });
62
+
63
+ console.log(response.content[0].text);
64
+ ```
65
+
66
+ ---
67
+
68
+ ## Manage Files
69
+
70
+ ### List Files
71
+
72
+ ```typescript
73
+ const files = await client.beta.files.list({
74
+ betas: ["files-api-2025-04-14"],
75
+ });
76
+ for (const f of files.data) {
77
+ console.log(`${f.id}: ${f.filename} (${f.size_bytes} bytes)`);
78
+ }
79
+ ```
80
+
81
+ ### Delete a File
82
+
83
+ ```typescript
84
+ await client.beta.files.delete("file_011CNha8iCJcU1wXNR6q4V8w", {
85
+ betas: ["files-api-2025-04-14"],
86
+ });
87
+ ```
88
+
89
+ ### Download a File
90
+
91
+ ```typescript
92
+ const response = await client.beta.files.download(
93
+ "file_011CNha8iCJcU1wXNR6q4V8w",
94
+ { betas: ["files-api-2025-04-14"] },
95
+ );
96
+ const content = Buffer.from(await response.arrayBuffer());
97
+ await fs.promises.writeFile("output.txt", content);
98
+ ```
@@ -0,0 +1,178 @@
1
+ # Streaming — TypeScript
2
+
3
+ ## Quick Start
4
+
5
+ ```typescript
6
+ const stream = client.messages.stream({
7
+ model: "claude-opus-4-6",
8
+ max_tokens: 1024,
9
+ messages: [{ role: "user", content: "Write a story" }],
10
+ });
11
+
12
+ for await (const event of stream) {
13
+ if (
14
+ event.type === "content_block_delta" &&
15
+ event.delta.type === "text_delta"
16
+ ) {
17
+ process.stdout.write(event.delta.text);
18
+ }
19
+ }
20
+ ```
21
+
22
+ ---
23
+
24
+ ## Handling Different Content Types
25
+
26
+ > **Opus 4.6:** Use `thinking: {type: "adaptive"}`. On older models, use `thinking: {type: "enabled", budget_tokens: N}` instead.
27
+
28
+ ```typescript
29
+ const stream = client.messages.stream({
30
+ model: "claude-opus-4-6",
31
+ max_tokens: 16000,
32
+ thinking: { type: "adaptive" },
33
+ messages: [{ role: "user", content: "Analyze this problem" }],
34
+ });
35
+
36
+ for await (const event of stream) {
37
+ switch (event.type) {
38
+ case "content_block_start":
39
+ switch (event.content_block.type) {
40
+ case "thinking":
41
+ console.log("\n[Thinking...]");
42
+ break;
43
+ case "text":
44
+ console.log("\n[Response:]");
45
+ break;
46
+ }
47
+ break;
48
+ case "content_block_delta":
49
+ switch (event.delta.type) {
50
+ case "thinking_delta":
51
+ process.stdout.write(event.delta.thinking);
52
+ break;
53
+ case "text_delta":
54
+ process.stdout.write(event.delta.text);
55
+ break;
56
+ }
57
+ break;
58
+ }
59
+ }
60
+ ```
61
+
62
+ ---
63
+
64
+ ## Streaming with Tool Use (Tool Runner)
65
+
66
+ Use the tool runner with `stream: true`. The outer loop iterates over tool runner iterations (messages), the inner loop processes stream events:
67
+
68
+ ```typescript
69
+ import Anthropic from "@anthropic-ai/sdk";
70
+ import { betaZodTool } from "@anthropic-ai/sdk/helpers/beta/zod";
71
+ import { z } from "zod";
72
+
73
+ const client = new Anthropic();
74
+
75
+ const getWeather = betaZodTool({
76
+ name: "get_weather",
77
+ description: "Get current weather for a location",
78
+ inputSchema: z.object({
79
+ location: z.string().describe("City and state, e.g., San Francisco, CA"),
80
+ }),
81
+ run: async ({ location }) => `72°F and sunny in ${location}`,
82
+ });
83
+
84
+ const runner = client.beta.messages.toolRunner({
85
+ model: "claude-opus-4-6",
86
+ max_tokens: 4096,
87
+ tools: [getWeather],
88
+ messages: [
89
+ { role: "user", content: "What's the weather in Paris and London?" },
90
+ ],
91
+ stream: true,
92
+ });
93
+
94
+ // Outer loop: each tool runner iteration
95
+ for await (const messageStream of runner) {
96
+ // Inner loop: stream events for this iteration
97
+ for await (const event of messageStream) {
98
+ switch (event.type) {
99
+ case "content_block_delta":
100
+ switch (event.delta.type) {
101
+ case "text_delta":
102
+ process.stdout.write(event.delta.text);
103
+ break;
104
+ case "input_json_delta":
105
+ // Tool input being streamed
106
+ break;
107
+ }
108
+ break;
109
+ }
110
+ }
111
+ }
112
+ ```
113
+
114
+ ---
115
+
116
+ ## Getting the Final Message
117
+
118
+ ```typescript
119
+ const stream = client.messages.stream({
120
+ model: "claude-opus-4-6",
121
+ max_tokens: 1024,
122
+ messages: [{ role: "user", content: "Hello" }],
123
+ });
124
+
125
+ for await (const event of stream) {
126
+ // Process events...
127
+ }
128
+
129
+ const finalMessage = await stream.finalMessage();
130
+ console.log(`Tokens used: ${finalMessage.usage.output_tokens}`);
131
+ ```
132
+
133
+ ---
134
+
135
+ ## Stream Event Types
136
+
137
+ | Event Type | Description | When it fires |
138
+ | --------------------- | --------------------------- | --------------------------------- |
139
+ | `message_start` | Contains message metadata | Once at the beginning |
140
+ | `content_block_start` | New content block beginning | When a text/tool_use block starts |
141
+ | `content_block_delta` | Incremental content update | For each token/chunk |
142
+ | `content_block_stop` | Content block complete | When a block finishes |
143
+ | `message_delta` | Message-level updates | Contains `stop_reason`, usage |
144
+ | `message_stop` | Message complete | Once at the end |
145
+
146
+ ## Best Practices
147
+
148
+ 1. **Always flush output** — Use `process.stdout.write()` for immediate display
149
+ 2. **Handle partial responses** — If the stream is interrupted, you may have incomplete content
150
+ 3. **Track token usage** — The `message_delta` event contains usage information
151
+ 4. **Use `finalMessage()`** — Get the complete `Anthropic.Message` object even when streaming. Don't wrap `.on()` events in `new Promise()` — `finalMessage()` handles all completion/error/abort states internally
152
+ 5. **Buffer for web UIs** — Consider buffering a few tokens before rendering to avoid excessive DOM updates
153
+ 6. **Use `stream.on("text", ...)` for deltas** — The `text` event provides just the delta string, simpler than manually filtering `content_block_delta` events
154
+ 7. **For agentic loops with streaming** — See the [Streaming Manual Loop](./tool-use.md#streaming-manual-loop) section in tool-use.md for combining `stream()` + `finalMessage()` with a tool-use loop
155
+
156
+ ## Raw SSE Format
157
+
158
+ If using raw HTTP (not SDKs), the stream returns Server-Sent Events:
159
+
160
+ ```
161
+ event: message_start
162
+ data: {"type":"message_start","message":{"id":"msg_...","type":"message",...}}
163
+
164
+ event: content_block_start
165
+ data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
166
+
167
+ event: content_block_delta
168
+ data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}
169
+
170
+ event: content_block_stop
171
+ data: {"type":"content_block_stop","index":0}
172
+
173
+ event: message_delta
174
+ data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":12}}
175
+
176
+ event: message_stop
177
+ data: {"type":"message_stop"}
178
+ ```