Workflow JSON cookbook
Copy-and-paste workflow graphs: tool chains, Lua and LLM steps, loops, approvals, and child workflows.
On this page
A workflow graph is a JSON document: a list of steps and the order they depend on. You normally build it on the canvas in Workflow Studio, but you can paste a graph into the JSON editor, use Import, or send it through the API.
Who this is for: people who want a working starting point.
What you will do: copy an example, replace the placeholders, and check it with Dry-run.
How to use these examples
- Open a workflow, switch to JSON (see JSON editor), paste the example, and click Apply to canvas. Or use Import in the toolbar's JSON menu (or under More in a narrow window).
- Replace every
REPLACE_…value: MCP instance ids, agent ids, workflow ids. Import lets you map tool servers to your own instances. - Click Dry-run, fix any messages, then Run.
You should see the steps on the canvas, connected in the order of their depends_on.
Graph structure
| Field | Description |
|---|---|
steps |
The list of steps. Required, at least one. |
input_schema |
Optional. Describes the start input (type, properties, defaults). |
workflow_id, name, description |
Optional labels. Importing does not use them to pick a workflow. |
Every step needs an id, a type, and a name. The types are mcp, lua, llm, agent, human, workflow, for_each, while, and external_agent. See Step types for every field.
Example 1: a tool chain with parallel branches
Two steps depend on the same step, so they run at the same time. A last step waits for both.
{
"steps": [
{
"id": "fetch-order",
"type": "mcp",
"name": "Fetch order",
"instance_id": "REPLACE_MCP_INSTANCE",
"tool_name": "get_order",
"tool_args": { "order_id": "{{input.order_id}}" },
"timeout_s": 30
},
{
"id": "fetch-customer",
"type": "mcp",
"name": "Fetch customer",
"instance_id": "REPLACE_MCP_INSTANCE",
"tool_name": "get_customer",
"tool_args": { "customer_id": "{{steps.fetch-order.result.customer_id}}" },
"depends_on": ["fetch-order"],
"timeout_s": 30
},
{
"id": "fetch-shipping",
"type": "mcp",
"name": "Fetch shipping status",
"instance_id": "REPLACE_MCP_INSTANCE",
"tool_name": "get_shipment",
"tool_args": { "order_id": "{{input.order_id}}" },
"depends_on": ["fetch-order"],
"timeout_s": 30
},
{
"id": "summarise",
"type": "lua",
"name": "Combine results",
"depends_on": ["fetch-customer", "fetch-shipping"],
"script": "return { summary = steps['fetch-customer'].result.name .. ': ' .. steps['fetch-shipping'].result.status }",
"timeout_s": 5
}
]
}fetch-customer and fetch-shipping both depend only on fetch-order, so they run in parallel. Tool names and fields depend on your connector. Browse them in the connector catalog.
Example 2: tool, Lua, and LLM
Fetch data, calculate a price in Lua, and have a model write one line.
{
"input_schema": {
"type": "object",
"properties": {
"sku": { "type": "string" },
"email": { "type": "string" }
},
"required": ["sku", "email"]
},
"steps": [
{
"id": "fetch-product",
"type": "mcp",
"name": "Fetch product",
"instance_id": "REPLACE_MCP_INSTANCE",
"tool_name": "get_product",
"tool_args": { "sku": "{{input.sku}}" },
"timeout_s": 30
},
{
"id": "fetch-customer",
"type": "mcp",
"name": "Fetch customer",
"instance_id": "REPLACE_MCP_INSTANCE",
"tool_name": "get_customer",
"tool_args": { "email": "{{input.email}}" },
"timeout_s": 30
},
{
"id": "calculate-pricing",
"type": "lua",
"name": "Calculate discount",
"depends_on": ["fetch-product", "fetch-customer"],
"timeout_s": 5,
"script": "local product = steps['fetch-product'].result\nlocal customer = steps['fetch-customer'].result\nlocal discount = 0\nif customer.tier == 'gold' then discount = 0.15 elseif customer.tier == 'silver' then discount = 0.10 end\nlocal total = product.price * (1 - discount)\nreturn { total = math.floor(total * 100) / 100, summary = string.format('%s: %.2f', product.name, total) }"
},
{
"id": "generate-invoice",
"type": "llm",
"name": "Write invoice line",
"model_ref": "REPLACE_MODEL_REF",
"prompt": "Write a one-line invoice for: {{steps.calculate-pricing.result.summary}}",
"depends_on": ["calculate-pricing"],
"timeout_s": 60
}
]
}Replace REPLACE_MODEL_REF with a model from your Providers catalog. The reply is at {{steps.generate-invoice.result.text}}.
Example 3: for_each over a list
A Lua step builds the list, then each item is processed.
{
"steps": [
{
"id": "list-items",
"type": "lua",
"name": "Build item list",
"script": "return { items = { 'alpha', 'beta', 'gamma' } }",
"timeout_s": 5
},
{
"id": "process-each",
"type": "for_each",
"name": "Process each item",
"depends_on": ["list-items"],
"for_each_items": "{{steps.list-items.result.items}}",
"for_each_max_parallel": 10,
"for_each_body_steps": [
{
"id": "process",
"type": "lua",
"name": "Process one",
"script": "return { index = index, label = tostring(item) }",
"timeout_s": 5
}
],
"timeout_s": 120
}
]
}A for_each step's result has count, max_parallel, succeeded_count, failed_count, and results[]: one entry per item with its index and either its result or an error.
A loop reads its list from a list, so return the list from the step before it, even when it is empty (return { items = items }).
An llm step returns plain text, not a list. If you asked it for JSON, decode the text in a Lua step first (return { items = json.decode(steps["gen"].result.text).items }). For data you must be able to rely on, use an agent step whose agent has an output schema: the agent's reply is checked against the schema, and its fields land on the step result (for example {{steps.extract.result.items}}). See Step types.
A loop body with several steps
Run a small pipeline for each item: an agent, then a Lua step.
{
"id": "triage-each",
"type": "for_each",
"name": "Triage tickets",
"depends_on": ["load-tickets"],
"for_each_items": "{{steps.load-tickets.result.tickets}}",
"for_each_max_parallel": 5,
"for_each_body_steps": [
{
"id": "triage",
"type": "agent",
"name": "Triage one ticket",
"agent_id": "REPLACE_AGENT_ID",
"prompt": "Triage this ticket: {{foreach.item}}"
},
{
"id": "tag",
"type": "lua",
"name": "Tag it",
"depends_on": ["triage"],
"script": "return { ticket = tostring(item), ok = true }"
}
]
}for_each_body_steps is required. Use a list with one step for a single inner step. Body steps run one after another for each item. Set depends_on between body steps to fix their order, as tag does here: body steps with no dependency between them run in alphabetical order of their id, not in the order you list them. A body can contain another for_each or while.
Example 4: while (refine until good)
Repeat until a check passes, with a hard limit.
{
"id": "refine",
"type": "while",
"name": "Refine until the score passes",
"while_mode": "until",
"while_max_iterations": 5,
"while_condition": "{{body.score.pass}}",
"while_body_steps": [
{
"id": "draft",
"type": "lua",
"name": "Draft",
"script": "local n = loop.iteration or 0\nreturn { text = string.rep('x', (n + 1) * 40) }"
},
{
"id": "score",
"type": "lua",
"name": "Score",
"depends_on": ["draft"],
"script": "local n = #tostring(body.draft.text or '')\nreturn { pass = n >= 90 }"
}
]
}The result has:
| Field | What it holds |
|---|---|
iterations |
How many rounds ran (a number) |
max_iterations |
The limit you set |
stopped_reason |
condition when the check passed, max_iterations when the limit was reached |
last_body |
The final round's outputs, by inner step id, for example {{steps.refine.result.last_body.draft.text}} |
iteration_records |
Every round: [{ "iteration": 0, "body": { ... } }, ...] |
A loop that reaches the limit does not fail, so check stopped_reason before you treat the work as finished.
Example 5: a human approval gate
Pause before sending an email.
{
"id": "approve-send",
"type": "human",
"name": "Approve outbound email",
"task_type": "approval",
"approver": { "any_member": true },
"depends_on": ["draft-email"],
"task_payload": {
"title": "Review email draft",
"message": "Confirm before sending to {{input.recipient}}",
"draft": "{{steps.draft-email.result.text}}"
}
}Answer it from Command Center or the API. Later steps can read {{steps.approve-send.result.approved}}. See Human tasks.
Example 6: run another workflow
{
"id": "call-child",
"type": "workflow",
"name": "Run the enrichment workflow",
"depends_on": ["prep"],
"workflow_id": "REPLACE_CHILD_WORKFLOW_ID",
"input": {
"email": "{{steps.prep.result.email}}",
"backfill_after": "{{input.backfill_after | \"2026/01/01\"}}"
},
"wait": true,
"timeout_s": 600
}Leave child_target out while you build, so the child's draft runs. The step's result comes from the child step marked terminal: true, or else the child's last step (output_from). See Step types.
Example 7: skip a step on some runs
{
"id": "transcribe",
"type": "mcp",
"name": "Transcribe audio",
"instance_id": "REPLACE_MCP_INSTANCE",
"tool_name": "transcribe",
"depends_on": ["parse"],
"skip_when": "{{steps.parse.result.kind == \"text\"}}",
"tool_args": { "url": "{{steps.parse.result.audio_url}}" }
}skip_when takes a true/false value or a comparison with == or !=, and you can join several with && and ||, for example {{input.kind == "voice" && input.lang != "en"}}. Parentheses are not supported. A skipped step counts as finished, so steps after it still run. skip_when works on top-level steps only; inside a loop body, use enabled instead.
Example 8: read JSON that a model returns
Ask an llm step to answer in JSON, decode it in a lua step, then use the fields in later steps.
{
"steps": [
{
"id": "classify",
"type": "llm",
"name": "Classify the ticket",
"model_ref": "REPLACE_MODEL_REF",
"prompt": "Classify this support ticket. Reply with JSON only, no other text: {\"category\": \"billing\" | \"bug\" | \"other\", \"urgent\": true | false, \"tags\": [string]}\n\nTicket: {{input.ticket_text}}",
"timeout_s": 60
},
{
"id": "parse",
"type": "lua",
"name": "Read the model's JSON",
"depends_on": ["classify"],
"script": "local ok, data = pcall(json.decode, steps[\"classify\"].result.text)\nif not ok then\n error(\"model did not return valid JSON: \" .. tostring(data))\nend\nreturn { category = data.category, urgent = data.urgent == true, tags = data.tags or {} }",
"timeout_s": 5
},
{
"id": "alert",
"type": "mcp",
"name": "Tell the team",
"depends_on": ["parse"],
"instance_id": "REPLACE_MCP_INSTANCE",
"tool_name": "REPLACE_TOOL_NAME",
"skip_when": "{{steps.parse.result.urgent == false}}",
"tool_args": {
"text": "Urgent {{steps.parse.result.category}} ticket: {{input.ticket_text}}"
}
}
]
}If the model replies {"category":"billing","urgent":true,"tags":["refund"]}, the parse step's result is:
{ "category": "billing", "urgent": true, "tags": ["refund"] }Later steps read it as {{steps.parse.result.category}} and so on. The alert step only runs for urgent tickets.
- The
\nand\"inscriptare only there because the script sits inside JSON. In Studio's Lua editor, or with Edit script in the JSON editor, you write normal Lua. pcallcatches invalid JSON. Without it, bad JSON fails the step with the parse error.- When later steps must be able to rely on the fields, an
agentstep whose agent has an output schema is safer: its reply is checked against the schema, and the fields land on the step result without a Lua step. See Step types.
To go the other way, json.encode turns a table into JSON text, for example for a tool that takes a raw JSON body:
{
"id": "build-body",
"type": "lua",
"depends_on": ["load-orders"],
"script": "return { body = json.encode({ customer = input.customer_id, orders = steps[\"load-orders\"].result.rows }) }"
}Its result is { "body": "{\"customer\":\"c_42\",\"orders\":[...]}" }. See Lua scripts for how values convert.
Template quick reference
| Syntax | Resolves to |
|---|---|
{{input.field}} |
The run's start input |
{{steps.step-id.result.field}} |
An earlier step's output |
{{foreach.item}}, {{foreach.index}} |
The current item and its position (from 0) in a for_each body |
{{body.inner-id.field}} |
A step in the current round of a loop body |
{{while.iteration}}, {{while.prev.inner-id.field}} |
The round number (from 0) and the previous round's output |
In Lua, top-level steps are steps["step-id"].result. Inside a loop body use body.inner_id, item, index, and loop.iteration / loop.prev. See Step types and Template intellisense.
Check before you run
Click Dry-run in Studio, or call:
POST /v1/workflows/{id}/validate
See Dry-run validation.
Common problems
| Problem | What to try |
|---|---|
| Dry-run says a field is unknown | Compare the field name with Step types. |
| A Lua step says it must return a table | End the script with return { ... }. |
| A loop has no items | The step before it must return the list, even an empty one. |
| A tool step has no instance | Replace REPLACE_MCP_INSTANCE with an instance from Integrations. |