Skip to content

Branch a workflow on an LLM result ​

What you get: A webhook workflow that asks a model, checks the answer with an If node, and returns either the answer or "No answer generated".

Needs: A model: local Ollama as shipped, or a provider key · Time: 10 minutes · Nodes: 5

A Webhook Input at POST /webhook/conditional-demo passes user_prompt, system_prompt and session_id from the request body to an LLM Call. The Check Answer If node tests {{answer}}: if the model said anything, the Success Output returns it; if not, the Error Output returns "No answer generated". Only one output runs. As shipped, the model is llama3.1 on a local Ollama server, and the webhook has no authentication.

Steps ​

  1. Download conditional-flow.json, or take it from the samples folder on GitHub.

  2. In the studio's workflow editor, import the file (see import and export).

  3. Open the LLM Call node and point it at a model you have: pull llama3.1 into Ollama, or switch the provider. In Docker, use http://host.docker.internal:11434/v1 for Ollama on the host (on Linux, add extra_hosts: ["host.docker.internal:host-gateway"] to the api service). Save. To check the branching first without a model, use echo: the Provider list has no echo entry, so set the node's providerId to echo in the JSON file and import it again.

  4. Send a request:

    bash
    curl -X POST http://localhost:4000/webhook/conditional-demo \
      -H "Content-Type: application/json" \
      -d '{"user_prompt": "Is order 1042 late?"}'
  5. Open the run and click Check Answer to see evaluatedValue and which branch it took.

How it works ​

Diagram of the Conditional Branching Flow workflowRuns Webhook Input, then LLM Call, then Check Answer, then Success Output, then Error Output.Webhook Input (webhook_input)Webhook Inputwebhook_inputLLM Call (llm_call)LLM Callllm_callCheck Answer (if_node)Check Answerif_nodeSuccess Output (output)Success OutputoutputError Output (output)Error Outputoutput
Solid arrows are the order the steps run in. Dashed lines attach a chat model, memory, tools or workers to an agent; the agent uses them while it runs.
NodeTypeRole
Webhook Inputwebhook_inputStep in the run
LLM Callllm_callStep in the run
Check Answerif_nodeStep in the run
Success OutputoutputStep in the run
Error OutputoutputStep in the run

4 node types. Each type links to its config fields in the node reference.

What a run does ​

We ran it on a fresh install with the echo model and with a local stand-in server:

  • Echo. The webhook answered {"output":{"result":"Echo: Is order 1042 late?"}}. Check Answer evaluated to that text, took the true branch, and the Error Output was marked skipped.
  • No user_prompt in the body. Echo still answers, with a hint: "Echo provider received no user message. Wire a Text Input or Prompt Template into this LLM Call to see the echo." The true branch ran. A real model would get an empty message instead.
  • An empty answer. With a stand-in model that returns an empty string, the run took the false branch and returned {"output":{"result":"No answer generated"}}.
  • The model is down. With the model's server unreachable, the run failed at the LLM Call after 4 attempts, the If node never ran, and the caller got HTTP 400 with {"output":null,"error":"openai_compatible request failed after 4 attempt(s): fetch failed"}. The message names the node's provider; our stand-in used openai_compatible.

So the false branch catches an empty answer, which is rare with a working model. A model error, a bad key or a timeout fails the run before the If node. Handle those with an error workflow (below).

Write a better condition ​

The If node renders its condition as a template and reads the result as text. It is false when the text is empty, false, 0, null or undefined, and true otherwise. That is why {{answer}} means "the model said something".

Keep the whole expression inside the braces. Text outside them is not evaluated: a condition such as {{answer}} === 'true' renders as text such as Yes === 'true', which is never empty, so it is always true.

A condition can also be a JavaScript expression, run in a sandbox. With {{ answer.toLowerCase().includes("late") }}, "Is order 1042 late?" took the true branch and "Where is order 1042?" the false one. Two habits make conditions sturdier:

  • Ask the model for a label, not prose. Tell it to answer with one word, such as refund or question, or parse JSON first with an Output Parser and test {{parsed.intent}}. See extract structured JSON.
  • Use a Switch node for more than two paths. It compares one value with a list of cases and routes to the first match, or to default.

Handle failures with an error workflow ​

Build a second workflow that starts with an Error Trigger and import it. Then add "settings": { "errorWorkflowId": "<its id>" } to this workflow's JSON, inside workflow, and import it again; the editor has no field for this setting. When a run fails, L2M starts it with the failed run's details: source_workflow_id, source_workflow_name, execution_id, error, error_stack and timestamp.

We tested it with an error workflow whose HTTP Request step posts those fields to a local server. With the model down, it received {"wf":"Conditional Branching Flow","err":"openai_compatible request failed after 4 attempt(s): fetch failed","exec":"..."}. The caller of the webhook still gets the 400 error; the error workflow runs on the side, for alerts or clean-up.

Make it yours ​

  • Write the system prompt on the LLM Call node instead of taking system_prompt from the request, so callers cannot rewrite the model's instructions.
  • Replace the two Output nodes with real work on each branch: a reply on one, a ticket or an approval on the other. End with a Merge / Join node if the branches should meet again.

Make it safer ​

These controls are optional; the recipe runs without them.

  • Secure the webhook before anyone else can reach it: a bearer token or an HMAC signature, and idempotency for retried calls. See Secure webhooks.
  • A model's answer is untrusted text. Branch on a fixed label, not on free text that a caller can steer.
  • Cap what callers can spend with a budget.

Further reading ​

This website (not the L2M product) uses Google Analytics, only after you accept cookies. Cookie settings