Workflow Timeout Template
The Workflow Timeout template demonstrates how BindAI can stop a workflow when its total execution time exceeds a configured limit. The timeout is enforced by the workflow executor. Before each node is executed, the executor compares the elapsed time since the workflow started with the configured timeout duration. If the limit has been exceeded, the workflow fails with the error"Workflow timeout.".
Purpose
This template demonstrates how to:- configure a workflow timeout
- limit total workflow execution time
- detect workflows that run longer than allowed
- fail timed-out workflows predictably
- protect workflows from excessive execution time
Workflow Structure
A timeout does not change the normal workflow structure.Timeout Policy
A workflow timeout is configured using aTimeoutPolicy.
The policy defines the maximum number of seconds allowed for workflow execution.
For example:
How Timeout Detection Works
The executor checks the timeout before executing each node. Conceptually:started_at timestamp.
The executor compares:
Timeout Trigger
If the elapsed execution time exceeds the configured limit, the executor immediately enters the workflow failure path. Conceptually:Timeout Failure Behavior
A timeout is treated as a workflow failure. When_failure() is called, BindAI:
- executes registered compensation handlers
- records the timeout error
- marks the workflow as completed
- records the finish time
- emits a
workflow.failedevent - persists the workflow state
- stores failed execution history
- returns an unsuccessful
WorkflowResult
Timeout and Node Execution
Timeout checking occurs between node executions. For example:Timeout Duration
The timeout duration is measured from the workflow’sstarted_at timestamp.
The executor does not reset the timeout after each node.
For example, with:
Successful Execution
If the workflow finishes before the timeout limit, execution completes normally.Timed-Out Execution
If the workflow exceeds the configured duration before the next node execution, the workflow fails.Workflow Events
A successful workflow emits a completion event:Execution History
Timed-out workflows are stored as failed executions in the workflow history. The history records:Persistence
The workflow state is persisted when the timeout failure occurs. The executor saves the workflow instance before returning the failure result. This allows the failed execution state to remain available to the rest of the workflow infrastructure.Timeout and Retry
Timeouts and retries solve different problems.
A retry policy does not make the timeout disappear.
The workflow timeout is checked independently by the executor before node execution.
Timeout and Loops
Timeouts are particularly useful for workflows containing loops. A loop may intentionally execute many iterations:Timeout and Parallel Execution
Timeouts also apply to workflows containing parallel branches. For example:Long-Running Operations
Timeouts are useful for workflows that interact with:- AI providers
- external APIs
- databases
- document processing systems
- cloud services
- network operations
- external integrations
Choosing a Timeout
Timeout values should reflect the expected total duration of the workflow. For example:
These values are examples rather than framework-enforced defaults.
Choose a timeout based on the actual workload and operational requirements.
Best Practices
- Set a realistic timeout for the entire workflow.
- Remember that the timeout is measured from
started_at. - Do not assume the timeout interrupts an already-running node.
- Use timeouts as protection against unexpectedly long workflows.
- Combine timeouts with retry policies when appropriate.
- Monitor
workflow.failedevents for timeout failures. - Inspect execution history for repeated timeout errors.
- Make loop termination conditions explicit.
- Design long-running operations with the executor’s node-level timeout checking behavior in mind.
Summary
The Workflow Timeout template demonstrates how BindAI limits the total execution time of a workflow. ATimeoutPolicy specifies the maximum duration in seconds. During execution, the WorkflowExecutor checks the elapsed time before each node. If the configured limit has been exceeded, the executor fails the workflow with:
workflow.failed event, persists the execution state, stores failed history, and returns an unsuccessful WorkflowResult.
Timeouts therefore provide a predictable safety mechanism for preventing workflows from continuing indefinitely while remaining compatible with loops, retries, parallel execution, and other workflow features.