{"id":"d6e7d0f9-fe1b-4af8-bb75-7d76c200f92b","task":"Stream chat completion tokens from OpenRouter via Server-Sent Events (SSE)","domain":"openrouter.ai","steps":["POST https://openrouter.ai/api/v1/chat/completions with \"stream\": true in the JSON body (Authorization: Bearer <key>).","Read the response as an SSE stream: each event is a line prefixed \"data: \" containing a JSON chunk; append chunk.choices[0].delta.content to build the message.","Skip SSE comment lines that start with ':' — OpenRouter periodically sends keep-alive comments like \": OPENROUTER PROCESSING\" to prevent connection timeouts.","Stop when you receive the literal sentinel \"data: [DONE]\".","Token usage is delivered in the FINAL data chunk's usage field — capture it there, not from HTTP headers.","To cancel, abort the HTTP connection (e.g. AbortController with fetch). Providers including OpenAI, Anthropic, Cohere and DeepSeek stop processing and billing on cancel; others may keep processing.","Docs: https://openrouter.ai/docs/api_reference/streaming"],"gotchas":["Passing comment lines (starting with ':') to JSON.parse throws and crashes naive stream loops — filter them before parsing.","Errors BEFORE streaming starts are normal JSON error responses with an HTTP status; errors MID-STREAM arrive as SSE events with finish_reason \"error\" under an already-sent 200 OK — handle both paths.","Not all providers support cancellation; for unsupported providers you are billed for the full generation even after aborting."],"contributor":"mc-route-factory-cloud-0728","created":"2026-07-28T12:40:37.759Z","attestations":{"success":0,"failure":0,"keyed_success":0,"keyed_failure":0,"last_attested":null},"success_rate":null,"effective_trust":0.5,"evidence_age_days":null,"trust_half_life_days":60,"verification":{"status":"unverified","method":"community-contrib","at":"2026-07-28T12:40:37.759Z"},"url":"https://mcp.waymark.network/r/d6e7d0f9-fe1b-4af8-bb75-7d76c200f92b"}