Skip to main content

createLLMStreamNode

workflow.createLLMStreamNode() Creates a streaming LLM invocation node. When the node is executed, provider.stream(llmInput) is called, LLMStreamChunk is produced, and runStreamNode collects chunks and returns aggregate text.

Signature

workflow.createLLMStreamNode<Input>(
options: Pick<
LLMNodeOptions<Input>,
"name" | "provider" | "mapInput" | "historyLimit" | "metadata"
>,
): BaseStreamNode<Input, LLMStreamChunk, string>

Parameter

ParameterTypeDescription
optionsLLMStreamNodeOptions<Input>Define provider, input mappings, and node meta information.

Options Field

FieldTypeDescription
namestringNode name, default "llm-stream".
providerLLMProviderSourceprovider instance, provider id, provider ref, or resolver. Required.
mapInput(input, context) => LLMChatInputTranslate the business input into an LLM request. Required.
historyLimitnumberThe number of nodes retained in the execution history. The default value is 50.
metadataWorkflowNodeMetadataInputThe node displays meta information.

LLMStreamChunk

TypeFieldDescription
text-deltatext: stringA piece of incremental text.
finishfinishReason?: FinishReasonStream end reason.
finishusage?: LanguageModelUsagetoken usage.
finishproviderMetadata?: ProviderMetadataprovider meta information.

Return value

ReturnTypeDescription
Node ObjectBaseStreamNode<Input, LLMStreamChunk, string>Execute via workflow.runStreamNode.
chunksLLMStreamChunk[]runStreamNode All chunks collected.
outputstringNode finalize splices all text-delta.text.

When executor is configured with tokenUsage: { enabled: true }, the token usage is automatically reported from the usage of the stream finish chunk by using the provider parsed by getLLMProvider() during node execution. the same stream is reported only once. if the provider does not return the usage, the token usage is not written. Automatic reporting retains the input, output, reasoning, cached input, and cache creation/write token details.

Call process

PhaseInputResult
Upstream nodeInputBusiness input
mapInputInput, NodeContextLLMChatInput
provider streamLLMChatInputAsyncIterable<LLMStreamChunk>
runStreamNodechunks{ chunks, output: string }

Example

const streamNode = workflow.createLLMStreamNode<ParsedInput>({
name: "draft-stream",
provider: workflow.createLLMProviderRef("default"),
mapInput(input) {
return {
prompt: input.message,
temperature: 0.3,
};
},
});

const { chunks, output } = await workflow.runStreamNode(streamNode, parsed, context);

Difference from createLLMNode

MethodOutput methodSuitable scenarios
createLLMNodeOne-time return of LLMChatOutput or mapOutput resultsBackground processing, non-real-time output.
createLLMStreamNodeChunk-by-chunk output, finally aggregated into a stringStreaming text needs to be shown in Desktop/server/embed.