Read incremental text from compatible model responses and send relevant message history to continue a conversation through the llm.sg API endpoint.
Use streaming to display text as it arrives, and send relevant message history to give a llm.sg model context for the next turn.
These Python examples assume the OpenAI-compatible chat format and a supporting model. Streaming and conversation behavior still require validation with an authorized key.
Stream text
Complete the quickstart setup, then set stream=True and read incremental text from each chunk.
import osfrom openai import OpenAIclient = OpenAI( api_key=os.environ["LLM_SG_API_KEY"], base_url="https://api.llm.sg/v1",)stream = client.chat.completions.create( model=os.environ["LLM_SG_MODEL"], messages=[{"role": "user", "content": "Explain model routing briefly."}], stream=True,)try: for chunk in stream: if chunk.choices: content = chunk.choices[0].delta.content if content: print(content, end="", flush=True)finally: stream.close()print()
Some chunks contain no text. Handle connection failures during iteration and distinguish an interrupted stream from a completed answer. Streaming changes when content appears; it does not guarantee shorter total generation time.
Continue a conversation
For the compatible chat format, send the relevant user and assistant messages in each request. Your application manages the history it chooses to send.
import osfrom openai import OpenAIclient = OpenAI( api_key=os.environ["LLM_SG_API_KEY"], base_url="https://api.llm.sg/v1",)model = os.environ["LLM_SG_MODEL"]messages = [{"role": "user", "content": "What is an API?"}]first = client.chat.completions.create(model=model, messages=messages)answer = first.choices[0].message.contentif not answer: raise RuntimeError("This example requires a text response")print(answer)messages.append({"role": "assistant", "content": answer})messages.append({"role": "user", "content": "Give a restaurant analogy."})second = client.chat.completions.create(model=model, messages=messages)print(second.choices[0].message.content)
History adds input tokens. Keep relevant recent messages or summarize older content, and leave enough context capacity for the answer.Check image input, tool calling, reasoning, and structured output support separately before using them. A model’s tool request does not execute the tool; your application usually performs that action and sends the result back.
{"docsConfig":{"theme":"mint","$schema":"https://mintlify.com/docs.json","name":"llm.sg Docs","description":"User guides for the llm.sg model router platform.","colors":{"primary":"#171717","light":"#E5E5E5","dark":"#000000"},"logo":{"light":"https://mintcdn.com/llmsg/FOR_5jAu1HmGbX-_/logo/llmsg-light.svg?fit=max\u0026auto=format\u0026n=FOR_5jAu1HmGbX-_\u0026q=85\u0026s=c751c79771f51fd51f5c84a398e3dc89","dark":"https://mintcdn.com/llmsg/FOR_5jAu1HmGbX-_/logo/llmsg-dark.svg?fit=max\u0026auto=format\u0026n=FOR_5jAu1HmGbX-_\u0026q=85\u0026s=1cd393d360733c6ada6ee0b5c3cd250d","href":"https://llm.sg"},"favicon":"https://mintcdn.com/llmsg/s4Y_9-SsMFIEzcVu/logo/dashboard-favicon.svg?fit=max\u0026auto=format\u0026n=s4Y_9-SsMFIEzcVu\u0026q=85\u0026s=cd08bc776b90db68bc30f1f792ead380","appearance":{"strict":true},"navbar":{"links":[{"href":"https://llm.sg/dashboard","label":"Dashboard"}]},"footer":{"links":[{"header":"Legal","items":[{"label":"Privacy policy","href":"/en/docs/privacy-policy"},{"label":"Terms of service","href":"/en/docs/terms-of-service"}]}]},"fonts":{"family":"DM Sans"},"contextual":{"options":["copy","view"],"display":"header"},"styling":{"latex":false},"navigation":{"languages":[{"language":"en","default":true,"navbar":{"links":[{"href":"https://llm.sg/dashboard","label":"Dashboard"}]},"href":"en/docs/overview"},{"language":"zh-Hans","footer":{"links":[{"header":"法律条款","items":[{"label":"隐私政策","href":"/zh-Hans/docs/privacy-policy"},{"label":"服务条款","href":"/zh-Hans/docs/terms-of-service"}]}]},"navbar":{"links":[{"href":"https://llm.sg/dashboard","label":"Dashboard"}]},"href":"zh-Hans/docs/overview"}]}},"deployment":{"deploymentMetadataValue":{"subdomain":"llmsg","hasAudioNarration":false,"actualSubdomain":"llmsg","customDomains":["wiki.llm.sg"],"gitSource":{"type":"github","owner":"agnesai-engineering","repo":"llmsgdocs","deployBranch":"master","contentDirectory":"","isPrivate":true,"mountPath":"","label":"","_id":"6adecd8990f989adb253c76e"},"entitlements":{},"buildId":"6aed2cddab6690d1634ff54e","clientVersion":"0.0.4143","searchFilterCounts":[{"language":"en","tag":"User guides","count":9},{"language":"zh-Hans","tag":"用户指南","count":9},{"language":"en","tag":"Get started","count":3},{"language":"en","tag":"Guides","count":3},{"language":"zh-Hans","tag":"开始使用","count":3},{"language":"zh-Hans","tag":"使用指南","count":3},{"language":"en","tag":"Legal","count":2},{"language":"zh-Hans","tag":"法律条款","count":2},{"language":"en","tag":"Help","count":1},{"language":"zh-Hans","tag":"帮助","count":1}]},"assistantConfig":{"enableAskAiButton":false,"enableStarterQuestions":false,"enableContextualStarterQuestions":false,"enableAskAiSearchBar":true},"basePath":"","hasChatPermissions":false,"hasWebMcpPermissions":false,"toggles":[{"name":"kill-isr","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-rbac-roles","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-mintlify-slack","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-integrations-settings","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"rbac-expansion","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-pr-grouping","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-prefix","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"analytics-v7","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"user-flows-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"no-publish-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-onboarding","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving-live","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"stripTemperatureMiddleware","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"support-platform-pylon-zendesk","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"instant-rollback","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications-server","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-fable","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fixed-ai-action-pricing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automation-resolve-merge-conflict","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"scoped-publishing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"mintql","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"page-level-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-version-history-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"billing-page-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"full-page-search","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workspace-projects","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"assistant-v5","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fork-pr-previews","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"revalidation-jwt","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-sso-scim","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"hobby-abuse-scan","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"dashboard-inbox","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-signup","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-dual-write","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workos-enabled","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"approve-review-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-metadata","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"sso-setup-deadline-banner","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-serve","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"live-agents","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"llms-txt-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false}],"googleFontsInlined":true},"customCss":[{"filePath":"style.css","content":"footer a[href*=\"utm_campaign=poweredBy\"],#footer a[href*=\"utm_campaign=poweredBy\"]{display:none!important}a[href*=\"apihub.agnes-ai.com\"],a[href^=\"https://apihub.agnes-ai.com\"]{pointer-events:none!important;cursor:default!important;text-decoration:none!important;color:inherit!important}#navbar,#sidebar{letter-spacing:0.02em}.nav-logo{margin-top:4px}#sidebar .sidebar-group-header{margin-top:0!important;margin-bottom:0.625rem!important;padding:0!important;min-height:0!important}@media (min-width: 1024px){#sidebar .sidebar-group-header{margin-bottom:0.625rem!important}}#sidebar .sidebar-group-header h3.sidebar-title,#sidebar h3.sidebar-title{margin:0;padding:0;font-size:0.875rem;line-height:1.5rem;font-weight:600;letter-spacing:0.02em;color:rgb(17 24 39)}html.dark #sidebar .sidebar-group-header h3.sidebar-title,html.dark #sidebar h3.sidebar-title,[data-theme=\"dark\"] #sidebar .sidebar-group-header h3.sidebar-title,[data-theme=\"dark\"] #sidebar h3.sidebar-title{color:rgb(229 231 235)}#sidebar .sidebar-group-header h3.sidebar-title span,#sidebar h3.sidebar-title span{display:block;line-height:inherit}#sidebar #sidebar-content li>a,#sidebar #sidebar-content li>button{font-size:0.875rem;font-weight:400;letter-spacing:0.02em}#sidebar #sidebar-content li[data-active]>a,#sidebar #sidebar-content li[data-active]>button{font-weight:500}.navbar-link>a{font-weight:400}table{border-collapse:collapse;border-spacing:0;width:100%;margin:1rem 0 1.5rem;border:none;border-radius:0;overflow:visible;background:transparent}table thead th,table tbody th,table tbody td{border:none;border-bottom:1px solid #eeeeee;text-align:left;vertical-align:top;padding:1rem 1.25rem 1rem 0;background:transparent}table thead th:last-child,table tbody th:last-child,table tbody td:last-child{padding-right:0}table thead th{font-weight:600;color:inherit}table tbody tr:last-child th,table tbody tr:last-child td{border-bottom:none}table tbody td:first-child,table tbody th:first-child{font-weight:600}html.dark table thead th,html.dark table tbody th,html.dark table tbody td,[data-theme=\"dark\"] table thead th,[data-theme=\"dark\"] table tbody th,[data-theme=\"dark\"] table tbody td{border-bottom-color:rgba(255, 255, 255, 0.1)}del{text-decoration:none;position:relative}del::after{content:\"\";position:absolute;left:0;right:0;top:50%;height:1px;background:currentColor;transform:translateY(-50%);pointer-events:none}h2{margin-top:2.5rem;margin-bottom:1rem;padding-bottom:0.375rem}h3{margin-top:1.75rem;margin-bottom:0.75rem}article>p:first-of-type,#main-content>p:first-of-type{font-size:1.05rem;line-height:1.7}pre{border-radius:0.5rem;margin:1rem 0 1.5rem}.note p,.tip p,.warning p,.info p{margin-bottom:0.5rem}.note ul,.tip ul,.warning ul,.info ul{margin-top:0.5rem;margin-bottom:0}.card-group{margin:1rem 0 1.5rem}.accordion-group{margin:1rem 0}.steps{margin:1.5rem 0}.tabs{margin:1rem 0 1.5rem}article :not(pre)>code,.prose :not(pre)>code,p :not(pre)>code,li :not(pre)>code,td :not(pre)>code,.field-row :not(pre)>code{background-color:#f3f4f6;color:#e85d4c;border-radius:0.375rem;padding:0.2rem 0.55rem;font-size:0.8125rem;font-weight:500;font-family:ui-monospace, SFMono-Regular, \"SF Mono\", Menlo, Consolas, monospace;border:none;box-decoration-break:clone;-webkit-box-decoration-break:clone}html.dark article :not(pre)>code,html.dark .prose :not(pre)>code,[data-theme=\"dark\"] article :not(pre)>code,[data-theme=\"dark\"] .prose :not(pre)>code{background-color:rgba(255, 255, 255, 0.08);color:#f87171}.field-row{display:block;margin:0.625rem 0 1rem;line-height:1.75}.field-label{color:#525252;margin-right:0.35rem}html.dark .field-label,[data-theme=\"dark\"] .field-label{color:#a3a3a3}.field-kv{display:block;margin:0.35rem 0;line-height:1.75}.field-kv .field-key{color:#525252;margin-right:0.35rem}html.dark .field-kv .field-key,[data-theme=\"dark\"] .field-kv .field-key{color:#a3a3a3}"}],"page":{"pagePath":"en/docs/chat","slug":"/en/docs/chat","pageMetadata":{"title":"Stream responses and manage conversations","sidebarTitle":"Chat","description":"Read incremental text from compatible model responses and send relevant message history to continue a conversation through the llm.sg API endpoint.","keywords":["llm.sg","streaming","conversation"],"href":"/en/docs/chat"},"description":{"compiledSource":"\"use strict\";\nconst {jsx: _jsx} = arguments[0];\nconst {useMDXComponents: _provideComponents} = arguments[0];\nfunction _createMdxContent(props) {\n const _components = {\n p: \"p\",\n ..._provideComponents(),\n ...props.components\n };\n return _jsx(_components.p, {\n children: \"Read incremental text from compatible model responses and send relevant message history to continue a conversation through the llm.sg API endpoint.\"\n });\n}\nfunction MDXContent(props = {}) {\n const {wrapper: MDXLayout} = {\n ..._provideComponents(),\n ...props.components\n };\n return MDXLayout ? _jsx(MDXLayout, {\n ...props,\n children: _jsx(_createMdxContent, {\n ...props\n })\n }) : _createMdxContent(props);\n}\nreturn {\n default: MDXContent\n};\n","frontmatter":{},"scope":{}},"pageType":"default","mdx":{"mdxSource":{"compiledSource":"\"use strict\";const{Fragment:_Fragment,jsx:_jsx,jsxs:_jsxs}=arguments[0];const{useMDXComponents:_provideComponents}=arguments[0];function _createMdxContent(props){const _components={a:`a`,code:`code`,p:`p`,pre:`pre`,span:`span`,..._provideComponents(),...props.components},{CodeBlock,Heading,Note}=_components;if(!CodeBlock)_missingMdxReference(`CodeBlock`,true);if(!Heading)_missingMdxReference(`Heading`,true);if(!Note)_missingMdxReference(`Note`,true);return _jsxs(_Fragment,{children:[_jsx(_components.p,{children:`Use streaming to display text as it arrives, and send relevant message history to give a llm.sg model context for the next turn.`}),`\n`,_jsx(Note,{children:_jsx(_components.p,{children:`These Python examples assume the OpenAI-compatible chat format and a supporting model. Streaming and conversation behavior still require validation with an authorized key.`})}),`\n`,_jsx(Heading,{level:`2`,id:`stream-text`,children:`Stream text`}),`\n`,_jsxs(_components.p,{children:[`Complete the `,_jsx(_components.a,{href:`/en/docs/quickstart`,children:`quickstart`}),` setup, then set `,_jsx(_components.code,{children:`stream=True`}),` and read incremental text from each chunk.`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`21`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`21`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` os`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`from`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` openai `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` OpenAI`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`client `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` OpenAI(`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` api_key`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`os.environ[`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"LLM_SG_API_KEY\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`],`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` base_url`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"https://api.llm.sg/v1\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`stream `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` client.chat.completions.create(`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` model`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`os.environ[`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"LLM_SG_MODEL\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`],`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` messages`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`[{`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"role\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`: `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"user\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"content\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`: `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"Explain model routing briefly.\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`}],`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` stream`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#569CD6`},children:`True`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`try`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` for`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` chunk `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`in`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` stream:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` if`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` chunk.choices:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` content `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` chunk.choices[`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`0`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`].delta.content`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` if`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` content:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:` print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(content, `}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`end`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`flush`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#569CD6`},children:`True`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`finally`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`:`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` stream.close()`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`()`})]}),`\n`]})})}),`\n`,_jsx(_components.p,{children:`Some chunks contain no text. Handle connection failures during iteration and distinguish an interrupted stream from a completed answer. Streaming changes when content appears; it does not guarantee shorter total generation time.`}),`\n`,_jsx(Heading,{level:`2`,id:`continue-a-conversation`,children:`Continue a conversation`}),`\n`,_jsx(_components.p,{children:`For the compatible chat format, send the relevant user and assistant messages in each request. Your application manages the history it chooses to send.`}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`19`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`19`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` os`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`from`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` openai `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` OpenAI`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`client `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` OpenAI(`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` api_key`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`os.environ[`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"LLM_SG_API_KEY\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`],`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:` base_url`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"https://api.llm.sg/v1\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`,`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`model `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` os.environ[`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"LLM_SG_MODEL\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`]`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`messages `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` [{`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"role\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`: `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"user\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"content\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`: `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"What is an API?\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`}]`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`first `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` client.chat.completions.create(`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`model`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`model, `}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`messages`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`messages)`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`answer `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` first.choices[`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`0`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`].message.content`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`if`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` not`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` answer:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` raise`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4EC9B0`},children:` RuntimeError`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"This example requires a text response\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(answer)`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`messages.append({`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"role\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`: `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"assistant\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"content\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`: answer})`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`messages.append({`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"role\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`: `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"user\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"content\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`: `}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"Give a restaurant analogy.\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`})`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`second `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` client.chat.completions.create(`}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`model`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`model, `}),_jsx(_components.span,{style:{color:`#953800`,\"--shiki-dark\":`#9CDCFE`},children:`messages`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`messages)`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(second.choices[`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`0`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`].message.content)`})]}),`\n`]})})}),`\n`,_jsx(_components.p,{children:`History adds input tokens. Keep relevant recent messages or summarize older content, and leave enough context capacity for the answer.`}),`\n`,_jsx(_components.p,{children:`Check image input, tool calling, reasoning, and structured output support separately before using them. A model’s tool request does not execute the tool; your application usually performs that action and sends the result back.`})]})}function MDXContent(props={}){const{wrapper:MDXLayout}={..._provideComponents(),...props.components};return MDXLayout?_jsx(MDXLayout,{...props,children:_jsx(_createMdxContent,{...props})}):_createMdxContent(props)}return{default:MDXContent};function _missingMdxReference(id,component){throw new Error(`Expected `+(component?`component`:`object`)+\" `\"+id+\"` to be defined: you likely forgot to import, pass, or provide it.\")}","frontmatter":{},"scope":{"config":{},"pageMetadata":{"title":"Stream responses and manage conversations","sidebarTitle":"Chat","description":"Read incremental text from compatible model responses and send relevant message history to continue a conversation through the llm.sg API endpoint.","keywords":["llm.sg","streaming","conversation"],"href":"/en/docs/chat"}}},"mdxSourceWithNoJs":null},"mdxExtracts":{"tableOfContents":[{"title":"Stream text","slug":"stream-text","depth":2,"children":[]},{"title":"Continue a conversation","slug":"continue-a-conversation","depth":2,"children":[]}],"codeExamples":{}},"jsLinks":[],"subdomain":"llmsg","siteName":"llm.sg Docs","title":"Stream responses and manage conversations - llm.sg Docs","navMaps":{"firstHrefInVersion":{},"firstHrefInLanguage":{"en":{"href":"/en/docs/chat","matchLevel":0},"zh-Hans":{"href":"/zh-Hans/docs/chat","matchLevel":1}}},"lastModified":"2026-11-05T10:06:03.462Z","originalFileLocation":"en/docs/chat.mdx","locale":"en"},"headHtml":"\u003cscript data-mint-head>(function(){var d=document.documentElement,p=null;if(!p){try{p=localStorage.getItem(\"isDarkMode\")}catch(e){}if(p==='true')p='dark';else if(p==='false')p='light';if(p!=='light'\u0026\u0026p!=='dark'\u0026\u0026p!=='system')p=\"system\"}var r=p==='system'?(matchMedia(\"(prefers-color-scheme: dark)\").matches?'dark':'light'):p;d.classList.remove(r==='dark'?'light':'dark');d.classList.add(r);d.setAttribute(\"data-theme-preference\",p)})();\u003c/script>\u003cmeta data-mint-head name=\"description\" content=\"Read incremental text from compatible model responses and send relevant message history to continue a conversation through the llm.sg API endpoint.\">\u003cmeta data-mint-head name=\"application-name\" content=\"llm.sg Docs\">\u003cmeta data-mint-head name=\"generator\" content=\"Mintlify\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/llmsg/G-zm4FuP4oHKftlT/_generated/favicon/android-chrome-192x192.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"apple-touch-icon\" type=\"image/png\" sizes=\"180x180\" href=\"/mintlify-assets/_mintlify/favicons/llmsg/G-zm4FuP4oHKftlT/_generated/favicon/apple-touch-icon.png\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/llmsg/G-zm4FuP4oHKftlT/_generated/favicon/favicon-16x16.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/llmsg/G-zm4FuP4oHKftlT/_generated/favicon/favicon-32x32.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/llmsg/G-zm4FuP4oHKftlT/_generated/favicon/favicon.ico\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/llmsg/G-zm4FuP4oHKftlT/_generated/favicon-dark/android-chrome-192x192.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/llmsg/G-zm4FuP4oHKftlT/_generated/favicon-dark/favicon-16x16.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/llmsg/G-zm4FuP4oHKftlT/_generated/favicon-dark/favicon-32x32.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/llmsg/G-zm4FuP4oHKftlT/_generated/favicon-dark/favicon.ico\" media=\"(prefers-color-scheme: dark)\">\u003cmeta data-mint-head property=\"og:title\" content=\"Stream responses and manage conversations - llm.sg Docs\">\u003cmeta data-mint-head property=\"og:description\" content=\"Read incremental text from compatible model responses and send relevant message history to continue a conversation through the llm.sg API endpoint.\">\u003cmeta data-mint-head property=\"og:site_name\" content=\"llm.sg Docs\">\u003cmeta data-mint-head property=\"og:url\" content=\"https://wiki.llm.sg/en/docs/chat\">\u003cmeta data-mint-head property=\"og:type\" content=\"website\">\u003cmeta data-mint-head property=\"og:image\" content=\"https://llmsg.mintlify.site/_mintlify/api/og/v2?division=Guides\u0026amp;title=Stream+responses+and+manage+conversations\u0026amp;description=Read+incremental+text+from+compatible+model+responses+and+send+relevant+message+history+to+continue+a+conversation+through+the+llm.sg+API+endpoint.\u0026amp;theme=d93cf05fcee7e9bc600d31b2\u0026amp;signature=36c8628f54c5b3a0d422b91048913592d3ba3e2fe82a17e1db498602b24c25fe\">\u003cmeta data-mint-head property=\"og:image:width\" content=\"1200\">\u003cmeta data-mint-head property=\"og:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"twitter:card\" content=\"summary_large_image\">\u003cmeta data-mint-head name=\"twitter:title\" content=\"Stream responses and manage conversations - llm.sg Docs\">\u003cmeta data-mint-head name=\"twitter:description\" content=\"Read incremental text from compatible model responses and send relevant message history to continue a conversation through the llm.sg API endpoint.\">\u003cmeta data-mint-head name=\"twitter:image\" content=\"https://llmsg.mintlify.site/_mintlify/api/og/v2?division=Guides\u0026amp;title=Stream+responses+and+manage+conversations\u0026amp;description=Read+incremental+text+from+compatible+model+responses+and+send+relevant+message+history+to+continue+a+conversation+through+the+llm.sg+API+endpoint.\u0026amp;theme=d93cf05fcee7e9bc600d31b2\u0026amp;signature=36c8628f54c5b3a0d422b91048913592d3ba3e2fe82a17e1db498602b24c25fe\">\u003cmeta data-mint-head name=\"twitter:image:width\" content=\"1200\">\u003cmeta data-mint-head name=\"twitter:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"msapplication-config\" content=\"/mintlify-assets/_mintlify/favicons/llmsg/G-zm4FuP4oHKftlT/_generated/favicon/browserconfig.xml\">\u003cmeta data-mint-head name=\"apple-mobile-web-app-title\" content=\"llm.sg Docs\">\u003cmeta data-mint-head name=\"msapplication-TileColor\" content=\"#171717\">\u003clink data-mint-head rel=\"canonical\" href=\"https://wiki.llm.sg/en/docs/chat\">\u003clink data-mint-head rel=\"alternate\" type=\"application/xml\" href=\"/sitemap.xml\">\u003clink data-mint-head rel=\"alternate\" type=\"text/markdown\" href=\"/en/docs/chat.md\">\u003clink data-mint-head rel=\"alternate\" hreflang=\"en\" href=\"https://wiki.llm.sg/en/docs/chat\">\u003clink data-mint-head rel=\"alternate\" hreflang=\"zh-Hans\" href=\"https://wiki.llm.sg/zh-Hans/docs/chat\">\u003clink data-mint-head rel=\"alternate\" hreflang=\"x-default\" href=\"https://wiki.llm.sg/en/docs/chat\">\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https://wiki.llm.sg/#organization\",\"name\":\"llm.sg Docs\",\"url\":\"https://wiki.llm.sg\",\"logo\":{\"@type\":\"ImageObject\",\"url\":\"https://mintcdn.com/llmsg/FOR_5jAu1HmGbX-_/logo/llmsg-light.svg?fit=max\u0026auto=format\u0026n=FOR_5jAu1HmGbX-_\u0026q=85\u0026s=c751c79771f51fd51f5c84a398e3dc89\"}},{\"@type\":\"WebSite\",\"@id\":\"https://wiki.llm.sg#website\",\"name\":\"llm.sg Docs\",\"url\":\"https://wiki.llm.sg\",\"publisher\":{\"@id\":\"https://wiki.llm.sg/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https://wiki.llm.sg/en/docs/chat#webpage\",\"url\":\"https://wiki.llm.sg/en/docs/chat\",\"name\":\"Stream responses and manage conversations\",\"description\":\"Read incremental text from compatible model responses and send relevant message history to continue a conversation through the llm.sg API endpoint.\",\"dateModified\":\"2026-11-05T10:06:03.462Z\",\"isPartOf\":{\"@id\":\"https://wiki.llm.sg#website\"},\"breadcrumb\":{\"@id\":\"https://wiki.llm.sg/en/docs/chat#breadcrumb\"}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://wiki.llm.sg/en/docs/chat#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Guides\",\"item\":\"https://wiki.llm.sg/en/docs/models\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Stream responses and manage conversations\",\"item\":\"https://wiki.llm.sg/en/docs/chat\"}]},{\"@type\":[\"Article\",\"TechArticle\"],\"@id\":\"https://wiki.llm.sg/en/docs/chat#article\",\"headline\":\"Stream responses and manage conversations\",\"name\":\"Stream responses and manage conversations\",\"description\":\"Read incremental text from compatible model responses and send relevant message history to continue a conversation through the llm.sg API endpoint.\",\"url\":\"https://wiki.llm.sg/en/docs/chat\",\"mainEntityOfPage\":{\"@id\":\"https://wiki.llm.sg/en/docs/chat#webpage\"},\"dateModified\":\"2026-11-05T10:06:03.462Z\",\"keywords\":\"llm.sg, streaming, conversation\",\"publisher\":{\"@id\":\"https://wiki.llm.sg/#organization\"},\"isPartOf\":{\"@id\":\"https://wiki.llm.sg#website\"}}]}\u003c/script>\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"WebSite\",\"name\":\"llm.sg Docs\",\"publisher\":{\"@type\":\"Organization\",\"name\":\"llm.sg Docs\"},\"provider\":{\"@type\":\"Organization\",\"name\":\"Mintlify\",\"url\":\"https://mintlify.com\"}}\u003c/script>","agentPointerHref":"/en/docs/chat.md","siteName":"llm.sg Docs","nav":{"global":null,"languages":[{"language":"en","default":true,"navbar":{"links":[{"href":"https://llm.sg/dashboard","label":"Dashboard"}]},"tabs":[{"tab":"User guides","groups":[{"group":"Get started","pages":[{"title":"Welcome to llm.sg","href":"/en/docs/overview"},{"title":"Account and API keys","href":"/en/docs/account-and-api-keys"},{"title":"Get started with llm.sg","sidebarTitle":"Quickstart","href":"/en/docs/quickstart"}]},{"group":"Guides","pages":[{"title":"Choose and switch models","sidebarTitle":"Models","description":"Discover models available to your llm.sg key, compare task suitability and capabilities, and switch model IDs while checking parameter compatibility.","href":"/en/docs/models"},{"title":"Stream responses and manage conversations","sidebarTitle":"Chat","href":"/en/docs/chat"},{"title":"Understand usage and billing","sidebarTitle":"Usage and billing","description":"Understand token usage, estimate text generation costs using current model prices, and compare llm.sg request records with your account billing data.","href":"/en/docs/usage-and-billing"}]},{"group":"Help","pages":[{"title":"Troubleshoot API requests","sidebarTitle":"Troubleshooting","href":"/en/docs/troubleshooting"}]},{"group":"Legal","pages":[{"title":"Privacy policy","href":"/en/docs/privacy-policy"},{"title":"Terms of service","href":"/en/docs/terms-of-service"}]}]}]},{"language":"zh-Hans","footer":{"links":[{"header":"法律条款","items":[{"label":"隐私政策","href":"/zh-Hans/docs/privacy-policy"},{"label":"服务条款","href":"/zh-Hans/docs/terms-of-service"}]}]},"navbar":{"links":[{"href":"https://llm.sg/dashboard","label":"Dashboard"}]},"pages":[{"title":"zh-Hans","href":"/zh-Hans/docs/chat"}]}]},"basePath":"","title":"Stream responses and manage conversations - llm.sg Docs","buildId":"dpl_Fyv1NbzwP68g2bEH2Roy98uY1zmE","deploymentId":"dpl_Fyv1NbzwP68g2bEH2Roy98uY1zmE","dynamics":[],"chunks":[]}