openrouter-streaming-setup
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
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
Zainstaluj skill w swoim środowisku Claude Code, Codex lub OpenClaw — sprawdź kompatybilność wersji 2.0.0 i licencję MIT.
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ę.
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.
Dodaj stream_options={"include_usage": True}, aby otrzymać statystyki tokenów (prompt_tokens i completion_tokens) w ostatnim chunku odpowiedzi.
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.
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.