Skip to main content

Workflow definitions

A workflow definition is a saved workflow runner plan. Instead of sending every step with each run, you save the plan once, give it a typed input contract and output contract, and run a pinned version of it with only the input. Versions never change after they are saved, so a run that names version: 3 always gets the same steps.

Workflow definitions need a Spanway enterprise license that includes the workflow_definitions feature. Running a definition also needs workflow_runner. Without the license every /v1/workflows/definitions route answers 403.

A workflow template install saves a definition for you, along with the custom agents it runs.

Scenario​

The incident summary run from the workflow runner works, but every engineer who starts it pastes the same two steps and sometimes mistypes a budget. The team saves the plan once as a definition with a typed input, service, and a typed output, summary. From then on a run is one call with the service name, every run of version 1 gets exactly the same steps, and a change to the prompt becomes version 2 without touching the runs already in flight.

Steps​

  1. In the Admin UI open Workflow Runs under Agentic, switch to the Saved workflows tab and click New saved workflow. Fill Name, pick Who can see and run it, paste the version as JSON in Version (plan, input_contract, output_contract and outputs as JSON) and click Save workflow. The same definition from the API looks like this.

    curl -X POST http://localhost:4000/v1/workflows/definitions/new \
    -H "Authorization: Bearer sk-my-key" -H "Content-Type: application/json" \
    -d '{
    "name": "incident-summary",
    "description": "Summarise the last week of incidents for one service",
    "visibility": "private",
    "version": {
    "plan": {
    "mode": "sequential",
    "run_budget_usd": 2.0,
    "steps": [
    {"key": "research", "target": {"kind": "a2a_agent", "agent_id": "research-agent"},
    "message": "List the incidents for {{input.service}} in the last week", "budget_usd": 1.0},
    {"key": "write", "target": {"kind": "custom_agent", "custom_agent_id": "writer-agent"},
    "message": "Write a short summary of: {{steps.research.output}}", "budget_usd": 0.5}
    ]
    },
    "input_contract": {"fields": [{"name": "service", "type": "string", "max_chars": 100}]},
    "output_contract": {"fields": [{"name": "summary", "type": "string"}]},
    "outputs": {"summary": "{{steps.write.output}}"}
    }
    }'
  2. Run it. Click the workflow's name, pick the Version and start a run from the Runs tab with a Saved workflow step, or call the run route with the input the contract asks for.

    curl -X POST http://localhost:4000/v1/workflows/definitions/run \
    -H "Authorization: Bearer sk-my-key" -H "Content-Type: application/json" \
    -d '{"workflow_id": "wf_01J...", "version": 1, "input": {"service": "checkout"}}'
  3. To change the prompt, open the workflow, click Add a version, edit the JSON and click Save as version 2, or call POST /v1/workflows/definitions/versions/new with the new content.

  4. To share a value such as a region with every workflow of an organization, open the Variables tab, pick the Organization, fill Variable name and Value and click Save variable, then read it in a step as {{vars.support_region}}.

Expected result​

The create call answers 200 with the definition and its first version.

{
"definition": {"workflow_id": "wf_01J...", "name": "incident-summary", "description": "Summarise the last week of incidents for one service", "visibility": "private", "owner_user_id": "jane", "latest_version": 1, "halted_at": null},
"version": {"workflow_id": "wf_01J...", "version": 1, "content_hash": "sha256:...", "spec": {"plan": {"mode": "sequential", "steps": ["..."]}}, "input_schema": {"type": "object", "properties": {"service": {"type": "string", "maxLength": 100}}}, "output_schema": {"type": "object", "properties": {"summary": {"type": "string"}}}}
}

The run call answers 200 with the run and its steps, the same shape the runner returns, and the run's output carries summary once the last step finished. The Saved workflows tab lists the workflow with its Latest version and whether New runs are allowed.

Save a definition​

curl -X POST http://localhost:4000/v1/workflows/definitions/new \
-H "Authorization: Bearer sk-my-key" -H "Content-Type: application/json" \
-d '{
"name": "incident-summary",
"description": "Summarise the last week of incidents for one service",
"visibility": "private",
"version": {
"plan": {
"mode": "sequential",
"run_budget_usd": 2.0,
"steps": [
{"key": "research", "target": {"kind": "a2a_agent", "agent_id": "research-agent"},
"message": "List the incidents for {{input.service}} in the last week", "budget_usd": 1.0},
{"key": "write", "target": {"kind": "custom_agent", "custom_agent_id": "writer-agent"},
"message": "Write a short summary of: {{steps.research.output}}", "budget_usd": 0.5}
]
},
"input_contract": {"fields": [{"name": "service", "type": "string", "max_chars": 100}]},
"output_contract": {"fields": [{"name": "summary", "type": "string"}]},
"outputs": {"summary": "{{steps.write.output}}"}
}
}'

The response holds the definition, its workflow_id and version 1 with the JSON Schemas the gateway generated from the contracts.

The plan takes the same fields as a runner start: mode, steps, run_budget_usd, max_parallel, deadline_seconds and on_failure. Every run the version starts, from this route or a trigger, carries its on_failure, so a run with one needs the workflow_retries licence feature. A version that runs as a sub-workflow step leaves failure handling to its parent run, so its own on_failure is not used there. A version is checked when it is saved, so a definition that could never run is refused with a 422 that names the place in the version, such as steps.write or outputs.summary:

  • The plan passes the same checks as a runner start (unique step keys, known dependencies, no cycles, step outputs used only after the step runs, step budgets within the run budget).
  • Every {{input.<field>}} in a step message or output template names a field of the input contract.
  • Every required output field has a template, and every template names a field of the output contract, an existing step or a declared input.
  • On Spanway Cloud, on_failure does not send an alert, because a run that would send one is refused (the 422 names plan.on_failure).

Contracts​

A contract is a list of fields. Each field has a name (lowercase letters, digits and underscores), a type (string, integer, number, boolean, object or array), required (default true), an optional description and, for strings only, max_chars. A contract holds at most 32 fields.

The gateway builds the JSON Schema from the fields itself, as a closed object with no $ref, and validates run input against it. You cannot upload your own JSON Schema. Before validation, input larger than 256,000 bytes or nested deeper than 16 levels is refused. An integer is accepted where a number is declared, and a boolean is never accepted as a number. A validation error names the field and the kind of problem, never the value you sent, so input never ends up in error messages, logs or audit rows.

Versions​

curl -X POST http://localhost:4000/v1/workflows/definitions/versions/new \
-H "Authorization: Bearer sk-my-key" -H "Content-Type: application/json" \
-d '{
"workflow_id": "wf_01J...",
"version": {
"plan": {
"mode": "sequential",
"run_budget_usd": 1,
"steps": [
{"key": "write", "target": {"kind": "custom_agent", "custom_agent_id": "writer-agent"},
"message": "Write a short summary of the incidents for {{input.service}}", "budget_usd": 0.5}
]
},
"input_contract": {"fields": [{"name": "service", "type": "string", "max_chars": 100}]},
"output_contract": {"fields": [{"name": "summary", "type": "string"}]},
"outputs": {"summary": "{{steps.write.output}}"}
}
}'

The version object has the same shape as the one in Save a definition, and the API reference lists every field.

A new version gets the next number. Two saves at the same moment get two different numbers, never the same one. Sending exactly the content of the latest version again returns that version unchanged and writes no audit row, so saving is safe to retry. Each version stores a SHA-256 content_hash of its canonical content.

Compare and restore versions​

When a new version of a saved workflow misbehaves, you want to see exactly what changed and put the old one back without retyping it. Compare lists every setting, step, prompt and contract field that differs between two versions with its old and new value, and restore saves an earlier version's content as the new latest version.

  1. On the Saved workflows tab click the workflow's name, pick the version to look at in Version, and pick another one in Compare version N with. The table shows Where, What (Added, Removed or Changed) and the value in each version. From the API:

    curl "http://localhost:4000/v1/workflows/definitions/compare?workflow_id=wf_01J...&from_version=1&to_version=2" \
    -H "Authorization: Bearer sk-my-key"
  2. To go back, pick the older version in Version, click Make version N the latest again and confirm with Restore, or call the API.

    curl -X POST http://localhost:4000/v1/workflows/definitions/versions/restore \
    -H "Authorization: Bearer sk-my-key" -H "Content-Type: application/json" \
    -d '{"workflow_id": "wf_01J...", "version": 1}'

The compare call answers with the two content hashes, identical, and one entry per change, sorted by path.

{
"workflow_id": "wf_01J...", "from_version": 1, "to_version": 2,
"from_content_hash": "sha256:1f...", "to_content_hash": "sha256:9c...", "identical": false,
"changes": [
{"path": "plan.steps[write].message", "kind": "changed",
"before": "Write a short summary of: {{steps.research.output}}",
"after": "Write a three line summary of: {{steps.research.output}}"}
]
}

Steps are matched by their key and contract fields by their name, so a step added in the middle shows as one added step instead of every later step changed. When the same steps or fields only swap places, which changes the order a sequential plan runs them in, the list itself shows as changed, for example plan.steps with the old and new order of keys. Other lists are compared position by position, and anything nested deeper than 12 levels is shown as one changed value. Anyone who can see the definition may compare its versions, and comparing changes nothing.

Restore answers like adding a version: the new version number, its content and its hash, which equals the hash of the restored version. Nothing is deleted, so every older version stays, and runs and triggers pinned to a version keep using it until you move them. The restored content is checked again as if it were saved today, so a step that calls a workflow you can no longer see answers 422. The same people who may add a version may restore one, the restore is written to the audit log with restored_from_version, and restoring content equal to the latest version returns it unchanged with no audit row.

Run a version​

curl -X POST http://localhost:4000/v1/workflows/definitions/run \
-H "Authorization: Bearer sk-my-key" -H "Content-Type: application/json" \
-d '{"workflow_id": "wf_01J...", "version": 1, "input": {"service": "checkout"}}'

The input is checked against the version's input contract and the run starts exactly like a runner start, as the calling virtual key (or the key named in run_as_key from the Admin UI), with every agent checked for that key. The definition's author lends only the plan, never their key, budget or agent access. run_budget_usd may be set to override the version's run budget. The run is named after the definition and the gateway adds workflow_definition_id and workflow_definition_version to its metadata. A raw runner start that sends either key, or any other workflow_definition_ key, is refused with reserved_metadata, so those keys always mean the run came from this route. Send an Idempotency-Key header to make retries safe, as on the runner's start route. A retry with the same key and body returns the first run with Idempotent-Replayed: true instead of starting a second one.

Organization variables​

A variable is a named, non-secret value that every definition owned by one organization can read, such as a support region, a team mailbox or a policy version. A step message or a child workflow input reads it as {{vars.<name>}}.

curl -X POST http://localhost:4000/v1/workflows/variables/set \
-H "Authorization: Bearer sk-my-key" -H "Content-Type: application/json" \
-d '{"organization_id": "org_01J...", "name": "support_region", "value": "eu-west"}'

GET /v1/workflows/variables/list?organization_id= lists them for members of that organization, and POST /v1/workflows/variables/delete with organization_id and name removes one. Only a proxy admin or an admin of that organization can set or delete a variable, and each change is written to the audit log. An organization holds up to 200 variables, names are lowercase letters, digits and underscores, and a value is at most 4,000 characters.

Variables are for plain settings only. Keep keys and tokens in MCP credentials or your secret manager. A name that looks like a secret (for example api_key or slack_token), a value that starts like a known key format, or a value that contains {{ is refused with 422.

Variables are filled in when a run starts, from the organization that owns the definition, so the run keeps the values it started with even if someone changes a variable later. A run that uses a variable the organization has not set is refused with 422 before anything runs. A definition with no owning organization cannot use variables. Values fill the steps of the definition being run, so a child workflow reads its own organization's variables when it is run on its own, and receives values from its parent only through its inputs.

In the Admin UI, the Variables tab of the Workflow Runs page lists an organization's variables and lets a proxy admin or an admin of that organization set, edit and delete them. Other members see the values with a note that they cannot change them.

Call a definition from a step​

A step can run a saved version as a sub-workflow instead of calling an agent. This needs the workflow_subworkflows feature as well as workflow_runner, both to save a version with such a step and to start a run that has one.

{
"key": "summarise",
"target": {
"kind": "workflow",
"workflow_id": "wf_01J...",
"version": 3,
"inputs": {
"service": {"kind": "template", "template": "{{input.service}}"},
"days": {"kind": "literal", "value": 7}
}
},
"budget_usd": 1.5,
"timeout_seconds": 1800
}

A step names one exact version. There is no "latest", so a version you saved keeps calling the same steps after its child gets new versions, and because a version can only pin versions that already exist, a definition can never end up calling itself. Each input is either a template or a literal value, and the names must be exactly the child's input contract fields, with every required field present. A template that is a single placeholder keeps the value's type, so {{input.days}} passes an integer as an integer, and any other template is rendered to a string. The rendered values are checked against the child's input contract before the child starts, and a mismatch fails the step with input_invalid, naming the field and never the value.

A workflow step has no message, runs its child once (retry.max_attempts stays 1, retries belong to the child's own steps) and takes no total_timeout_seconds. Its timeout_seconds bounds the whole child run and may be up to 86,400 seconds, while an agent step stays at 3,600. When the child completes, the step's output is the child's output object as compact JSON. Later steps read a field of it with {{steps.<key>.output.<field>}}, and the field must be one the child's output contract declares. Only sub-workflow steps have output fields. A child output too large for a step output fails the step with output_invalid instead of being cut off, and a child whose outputs do not match its own output contract fails with output_invalid too.

The child is an ordinary runner run linked to its parent step. It runs as the parent run's key, never as the definition's author, so every agent in it is checked for that key and the key's budgets and route limits apply to each call. Spawning a child is checked against the /v1/workflows/runner/start route of the run's key. The child's run budget is the smaller of what the parent step and the parent run have left, and the step fails with budget_exhausted when the child's step budgets add up to more. The child's deadline is the earlier of the parent's deadline and the step's timeout.

LimitDefault
Nesting below a root run (general_settings.workflow_runner.max_call_depth)4
Steps in a run and all its sub-workflows (general_settings.workflow_runner.max_expanded_steps)500
Running steps across one run tree (WORKFLOW_TREE_MAX_RUNNING_STEPS)16

Operators can lower the first two in general_settings.workflow_runner, never raise them. While a child runs, its parent step is waiting. A waiting step holds no lease and uses one of its run's max_parallel slots. A replica settles waiting steps on every poll, so each level of nesting adds up to one poll interval of delay.

When a version is saved, every version it pins must be visible to the author, must not be halted, and must be at least as widely shared as the definition being saved. A team definition can pin definitions of the same team or org definitions, and an org definition can pin only org definitions of its own organization, so a shared definition never depends on something its users cannot see. Depth, the step limit and the input names are checked across the whole tree. When a run starts, the tree is checked again as the run's key sees it, and every agent anywhere in it is checked for that key. A refusal names the path to the problem, such as Step summarise > workflow 4f2c v3 > step lookup. A version the run's key cannot see answers 404, the same as a missing one, and a halted one answers 409.

Cancel, resume and re-run only the root run. On a sub-workflow run they answer 409 and name the root. Cancelling the root cancels its children on the next poll, and the root ends cancelled once they have stopped. When a step of the parent fails, its running children are cancelled the same way. Resuming the root resumes its failed or cancelled children first, deepest first, so steps that already succeeded inside them are not run or paid for again, and the parent step waits on the same child instead of starting a new one. The run list shows root runs only, and GET /v1/workflows/runner/runs/{run_id}/steps on a parent shows each waiting step's child_run_id. The run ledger of a root run includes the requests of its sub-workflow runs.

Admin UI​

The Saved workflows tab of the Workflow Runs page under Agentic lists the definitions you can see with who can see them, their latest version and whether new runs are allowed. New saved workflow opens a form for the name, description, who can see and run it and the first version as JSON, prefilled with a one-step example to edit. A definition's name opens its detail sheet with its id, its content hash, its inputs, the plan of the version you pick and the other saved workflows that call it, including how many more you cannot see when you manage it. Add a version starts from the version on screen and saves it as the next one, and saving the latest content again changes nothing. Compare and restore shows what changed between the version on screen and another one, and, when an older version is on screen, Make version N the latest again saves it as the next version after a confirmation. Proxy admins also get Stop new runs, Allow new runs again and Delete, which asks for a confirmation and is refused while another definition pins it. Other roles see a note that those stay with proxy admins, and read-only roles see no write button at all. To run a saved workflow, use a Saved workflow step in Start run on the Runs tab, a trigger, or Run on an installed template. Without the workflow_definitions feature, the tab says so.

Who can do what​

Definitions use the same scopes as custom agents. A private definition belongs to the user who saved it, a team definition is shared with one team (owner_team_id) and an org definition is shared with the members of one organization.

An org definition belongs to the organization of the person who saves it, the same way an org custom agent does, and it is returned as owner_organization_id. People in other organizations or Cloud tenants cannot see, run or pin it. An org definition that belongs to no organization, which includes every one saved before this change, is visible only to its owner, proxy admins and admin viewers until someone in an organization adds a version, and then it takes that organization. A definition that already belongs to an organization keeps it.

ActionWho
Save a private definitionAny user whose role can write
Save a team definitionAn admin of that team or a proxy admin
Save an org definitionProxy admins
See and run a definitionIts owner, members of its team for team definitions, members of its organization for org definitions, proxy admins and admin viewers
Add or restore a versionIts owner, an admin of its team or a proxy admin
Compare versionsAnyone who can see it
Halt or release a definitionProxy admins
Delete a definitionProxy admins, and only while no other definition pins one of its versions

A definition you cannot see answers 404, the same as one that does not exist. GET /v1/workflows/definitions/usages lists the definitions that pin a version of this one and that you can see, and tells its managers how many others they cannot see.

Halting a definition stops new runs of every version, for example while its steps are being reviewed. Runs already going keep going. Deleting removes the definition and all its versions for good and is refused with a 409 while another definition pins it.

usages lists the definition versions that call this one from a step, recorded when they were saved.

Saving, versioning, restoring, halting, releasing and deleting each write an audit row against the definition. Admin viewers and internal user viewers can read definitions but not change or run them unless a custom role grants workflows.write. Halt and delete stay with proxy admins whatever the custom role. Deleting a definition turns off every workflow trigger that starts it, with disabled_reason set to definition_deleted and an audit row for each.

MethodPathPurpose
GET/v1/workflows/definitions/listDefinitions you can see, most recently changed first
GET/v1/workflows/definitions/info?workflow_id=&version=A definition with one version, the latest by default
GET/v1/workflows/definitions/usages?workflow_id=Definitions that pin this one
GET/v1/workflows/definitions/compare?workflow_id=&from_version=&to_version=What changed between two versions
POST/v1/workflows/definitions/versions/restoreSave an earlier version as the new latest
POST/v1/workflows/definitions/newSave a definition with its first version
POST/v1/workflows/definitions/versions/newAdd a version
POST/v1/workflows/definitions/runRun a pinned version
POST/v1/workflows/definitions/haltHalt ("halted": true) or release a definition
POST/v1/workflows/definitions/deleteDelete a definition and its versions

Data subject requests​

Definitions are configuration, so no retention period deletes them. Erasing a user through data subject requests deletes their private definitions that no other definition pins, and replaces their user id with the pseudonym on every other definition and version they own or wrote. Exports include the definitions they own or edited with the versions they wrote.

Errors​

StatusWhat it meansFix
403 workflow_definitions license messageThe license lacks the workflow_definitions feature.Add the feature to the license or ask sales@spanway.ai.
403 "Your role can read workflow definitions but not change them"A read only role tried to save, version, halt or delete.Use a role with workflows.write, or a proxy admin for halt and delete.
403 on a team or organization visibilityYou are not an admin of that team, or not a proxy admin for an organization.Pick Only me or a team you administer.
404The definition or version does not exist or is not visible to you, including a version named in compare or restore.Check the id and version on the Saved workflows tab.
409 "A proxy admin halted workflow definition ..." on runNew runs of the definition are stopped by a proxy admin.Click Allow new runs again, as a proxy admin.
409 on deleteAnother definition still calls this one from a step.Open Used by on the workflow and remove the callers first.
422 "the message uses vars., which this definition's organization has not set. Set it with POST /v1/workflows/variables/set" on runA step reads an organization variable nobody set yet.Set the variable on the Variables tab.
422The version content, the input or a variable value failed validation, for example a value that looks like a key.Fix the field the detail names.
403 "Only a proxy admin or an admin of this organization can do this"Someone else tried to set or delete an organization variable.Ask an organization admin or a proxy admin.
403 "You are not a member of this organization"The variable list was asked for an organization you do not belong to.Pick your own organization.
404 "Variable is not set"The variable to delete does not exist.Check the name on the Variables tab.
409 "An organization can hold at most 200 variables"The organization has the most variables it may hold.Delete variables you no longer need.