[{"data":1,"prerenderedAt":848},["ShallowReactive",2],{"docs-nav-titles":3,"page-docs_en-\u002Fguides\u002Fstreaming":52},{"en":4,"id":39},{"\u002Fauthentication":5,"\u002Fchangelog":6,"\u002Fguides\u002Fbilling":7,"\u002Fguides\u002Ferrors":8,"\u002Fguides\u002Fjson-output":9,"\u002Fguides\u002Frate-limits":10,"\u002Fguides\u002Freasoning":11,"\u002Fguides\u002Fstreaming":12,"\u002Fguides\u002Ftool-calling":13,"\u002Fguides\u002Ftop-up":14,"\u002F":15,"\u002Fintegrations\u002Faider":16,"\u002Fintegrations\u002Fclaude-code":17,"\u002Fintegrations\u002Fcline":18,"\u002Fintegrations\u002Fcontinue":19,"\u002Fintegrations\u002Fgoose":20,"\u002Fintegrations\u002Fhermes":21,"\u002Fintegrations":22,"\u002Fintegrations\u002Fkilo-code":23,"\u002Fintegrations\u002Flangchain":24,"\u002Fintegrations\u002Foh-my-pi":25,"\u002Fintegrations\u002Fopenai-sdk":26,"\u002Fintegrations\u002Fopenclaw":27,"\u002Fintegrations\u002Fopencode":28,"\u002Fintegrations\u002Fpi":29,"\u002Fintegrations\u002Fvercel-ai-sdk":30,"\u002Fintegrations\u002Fzed":31,"\u002Fmodels":32,"\u002Fquickstart":33,"\u002Freference\u002Fchat-completions":34,"\u002Freference":22,"\u002Freference\u002Fmessages":35,"\u002Freference\u002Fmodels":36,"\u002Freference\u002Fmodels-retrieve":37,"\u002Fsupport":38},"Authentication","Changelog","Billing","Errors & retries","JSON output","Rate limits","Reasoning content","Streaming","Tool calling","Topping up with QRIS","Introduction","Aider","Claude Code","Cline","Continue","Goose","Hermes Agent","Overview","Kilo Code","LangChain (Python)","Oh My Pi","OpenAI SDKs","OpenClaw","OpenCode","Pi","Vercel AI SDK","Zed","Models","Quickstart","Chat completions","Messages","List models","Retrieve a model","Support",{"\u002Fauthentication":40,"\u002Fchangelog":6,"\u002Fguides\u002Fbilling":7,"\u002Fguides\u002Ferrors":41,"\u002Fguides\u002Fjson-output":42,"\u002Fguides\u002Frate-limits":43,"\u002Fguides\u002Freasoning":44,"\u002Fguides\u002Fstreaming":12,"\u002Fguides\u002Ftool-calling":13,"\u002Fguides\u002Ftop-up":45,"\u002F":46,"\u002Fintegrations\u002Faider":16,"\u002Fintegrations\u002Fclaude-code":17,"\u002Fintegrations\u002Fcline":18,"\u002Fintegrations\u002Fcontinue":19,"\u002Fintegrations\u002Fgoose":20,"\u002Fintegrations\u002Fhermes":21,"\u002Fintegrations":47,"\u002Fintegrations\u002Fkilo-code":23,"\u002Fintegrations\u002Flangchain":24,"\u002Fintegrations\u002Foh-my-pi":25,"\u002Fintegrations\u002Fopenai-sdk":48,"\u002Fintegrations\u002Fopenclaw":27,"\u002Fintegrations\u002Fopencode":28,"\u002Fintegrations\u002Fpi":29,"\u002Fintegrations\u002Fvercel-ai-sdk":30,"\u002Fintegrations\u002Fzed":31,"\u002Fmodels":49,"\u002Fquickstart":33,"\u002Freference\u002Fchat-completions":34,"\u002Freference":47,"\u002Freference\u002Fmessages":35,"\u002Freference\u002Fmodels":50,"\u002Freference\u002Fmodels-retrieve":51,"\u002Fsupport":38},"Autentikasi","Error & retry","Output JSON","Rate limit","Konten reasoning","Isi saldo dengan QRIS","Pengantar","Ringkasan","OpenAI SDK","Model","Daftar model","Ambil satu model",{"id":53,"title":12,"body":54,"description":839,"extension":840,"eyebrow":841,"meta":842,"navTitle":841,"navigation":136,"path":843,"seo":844,"stem":845,"updatedAt":846,"__hash__":847},"docs_en\u002Fguides\u002Fstreaming.md",{"type":55,"value":56,"toc":833},"minimark",[57,86,91,691,695,703,741,745,770,774,814,829],[58,59,60,61,65,66,73,74,77,78,81,82,85],"p",{},"Set ",[62,63,64],"code",{},"\"stream\": true"," to receive the answer as it is generated. The response is a stream of ",[67,68,72],"a",{"href":69,"rel":70},"https:\u002F\u002Fhtml.spec.whatwg.org\u002Fmultipage\u002Fserver-sent-events.html",[71],"nofollow","Server-Sent Events"," (",[62,75,76],{},"text\u002Fevent-stream",") in the OpenAI format: each event is a ",[62,79,80],{},"chat.completion.chunk",", and the stream ends with ",[62,83,84],{},"data: [DONE]",".",[87,88,90],"h2",{"id":89},"stream-a-response","Stream a response",[92,93,94,397,613],"code-group",{},[95,96,102],"pre",{"className":97,"code":98,"filename":99,"language":100,"meta":101,"style":101},"language-python shiki shiki-themes laras-ink","import os\nfrom openai import OpenAI\n\nclient = OpenAI(\n    base_url=\"https:\u002F\u002Fapi.larasrouter.com\u002Fv1\",\n    api_key=os.environ[\"LARAS_API_KEY\"],\n)\n\nstream = client.chat.completions.create(\n    model=\"deepseek-v4.1-flash\",\n    messages=[{\"role\": \"user\", \"content\": \"Write a haiku about Jakarta rain.\"}],\n    stream=True,\n    stream_options={\"include_usage\": True},\n)\n\nfor chunk in stream:\n    if chunk.choices and chunk.choices[0].delta.content:\n        print(chunk.choices[0].delta.content, end=\"\", flush=True)\n    if chunk.usage:\n        print(f\"\\n\\n{chunk.usage.total_tokens} tokens\")\n","Python","python","",[62,103,104,117,131,138,154,168,185,191,196,212,225,259,272,293,298,303,318,339,367,375],{"__ignoreMap":101},[105,106,109,113],"span",{"class":107,"line":108},"line",1,[105,110,112],{"class":111},"sifj9","import",[105,114,116],{"class":115},"snt3o"," os\n",[105,118,120,123,126,128],{"class":107,"line":119},2,[105,121,122],{"class":111},"from",[105,124,125],{"class":115}," openai ",[105,127,112],{"class":111},[105,129,130],{"class":115}," OpenAI\n",[105,132,134],{"class":107,"line":133},3,[105,135,137],{"emptyLinePlaceholder":136},true,"\n",[105,139,141,144,147,151],{"class":107,"line":140},4,[105,142,143],{"class":115},"client ",[105,145,146],{"class":111},"=",[105,148,150],{"class":149},"sIGQv"," OpenAI",[105,152,153],{"class":115},"(\n",[105,155,157,160,162,165],{"class":107,"line":156},5,[105,158,159],{"class":115},"    base_url",[105,161,146],{"class":111},[105,163,164],{"class":149},"\"https:\u002F\u002Fapi.larasrouter.com\u002Fv1\"",[105,166,167],{"class":115},",\n",[105,169,171,174,176,179,182],{"class":107,"line":170},6,[105,172,173],{"class":115},"    api_key",[105,175,146],{"class":111},[105,177,178],{"class":115},"os.environ[",[105,180,181],{"class":149},"\"LARAS_API_KEY\"",[105,183,184],{"class":115},"],\n",[105,186,188],{"class":107,"line":187},7,[105,189,190],{"class":115},")\n",[105,192,194],{"class":107,"line":193},8,[105,195,137],{"emptyLinePlaceholder":136},[105,197,199,202,204,207,210],{"class":107,"line":198},9,[105,200,201],{"class":115},"stream ",[105,203,146],{"class":111},[105,205,206],{"class":115}," client.chat.completions.",[105,208,209],{"class":149},"create",[105,211,153],{"class":115},[105,213,215,218,220,223],{"class":107,"line":214},10,[105,216,217],{"class":115},"    model",[105,219,146],{"class":111},[105,221,222],{"class":149},"\"deepseek-v4.1-flash\"",[105,224,167],{"class":115},[105,226,228,231,233,236,239,242,245,248,251,253,256],{"class":107,"line":227},11,[105,229,230],{"class":115},"    messages",[105,232,146],{"class":111},[105,234,235],{"class":115},"[{",[105,237,238],{"class":149},"\"role\"",[105,240,241],{"class":115},": ",[105,243,244],{"class":149},"\"user\"",[105,246,247],{"class":115},", ",[105,249,250],{"class":149},"\"content\"",[105,252,241],{"class":115},[105,254,255],{"class":149},"\"Write a haiku about Jakarta rain.\"",[105,257,258],{"class":115},"}],\n",[105,260,262,265,267,270],{"class":107,"line":261},12,[105,263,264],{"class":115},"    stream",[105,266,146],{"class":111},[105,268,269],{"class":149},"True",[105,271,167],{"class":115},[105,273,275,278,280,283,286,288,290],{"class":107,"line":274},13,[105,276,277],{"class":115},"    stream_options",[105,279,146],{"class":111},[105,281,282],{"class":115},"{",[105,284,285],{"class":149},"\"include_usage\"",[105,287,241],{"class":115},[105,289,269],{"class":149},[105,291,292],{"class":115},"},\n",[105,294,296],{"class":107,"line":295},14,[105,297,190],{"class":115},[105,299,301],{"class":107,"line":300},15,[105,302,137],{"emptyLinePlaceholder":136},[105,304,306,309,312,315],{"class":107,"line":305},16,[105,307,308],{"class":111},"for",[105,310,311],{"class":115}," chunk ",[105,313,314],{"class":111},"in",[105,316,317],{"class":115}," stream:\n",[105,319,321,324,327,330,333,336],{"class":107,"line":320},17,[105,322,323],{"class":111},"    if",[105,325,326],{"class":115}," chunk.choices ",[105,328,329],{"class":111},"and",[105,331,332],{"class":115}," chunk.choices[",[105,334,335],{"class":149},"0",[105,337,338],{"class":115},"].delta.content:\n",[105,340,342,345,348,350,353,355,358,361,363,365],{"class":107,"line":341},18,[105,343,344],{"class":111},"        print",[105,346,347],{"class":115},"(chunk.choices[",[105,349,335],{"class":149},[105,351,352],{"class":115},"].delta.content, end",[105,354,146],{"class":111},[105,356,357],{"class":149},"\"\"",[105,359,360],{"class":115},", flush",[105,362,146],{"class":111},[105,364,269],{"class":149},[105,366,190],{"class":115},[105,368,370,372],{"class":107,"line":369},19,[105,371,323],{"class":111},[105,373,374],{"class":115}," chunk.usage:\n",[105,376,378,380,383,386,389,392,395],{"class":107,"line":377},20,[105,379,344],{"class":111},[105,381,382],{"class":115},"(",[105,384,385],{"class":111},"f",[105,387,388],{"class":149},"\"",[105,390,391],{"class":115},"\\n\\n{chunk.usage.total_tokens}",[105,393,394],{"class":149}," tokens\"",[105,396,190],{"class":115},[95,398,403],{"className":399,"code":400,"filename":401,"language":402,"meta":101,"style":101},"language-typescript shiki shiki-themes laras-ink","import OpenAI from \"openai\";\n\nconst client = new OpenAI({\n  baseURL: \"https:\u002F\u002Fapi.larasrouter.com\u002Fv1\",\n  apiKey: process.env.LARAS_API_KEY,\n});\n\nconst stream = await client.chat.completions.create({\n  model: \"deepseek-v4.1-flash\",\n  messages: [{ role: \"user\", content: \"Write a haiku about Jakarta rain.\" }],\n  stream: true,\n  stream_options: { include_usage: true },\n});\n\nfor await (const chunk of stream) {\n  const text = chunk.choices[0]?.delta?.content;\n  if (text) process.stdout.write(text);\n  if (chunk.usage) console.log(`\\n\\n${chunk.usage.total_tokens} tokens`);\n}\n","Node.js","typescript",[62,404,405,420,424,442,451,456,461,465,483,492,507,517,527,531,535,553,570,584,608],{"__ignoreMap":101},[105,406,407,409,412,414,417],{"class":107,"line":108},[105,408,112],{"class":111},[105,410,411],{"class":115}," OpenAI ",[105,413,122],{"class":111},[105,415,416],{"class":149}," \"openai\"",[105,418,419],{"class":115},";\n",[105,421,422],{"class":107,"line":119},[105,423,137],{"emptyLinePlaceholder":136},[105,425,426,429,432,434,437,439],{"class":107,"line":133},[105,427,428],{"class":111},"const",[105,430,431],{"class":115}," client ",[105,433,146],{"class":111},[105,435,436],{"class":111}," new",[105,438,150],{"class":149},[105,440,441],{"class":115},"({\n",[105,443,444,447,449],{"class":107,"line":140},[105,445,446],{"class":115},"  baseURL: ",[105,448,164],{"class":149},[105,450,167],{"class":115},[105,452,453],{"class":107,"line":156},[105,454,455],{"class":115},"  apiKey: process.env.LARAS_API_KEY,\n",[105,457,458],{"class":107,"line":170},[105,459,460],{"class":115},"});\n",[105,462,463],{"class":107,"line":187},[105,464,137],{"emptyLinePlaceholder":136},[105,466,467,469,472,474,477,479,481],{"class":107,"line":193},[105,468,428],{"class":111},[105,470,471],{"class":115}," stream ",[105,473,146],{"class":111},[105,475,476],{"class":111}," await",[105,478,206],{"class":115},[105,480,209],{"class":149},[105,482,441],{"class":115},[105,484,485,488,490],{"class":107,"line":198},[105,486,487],{"class":115},"  model: ",[105,489,222],{"class":149},[105,491,167],{"class":115},[105,493,494,497,499,502,504],{"class":107,"line":214},[105,495,496],{"class":115},"  messages: [{ role: ",[105,498,244],{"class":149},[105,500,501],{"class":115},", content: ",[105,503,255],{"class":149},[105,505,506],{"class":115}," }],\n",[105,508,509,512,515],{"class":107,"line":227},[105,510,511],{"class":115},"  stream: ",[105,513,514],{"class":149},"true",[105,516,167],{"class":115},[105,518,519,522,524],{"class":107,"line":261},[105,520,521],{"class":115},"  stream_options: { include_usage: ",[105,523,514],{"class":149},[105,525,526],{"class":115}," },\n",[105,528,529],{"class":107,"line":274},[105,530,460],{"class":115},[105,532,533],{"class":107,"line":295},[105,534,137],{"emptyLinePlaceholder":136},[105,536,537,539,541,543,545,547,550],{"class":107,"line":300},[105,538,308],{"class":111},[105,540,476],{"class":111},[105,542,73],{"class":115},[105,544,428],{"class":111},[105,546,311],{"class":115},[105,548,549],{"class":111},"of",[105,551,552],{"class":115}," stream) {\n",[105,554,555,558,561,563,565,567],{"class":107,"line":305},[105,556,557],{"class":111},"  const",[105,559,560],{"class":115}," text ",[105,562,146],{"class":111},[105,564,332],{"class":115},[105,566,335],{"class":149},[105,568,569],{"class":115},"]?.delta?.content;\n",[105,571,572,575,578,581],{"class":107,"line":320},[105,573,574],{"class":111},"  if",[105,576,577],{"class":115}," (text) process.stdout.",[105,579,580],{"class":149},"write",[105,582,583],{"class":115},"(text);\n",[105,585,586,588,591,594,596,599,602,605],{"class":107,"line":341},[105,587,574],{"class":111},[105,589,590],{"class":115}," (chunk.usage) console.",[105,592,593],{"class":149},"log",[105,595,382],{"class":115},[105,597,598],{"class":149},"`\\n\\n${",[105,600,601],{"class":149},"chunk.usage.total_tokens",[105,603,604],{"class":149},"} tokens`",[105,606,607],{"class":115},");\n",[105,609,610],{"class":107,"line":369},[105,611,612],{"class":115},"}\n",[95,614,619],{"className":615,"code":616,"filename":617,"language":618,"meta":101,"style":101},"language-bash shiki shiki-themes laras-ink","curl https:\u002F\u002Fapi.larasrouter.com\u002Fv1\u002Fchat\u002Fcompletions \\\n  -H \"Authorization: Bearer $LARAS_API_KEY\" \\\n  -H \"Content-Type: application\u002Fjson\" \\\n  --no-buffer \\\n  -d '{\n    \"model\": \"deepseek-v4.1-flash\",\n    \"messages\": [{\"role\": \"user\", \"content\": \"Write a haiku about Jakarta rain.\"}],\n    \"stream\": true,\n    \"stream_options\": {\"include_usage\": true}\n  }'\n","cURL","bash",[62,620,621,632,642,651,658,666,671,676,681,686],{"__ignoreMap":101},[105,622,623,626,629],{"class":107,"line":108},[105,624,625],{"class":149},"curl",[105,627,628],{"class":149}," https:\u002F\u002Fapi.larasrouter.com\u002Fv1\u002Fchat\u002Fcompletions",[105,630,631],{"class":115}," \\\n",[105,633,634,637,640],{"class":107,"line":119},[105,635,636],{"class":149},"  -H",[105,638,639],{"class":149}," \"Authorization: Bearer $LARAS_API_KEY\"",[105,641,631],{"class":115},[105,643,644,646,649],{"class":107,"line":133},[105,645,636],{"class":149},[105,647,648],{"class":149}," \"Content-Type: application\u002Fjson\"",[105,650,631],{"class":115},[105,652,653,656],{"class":107,"line":140},[105,654,655],{"class":149},"  --no-buffer",[105,657,631],{"class":115},[105,659,660,663],{"class":107,"line":156},[105,661,662],{"class":149},"  -d",[105,664,665],{"class":149}," '{\n",[105,667,668],{"class":107,"line":170},[105,669,670],{"class":149},"    \"model\": \"deepseek-v4.1-flash\",\n",[105,672,673],{"class":107,"line":187},[105,674,675],{"class":149},"    \"messages\": [{\"role\": \"user\", \"content\": \"Write a haiku about Jakarta rain.\"}],\n",[105,677,678],{"class":107,"line":193},[105,679,680],{"class":149},"    \"stream\": true,\n",[105,682,683],{"class":107,"line":198},[105,684,685],{"class":149},"    \"stream_options\": {\"include_usage\": true}\n",[105,687,688],{"class":107,"line":214},[105,689,690],{"class":149},"  }'\n",[87,692,694],{"id":693},"what-the-stream-looks-like","What the stream looks like",[95,696,701],{"className":697,"code":699,"filename":76,"language":700,"meta":101},[698],"language-text","data: {\"id\":\"chatcmpl-…\",\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\",\"content\":\"\"}}]}\n\ndata: {\"id\":\"chatcmpl-…\",\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Grey sky\"}}]}\n\ndata: {\"id\":\"chatcmpl-…\",\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\ndata: {\"id\":\"chatcmpl-…\",\"object\":\"chat.completion.chunk\",\"choices\":[],\"usage\":{\"prompt_tokens\":16,\"completion_tokens\":42,\"total_tokens\":58,\"prompt_cache_hit_tokens\":0,\"prompt_cache_miss_tokens\":16}}\n\ndata: [DONE]\n","text",[62,702,699],{"__ignoreMap":101},[704,705,706,725],"ul",{},[707,708,709,713,714,717,718,721,722,85],"li",{},[710,711,712],"strong",{},"Reasoning"," arrives first, in ",[62,715,716],{},"delta.reasoning_content",", before the answer's ",[62,719,720],{},"delta.content",". See ",[67,723,11],{"href":724},"\u002Fguides\u002Freasoning",[707,726,727,730,731,734,735,721,738,85],{},[710,728,729],{},"Tool calls"," arrive as ",[62,732,733],{},"delta.tool_calls"," fragments that you concatenate by ",[62,736,737],{},"index",[67,739,13],{"href":740},"\u002Fguides\u002Ftool-calling",[87,742,744],{"id":743},"token-usage","Token usage",[58,746,747,748,751,752,754,755,758,759,762,763,766,767,85],{},"With ",[62,749,750],{},"stream_options.include_usage"," set to ",[62,753,514],{},", the last chunk before ",[62,756,757],{},"[DONE]"," carries the ",[62,760,761],{},"usage"," object and an empty ",[62,764,765],{},"choices"," array. Without it, you receive no usage chunk — your request is still metered and charged the same way, and you can see it in the console under ",[710,768,769],{},"Usage",[87,771,773],{"id":772},"how-a-stream-ends","How a stream ends",[704,775,776,792,805],{},[707,777,778,781,782,785,786,788,789,791],{},[710,779,780],{},"Normally:"," a chunk with a ",[62,783,784],{},"finish_reason",", then (if requested) the usage chunk, then ",[62,787,84],{},". We send ",[62,790,757],{}," only after the request's usage has been recorded, so a stream you saw finish is a stream we billed exactly.",[707,793,794,797,798,801,802,804],{},[710,795,796],{},"With an error mid-stream:"," if the provider fails after the stream has started, the stream ends with an error event in the usual envelope, for example ",[62,799,800],{},"data: {\"error\":{\"message\":\"…\",\"type\":\"api_error\",\"code\":\"provider_unavailable\",\"param\":null}}",". Treat a stream that ends without ",[62,803,757],{}," as incomplete.",[707,806,807,810,811,85],{},[710,808,809],{},"Errors before the stream starts"," — an invalid key, an empty wallet, a rate limit — are returned as a normal JSON error response with the matching HTTP status, not as a stream. See ",[67,812,8],{"href":813},"\u002Fguides\u002Ferrors",[815,816,819],"callout",{"title":817,"type":818},"If you disconnect","info",[58,820,821,822,825,826,828],{},"Closing the connection early stops delivery to you, but not the generation: it runs to its end, up to your ",[62,823,824],{},"max_tokens",", and you are charged for the usage the provider reports for it. Your balance is settled within seconds either way. Set ",[62,827,824],{}," to bound what a generation you abandon can cost.",[830,831,832],"style",{},"html pre.shiki code .sifj9, html code.shiki .sifj9{--shiki-default:#8A8F98}html pre.shiki code .snt3o, html code.shiki .snt3o{--shiki-default:#B4B8BF}html pre.shiki code .sIGQv, html code.shiki .sIGQv{--shiki-default:#F7F8F8}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":101,"searchDepth":133,"depth":133,"links":834},[835,836,837,838],{"id":89,"depth":119,"text":90},{"id":693,"depth":119,"text":694},{"id":743,"depth":119,"text":744},{"id":772,"depth":119,"text":773},"Receive the response token by token as Server-Sent Events, with token usage on the final chunk.","md",null,{},"\u002Fguides\u002Fstreaming",{"title":12,"description":839},"guides\u002Fstreaming","2026-11-15","VyWRpnbiMzKwzQf6OOHPecGuhb6PKEBT6cxZmIL2UGw",1796424410883]