Skip to main content

getLLMProvider

workflow.getLLMProvider() is a provider parsing tool. The LLM node is called when it is executed, and the business code can also be used directly in custom nodes or advanced scenarios.

When executor is configured with tokenUsage.enabled, the token usage is automatically reported when the provider parsed by getLLMProvider() is returned by chat() or when the stream finish chunk carries usage. workspace examples that call provider directly in custom nodes will also follow the same statistics link; provider without usage will not write statistics. The report will read the usage field of common provider, including cache read / cache write or cache creation token details.

Signature

workflow.getLLMProvider(
context: NodeContext,
nodeName: string,
source?: LLMProviderSource,
): LLMProvider

Parameter

ParameterTypeDescription
contextNodeContextRead provider, binding, and default provider from context.providers.
nodeNamestringCurrent node name; used for node-level binding and error messages.
sourceLLMProviderSourceOptional provider source: instance, id, ref, or resolver.

ProviderRegistry

FieldTypeDescription
llmLLMProvider | undefinedA compatible default single provider.
llmProvidersobject | undefinedprovider id to provider instance mapping.
llmProviderBindingsobject | undefinedNode name to provider/model binding mapping.
defaultLLMProviderBindingLLMProviderBinding | undefinedDefault provider/model binding.
defaultLLMProviderIdstring | undefinedthe default provider id.

LLMProviderSource

FormTypeDescription
provider examplesLLMProviderDirectly back.
provider idstringSearch from llmProviders.
provider refLLMProviderRefCheck ref.id first and use ref.fallback if necessary.
resolver(context, nodeName) => LLMProvider | undefinedCustom parsing.

Parsing order

OrderSourceDescription
1Node BindingUse context.providers.llmProviderBindings[nodeName] first.
2sourceThe provider source declared using the node.
3Default BindingUse defaultLLMProviderBinding.
4default idUse defaultLLMProviderId to find from llmProviders.
5Compatible with default providerUse context.providers.llm directly.
6ref fallbackOnly try when source is ref.

Return value

ReturnTypeDescription
providerLLMProvidercontains name, chat(input), stream(input).

Error

SituationError TypeDescription
Bound provider/model not foundprovider_callError reporting will include providerId, modelId and nodeName.
Failed to parse all sourcesprovider_callThe current node requires an LLM provider, but no provider is available in the context.

Example

const provider = workflow.getLLMProvider(
context,
"answer",
workflow.createLLMProviderRef("default"),
);

const output = await provider.chat({ prompt: input.message });