Toolverse
All skills

openrouter-streaming-setup

by jeremylongshore

Implement streaming responses with OpenRouter. Use when building real-time chat interfaces or reducing time-to-first-token. Trigger with phrases like 'openrouter streaming', 'openrouter sse', 'stream response', 'real-time openrouter'.

Installation

Pick a client and clone the repository into its skills directory.

Installation

Quick info

Category
Security
Views
11

About this skill

Implement streaming responses with OpenRouter. Use when building real-time chat interfaces or reducing time-to-first-token. Trigger with phrases like 'openrouter streaming', 'openrouter sse', 'stream response', 'real-time openrouter'.

How to use

  1. Zainstaluj skill w swoim środowisku Claude Code, Codex lub OpenClaw — sprawdź kompatybilność wersji 2.0.0 i licencję MIT.

  2. Skonfiguruj klucz API OpenRouter w zmiennej środowiskowej OPENROUTER_API_KEY oraz ustaw nagłówki HTTP (HTTP-Referer i X-Title) wskazujące na Twoją aplikację.

  3. W kodzie Python zaimportuj bibliotekę OpenAI i utwórz klienta z base_url="https://openrouter.ai/api/v1", następnie wywołaj chat.completions.create() z parametrem stream=True.

  4. Dodaj stream_options={"include_usage": True}, aby otrzymać statystyki tokenów (prompt_tokens i completion_tokens) w ostatnim chunku odpowiedzi.

  5. Iteruj po strumieniu — każdy chunk zawiera fragment treści w choices[0].delta.content; wypisuj tokeny na bieżąco za pomocą print(token, end="", flush=True), aby użytkownik widział odpowiedź w czasie rzeczywistym.

  6. Obsłuż ostatni chunk, który zawiera dane użycia (chunk.usage) — wyświetl liczbę tokenów wejściowych i wyjściowych, aby śledzić koszty API.

Related skills