Signature
for await.
The events
started, then token deltas, then completed.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Stream tokens as they are produced
for await (const event of RunAnywhere.llm.generateStream('Write a haiku')) {
if (event.type === 'textDelta') setText((t) => t + event.text)
}
generateStream(input: string | ChatMessage[], options?: LlmOptions): AsyncIterable<GenerationEvent>
for await.
started, then token deltas, then completed.
for await (const event of RunAnywhere.llm.generateStream(prompt)) {
switch (event.type) {
case 'textDelta':
setText((t) => t + event.text)
break
case 'completed':
setSpeed(event.result.tokensPerSecond)
break
}
}
for await
loop itself throws. This is the opposite of the Web SDK, where in-flight failures arrive as a
failed event. Do not share one error-handling helper between them.try {
for await (const event of RunAnywhere.llm.generateStream(prompt)) {
if (event.type === 'textDelta') setText((t) => t + event.text)
}
} catch (error) {
showError(error)
}
const buffer = useRef('')
useEffect(() => {
const id = setInterval(() => {
if (buffer.current) {
setText((t) => t + buffer.current)
buffer.current = ''
}
}, 50)
return () => clearInterval(id)
}, [])
// in the loop
if (event.type === 'textDelta') buffer.current += event.text
const cancelled = useRef(false)
useEffect(
() => () => {
cancelled.current = true
},
[]
)
for await (const event of RunAnywhere.llm.generateStream(prompt)) {
if (cancelled.current) break
if (event.type === 'textDelta') setText((t) => t + event.text)
}