$npx -y skills add agents-inc/skills --skill ai-infrastructure-replicateReplicate SDK patterns for TypeScript/Node.js -- client setup, predictions, streaming, webhooks, file handling, model versioning, deployments, and training
| 1 | # Replicate SDK Patterns |
| 2 | |
| 3 | > **Quick Guide:** Use the `replicate` npm package to run open-source ML models on serverless GPUs. Use `replicate.run()` for synchronous execution that returns output directly, `replicate.stream()` for SSE-based streaming, or `replicate.predictions.create()` for async background jobs with webhook notifications. Models are referenced as `owner/model` (uses latest version) or `owner/model:version` (pinned). File outputs are `FileOutput` objects implementing `ReadableStream`. Cold starts are expected for infrequently-used models -- use deployments with `min_instances` to keep models warm. |
| 4 | |
| 5 | --- |
| 6 | |
| 7 | <critical_requirements> |
| 8 | |
| 9 | ## CRITICAL: Before Using This Skill |
| 10 | |
| 11 | > **All code must follow project conventions in CLAUDE.md** (kebab-case, named exports, import ordering, `import type`, named constants) |
| 12 | |
| 13 | **(You MUST never hardcode API tokens -- always use environment variables via `process.env.REPLICATE_API_TOKEN`)** |
| 14 | |
| 15 | **(You MUST handle `FileOutput` objects for models that return files -- do not assume outputs are plain strings or URLs)** |
| 16 | |
| 17 | **(You MUST validate webhooks using `validateWebhook()` from the `replicate` package -- never trust unverified webhook payloads)** |
| 18 | |
| 19 | **(You MUST account for cold starts when running infrequently-used models -- use deployments for latency-sensitive applications)** |
| 20 | |
| 21 | **(You MUST specify model versions (`owner/model:version`) in production to ensure reproducible results -- unversioned references use the latest, which can change)** |
| 22 | |
| 23 | </critical_requirements> |
| 24 | |
| 25 | --- |
| 26 | |
| 27 | **Auto-detection:** Replicate, replicate, replicate.run, replicate.stream, replicate.predictions, replicate.deployments, replicate.trainings, replicate.models, FileOutput, validateWebhook, REPLICATE_API_TOKEN, serverless GPU, cold start, webhook_events_filter |
| 28 | |
| 29 | **When to use:** |
| 30 | |
| 31 | - Running open-source ML models (Llama, Stable Diffusion, Whisper, etc.) without managing GPU infrastructure |
| 32 | - Generating images, transcribing audio, running LLMs, or any ML inference via API |
| 33 | - Streaming LLM output in real-time with server-sent events |
| 34 | - Processing predictions asynchronously with webhook notifications |
| 35 | - Fine-tuning models with custom training data |
| 36 | - Running models on dedicated hardware with custom scaling via deployments |
| 37 | |
| 38 | **Key patterns covered:** |
| 39 | |
| 40 | - Client initialization and configuration (auth, user agent, file encoding) |
| 41 | - Running predictions (`replicate.run()`, `replicate.predictions.create()`, `replicate.wait()`) |
| 42 | - Streaming output (`replicate.stream()` with SSE events) |
| 43 | - Model versioning (`owner/model` vs `owner/model:version`) |
| 44 | - File input/output handling (`FileOutput`, file uploads, `Buffer` inputs) |
| 45 | - Webhooks (setup, event filtering, signature validation) |
| 46 | - Deployments (custom hardware, scaling, keeping models warm) |
| 47 | - Training / fine-tuning |
| 48 | |
| 49 | **When NOT to use:** |
| 50 | |
| 51 | - You need a unified multi-provider LLM SDK (OpenAI, Anthropic, Google) -- use a provider-agnostic SDK |
| 52 | - You want to run models locally -- Replicate is a cloud-only serverless platform |
| 53 | - You need sub-second latency guarantees without deployments -- cold starts can take minutes |
| 54 | |
| 55 | --- |
| 56 | |
| 57 | ## Examples Index |
| 58 | |
| 59 | - [Core: Setup, Predictions & Files](examples/core.md) -- Client init, run(), predictions.create(), wait(), file I/O, error handling |
| 60 | - [Streaming & Webhooks](examples/streaming-webhooks.md) -- stream(), SSE events, webhook setup, signature validation |
| 61 | - [Deployments & Training](examples/deployments-training.md) -- Custom hardware, scaling, fine-tuning, model management |
| 62 | - [Quick API Reference](reference.md) -- Method signatures, constructor options, error types, model reference format |
| 63 | |
| 64 | --- |
| 65 | |
| 66 | <philosophy> |
| 67 | |
| 68 | ## Philosophy |
| 69 | |
| 70 | Replicate provides **serverless GPU infrastructure** for running open-source ML models. You send inputs, Replicate allocates GPU hardware, runs the model, and returns outputs. No Docker, no CUDA drivers, no GPU provisioning. |
| 71 | |
| 72 | **Core principles:** |
| 73 | |
| 74 | 1. **Serverless execution** -- Models run on-demand on Replicate's infrastructure. You pay only for compute time. Cold starts are a trade-off for not maintaining always-on GPUs. |
| 75 | 2. **Model marketplace** -- Thousands of community and official models available at `replicate.com/explore`. Run any public model with just its identifier. |
| 76 | 3. **Version pinning for reproducibility** -- Models are versioned with SHA-256 hashes. Pin to a version in production (`owner/model:abc123...`) to guarantee identical behavior across deploys. |
| 77 | 4. **Three execution modes** -- `replicate.run()` for synchronous wait, `replicate.stream()` for real-time SSE output, `replicate.predictions.create()` for fire-and-forget with webhooks. |
| 78 | 5. **File-first I/O** -- Many models accept and produce files (images, audio, video). The SDK handles file uploads automatically and returns `FileOutput` objects for file outputs. |
| 79 | |
| 80 | </philoso |