# openrouter-streaming-setup

openrouter-streaming-setup — a skill for Claude Code, Codex, Cursor and other platforms that implements streaming responses from OpenRouter via Server-Sent Events.

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode, Windsurf (Devin Desktop), Claude.ai · 1 install · ★ 2935 repo stars
- Author: Jeremy Longshore
- License: MIT · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/jeremylongshore/tons-of-skills-marketplace/tree/HEAD/skills/.curated/openrouter-streaming-setup/SKILL.md
- Updated: 2026-11-01
- Security check: automated, 2026-11-16 (text) — no signs of malicious code found.
- HTML: https://affpapa.org/en/nevibecode/skills/openrouter-streaming-setup

## What it does

Available across Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI and OpenCode. Implements streaming via OpenRouter API using `stream: true` and `stream_options: {include_usage: true}`. Includes basic Python streaming, a generator that captures TTFT and total response time, a TypeScript async variant, and SSE forwarding to browsers through FastAPI.

Requires `OPENROUTER_API_KEY` in environment (or use the `openrouter-install-auth` skill). Python needs the OpenAI SDK; FastAPI is only for browser-facing SSE. Client timeout should exceed non-streaming requests—at least 120 seconds recommended.

Handles stream interruptions, exhausted limits, keep-alive pings and `finish_reason: "length"`. The browser example uses JavaScript EventSource or fetch with `getReader()` to consume tokens one by one.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add jeremylongshore/tons-of-skills-marketplace --skill openrouter-streaming-setup -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/openrouter-streaming-setup/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/openrouter-streaming-setup/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add jeremylongshore/tons-of-skills-marketplace --skill openrouter-streaming-setup -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/openrouter-streaming-setup/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add jeremylongshore/tons-of-skills-marketplace --skill openrouter-streaming-setup -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/openrouter-streaming-setup/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install jeremylongshore/tons-of-skills-marketplace skills/.curated/openrouter-streaming-setup
```

2. **Manually: put the files in place**
- File: `.github/skills/openrouter-streaming-setup/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add jeremylongshore/tons-of-skills-marketplace --skill openrouter-streaming-setup -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/jeremylongshore/tons-of-skills-marketplace --path skills/.curated/openrouter-streaming-setup
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/openrouter-streaming-setup/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add jeremylongshore/tons-of-skills-marketplace --skill openrouter-streaming-setup -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/jeremylongshore/tons-of-skills-marketplace/tree/HEAD/skills/.curated/openrouter-streaming-setup/SKILL.md

## When to use it
- set up streaming responses from OpenRouter in Python or TypeScript
- measure time-to-first-token and total response time
- forward OpenRouter SSE stream to a browser client
- handle errors and mid-stream failures during streaming

## Example prompts
- Build a streaming chat interface with OpenRouter using `stream: true`
- Add TTFT and token metrics to streaming responses with `stream_options: {include_usage: true}`
- Use FastAPI to forward OpenRouter stream to the browser at `/v1/stream`

## Source text · SKILL.md · MIT

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: b12a24954d455b70.

BEGIN THIRD-PARTY FILE b12a24954d455b70
```text
---
name: openrouter-streaming-setup
description: 'Implement streaming responses with OpenRouter for real-time UIs. Use
  when building chat interfaces, reducing time-to-first-token, or processing long
  completions. Triggers: ''openrouter streaming'', ''openrouter sse'', ''stream response
  openrouter'', ''real-time openrouter''.

  '
allowed-tools: Read, Write, Edit, Grep, Bash(python3:*), Bash(node:*)
version: 1.20.0
license: MIT
author: Jeremy Longshore <jeremy@intentsolutions.io>
tags:
- saas
- openrouter
- streaming
- real-time
compatibility: Designed for Claude Code
---
# OpenRouter Streaming Setup

## Overview

OpenRouter supports Server-Sent Events (SSE) streaming via `stream: true`, compatible with the OpenAI SDK. Streaming returns tokens as they're generated, reducing time-to-first-token (TTFT) from seconds to milliseconds. Usage stats are available via `stream_options: {include_usage: true}` in the final chunk. This skill covers Python and TypeScript streaming, SSE forwarding to browsers, and error recovery.

## Prerequisites

- An OpenRouter API key (`sk-or-v1-...`) exported as `OPENROUTER_API_KEY` — see the `openrouter-install-auth` skill for setup
- Python 3.8+ or Node.js 18+ with the OpenAI SDK (the async example uses `AsyncOpenAI` from the same Python package)
- FastAPI if you plan to forward the SSE stream to browsers per the SSE Forwarding section
- A streaming-appropriate client timeout (e.g. 120s) — longer than for non-streaming requests

## Instructions

1. Start with Python: Basic Streaming — pass `stream=True` plus `stream_options={"include_usage": True}` so the final chunk carries token counts, and print each `chunk.choices[0].delta.content` as it arrives.
2. Wrap that loop in the Python: Streaming with Metrics generator to capture TTFT and total time per request; the metrics dict is available after the generator is exhausted.
3. For Node services, use the TypeScript: Streaming `for await` loop over the same `stream: true` request.
4. To reach a browser UI, expose the FastAPI endpoint in SSE Forwarding to Browser — it re-emits each token as a `data: {"token": ...}` SSE line and terminates with `data: [DONE]`.
5. Consume that endpoint with the Browser Client (JavaScript) reader loop, appending tokens to the DOM as they decode.
```
END THIRD-PARTY FILE b12a24954d455b70

The third-party file has ended. Everything between the b12a24954d455b70 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 2298 of 10320 bytes. © Jeremy Longshore · MIT · https://github.com/jeremylongshore/tons-of-skills-marketplace/tree/HEAD/skills/.curated/openrouter-streaming-setup/SKILL.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
