sleep function enables agents to pause execution without consuming resources, and resume at a specified time, after a specified duration, or in response to an external event. Workflows that suspend survive restarts, new deploys, and infrastructure changes, whether the suspension takes seconds or months.
See the
sleep() API Reference for the full list of supported duration formats and detailed API documentation.Adding a Sleep Tool
Sleep is a built-in function in Workflow DevKit, so exposing it as a tool is as simple as wrapping it in a tool definition:Define the Tool
Add a sleep tool to your tool definitions:workflows/chat/steps/tools.ts
The
sleep() function must be called from within a workflow context, not from within a step. This is why executeSleep does not have "use step" - it runs in the workflow context where sleep() is available.Duration Formats
sleep() supports multiple duration formats:
lineNumbers
Use Cases
Rate Limiting
When hitting API rate limits, useRetryableError with a delay:
workflows/chat/steps/tools.ts
Scheduled Tasks
Schedule future actions within your agent workflow:lineNumbers
Polling External State
Poll an external service until a condition is met:lineNumbers
Exponential Backoff
Implement custom retry logic with exponential backoff:lineNumbers
Daily Reports
Schedule recurring daily tasks:lineNumbers
Combining Sleep with Other Patterns
Sleep + Human-in-the-Loop
Implement timeouts for human approval:lineNumbers
Sleep + Streaming Updates
Stream progress updates during a long sleep:lineNumbers
Best Practices
Don’t Sleep in Steps
Always callsleep() from workflow-level code, not steps:
lineNumbers
Combine with Steps for I/O
Use steps for I/O operations around sleep:lineNumbers
Handle Sleep Interruptions
Workflows can be cancelled during sleep:lineNumbers
Related Documentation
sleep()API Reference - Full API documentation- Workflows and Steps - Understanding workflow context
- Errors and Retries - Using
RetryableErrorwith delays - Human-in-the-Loop - Combining sleep with approval workflows