Workflow Timeout Template
The Workflow Timeout template demonstrates how BindAI’s workflow executor detects when a workflow has exceeded its configured elapsed-time limit. Timeout checking is implemented in the workflow executor. Before processing a node, the executor compares the elapsed time since the workflow started with the configuredTimeoutPolicy.
If the elapsed time exceeds the configured limit, the executor enters the normal workflow failure path with the error:
Purpose
This template demonstrates how to:- Configure a workflow timeout policy
- Limit the elapsed time allowed for a workflow
- Detect workflows that exceed their configured duration at executor check points
- Handle timeout detection through normal workflow failure handling
- Protect against workflows that take unexpectedly long to complete
Workflow Structure
A timeout does not introduce additional workflow nodes. The normal workflow structure remains:TimeoutPolicy
Timeout configuration is represented byTimeoutPolicy:
A policy can be assigned to the workflow execution context:
Timeout Policy Defaults
TimeoutPolicy is a dataclass.
For example:
fail_workflow field currently does not change executor behavior.
When the executor detects that the timeout has been exceeded, it enters workflow failure handling regardless of the value of fail_workflow.
How Timeout Detection Works
The workflow executor checks the timeout during its main execution loop, before processing a node. Conceptually:started_at timestamp. Conceptually, the calculation is:
Timeout Trigger
If the elapsed time exceeds the configured duration when the executor checks it, the executor enters its failure path. The failure is generated using the error message:WorkflowResult.
The timeout mechanism does not run as a separate background timer.
Timeout Failure Behavior
A timeout is handled as a normal workflow failure. The failure path performs the workflow executor’s configured failure handling, which includes:- Executing registered compensation callbacks.
- Recording the timeout error.
- Marking the workflow context as completed.
- Recording the finish time.
- Emitting a
workflow.failedevent. - Persisting the workflow instance.
- Storing failed workflow history.
- Returning an unsuccessful
WorkflowResult.
Timeout Checking and Node Execution
Timeout detection occurs in the executor’s workflow loop before the next node is processed. For example:Total Workflow Duration
The timeout is measured from the workflow context’sstarted_at timestamp.
The executor does not reset the timeout after each node.
For example:
Successful Execution
If the workflow completes without a timeout being detected, execution proceeds through the normal success path.Timed-Out Execution
If the workflow has exceeded its timeout when the executor performs its next timeout check, the failure path is entered:WorkflowResult is unsuccessful and records the timeout error:
Workflow Events
A successful workflow emits the normal completion event:Execution History
A timed-out workflow is recorded as a failed execution through the workflow history mechanism. The recorded result identifies the unsuccessful execution and its error, including:Persistence
When timeout detection enters the workflow failure path, the executor persists the workflow instance before returning the failure result. This preserves the failed execution state through the configured workflow persistence mechanism. Persistence behavior depends on the store configured for the workflow infrastructure.fail_workflow Configuration
TimeoutPolicy contains this field:
WorkflowExecutor does not inspect fail_workflow.
When the timeout condition is exceeded, the executor directly invokes its normal failure path:
Timeout and Retry
Timeouts and retries address different failure scenarios.
Conceptually:
context.started_at timestamp.
Retries can increase total workflow duration, so the workflow timeout and retry policy should be configured with one another in mind.
Note that the current timeout check cannot interrupt a node while it is running, even if that node is retrying after an exception.
Timeout and Loops
Timeouts can be useful for workflows containing loops. For example:Timeout and Parallel Workflows
Timeout checking also uses the workflow context’s start time when a workflow contains parallel-style fan-out. For example:context.started_at value. The timeout policy does not define independent deadlines for individual branches.
The ParallelNode itself does not implement a separate timeout mechanism. A node that blocks during parallel execution can delay the executor’s next timeout check.
Do not treat the workflow timeout as a guarantee that all parallel work will be cancelled when the configured duration expires.
Long-Running Operations
Timeouts can be useful for workflows involving:- AI providers
- External APIs
- Databases
- Document processing
- Cloud services
- Network operations
- External integrations
Choosing a Timeout
Timeout values should reflect the expected total workflow duration, including the time spent executing nodes and any retries. The following are illustrative planning ranges, not BindAI defaults or enforced limits.
Choose a threshold based on actual workload characteristics and operational requirements. Account for variability in external services and remember that detection can be delayed while a node is executing.
Important Implementation Boundaries
The current timeout implementation:- Measures elapsed time from
context.started_at. - Checks the timeout in the executor’s workflow loop before processing a node.
- Compares elapsed time with
TimeoutPolicy.seconds. - Uses a strict greater-than comparison for the configured threshold.
- Enters normal workflow failure handling with
"Workflow timeout."when the limit is exceeded. - Uses the existing failure lifecycle, including compensation callbacks and failure events.
- Persists the workflow instance through the configured persistence mechanism.
- Records failed workflow history through the configured history mechanism.
- Interrupt a node that is already executing.
- Guarantee that execution stops exactly at the configured threshold.
- Create a separate timeout policy for each node.
- Create independently managed timeouts for parallel branches.
- Reset the workflow start time after retries.
- Use
fail_workflowto control timeout behavior. - Emit a dedicated timeout-specific event.
- Automatically cancel external operations.
Best Practices
- Set a realistic timeout for the entire workflow.
- Remember that elapsed time is measured from
context.started_at. - Account for the combined duration of node execution and retries.
- Do not assume the timeout interrupts an already-running node.
- Do not assume every node receives its own timeout duration.
- Do not assume
fail_workflow=Falsedisables timeout failure. - Use explicit loop termination conditions.
- Consider the timeout behavior of external clients and integrations.
- Combine timeouts with retry policies when appropriate.
- Monitor
workflow.failedevents and inspect failed execution history for timeout errors.
Related Templates
The Workflow Timeout template relates to:08.3 workflow-loop.md08.4 workflow-parallel.md08.6 workflow-retry.md08.8 workflow-schedule.md
Summary
The Workflow Timeout template demonstrates how BindAI detects when a workflow’s elapsed execution time exceeds a configured threshold. ATimeoutPolicy specifies the duration in seconds:
WorkflowExecutor checks elapsed time from context.started_at before processing a node.
If the configured threshold has been exceeded at a check point, the executor enters normal workflow failure handling with:
workflow.failed event, persists the workflow instance, stores failed history, and returns an unsuccessful WorkflowResult.
The policy also contains fail_workflow, but the current executor does not use that field.
Timeouts therefore provide a workflow-level elapsed-time check, not a hard cancellation mechanism. A node that is already executing can delay timeout detection, and additional cancellation support is required when running work must be forcibly stopped.