@@ -3,11 +3,12 @@ title: Pause and Resume
33description : Pause agent execution, perform operations, and resume without losing state.
44---
55
6+
67<Note >
78This example is available on GitHub: [ examples/01_standalone_sdk/09_pause_example.py] ( https://github.com/All-Hands-AI/agent-sdk/blob/main/examples/01_standalone_sdk/09_pause_example.py )
89</Note >
910
10- Pause agent execution mid-task and resume from where it left off :
11+ Pause agent execution mid-task by calling ` conversation.pause() ` :
1112
1213``` python icon="python" expandable examples/01_standalone_sdk/09_pause_example.py
1314import os
@@ -21,7 +22,6 @@ from openhands.sdk import (
2122 Agent,
2223 Conversation,
2324)
24- from openhands.sdk.conversation.state import AgentExecutionStatus
2525from openhands.sdk.tool import Tool, register_tool
2626from openhands.tools.execute_bash import BashTool
2727from openhands.tools.file_editor import FileEditorTool
@@ -54,30 +54,57 @@ agent = Agent(llm=llm, tools=tools)
5454conversation = Conversation(agent, workspace = os.getcwd())
5555
5656
57- print (" Simple pause example - Press Ctrl+C to pause" )
57+ print (" =" * 60 )
58+ print (" Pause and Continue Example" )
59+ print (" =" * 60 )
60+ print ()
61+
62+ # Phase 1: Start a long-running task
63+ print (" Phase 1: Starting agent with a task..." )
64+ conversation.send_message(
65+ " Create a file called countdown.txt and write numbers from 100 down to 1, "
66+ " one number per line. After you finish, summarize what you did."
67+ )
5868
59- # Send a message to get the conversation started
60- conversation.send_message( " repeatedly say hello world and don't stop " )
69+ print ( f " Initial status: { conversation.state.agent_status } " )
70+ print ( )
6171
6272# Start the agent in a background thread
6373thread = threading.Thread(target = conversation.run)
6474thread.start()
6575
66- try :
67- # Main loop - similar to the user's sample script
68- while (
69- conversation.state.agent_status != AgentExecutionStatus.FINISHED
70- and conversation.state.agent_status != AgentExecutionStatus.PAUSED
71- ):
72- # Send encouraging messages periodically
73- conversation.send_message(" keep going! you can do it!" )
74- time.sleep(1 )
75- except KeyboardInterrupt :
76- conversation.pause()
76+ # Let the agent work for a few seconds
77+ print (" Letting agent work for 5 seconds..." )
78+ time.sleep(5 )
7779
80+ # Phase 2: Pause the agent
81+ print ()
82+ print (" Phase 2: Pausing the agent..." )
83+ conversation.pause()
84+
85+ # Wait for the thread to finish (it will stop when paused)
7886thread.join()
7987
80- print (f " Agent status: { conversation.state.agent_status} " )
88+ print (f " Agent status after pause: { conversation.state.agent_status} " )
89+ print ()
90+
91+
92+ # Phase 3: Send a new message while paused
93+ print (" Phase 3: Sending a new message while agent is paused..." )
94+ conversation.send_message(
95+ " Actually, stop working on countdown.txt. Instead, create a file called "
96+ " hello.txt with just the text 'Hello, World!' in it."
97+ )
98+ print ()
99+
100+ # Phase 4: Resume the agent with .run()
101+ print (" Phase 4: Resuming agent with .run()..." )
102+ print (f " Status before resume: { conversation.state.agent_status} " )
103+
104+ # Resume execution
105+ conversation.run()
106+
107+ print (f " Final status: { conversation.state.agent_status} " )
81108```
82109
83110``` bash Running the Example
@@ -90,29 +117,27 @@ uv run python examples/01_standalone_sdk/09_pause_example.py
90117
91118Pause the agent from another thread or after a delay:
92119
93- ``` python highlight={4-6,9 }
94- import threading
95- import time
120+ ``` python highlight={11 }
121+ thread = threading.Thread( target = conversation.run)
122+ thread.start()
96123
97- def pause_after_delay ( conversation , seconds ):
98- time.sleep( seconds)
99- conversation.pause( )
124+ # Let the agent work for a few seconds
125+ print ( " Letting agent work for 5 seconds... " )
126+ time.sleep( 5 )
100127
101- thread = threading.Thread(target = pause_after_delay, args = (conversation, 5 ))
102- thread.start()
103- conversation.run() # Will pause after 5 seconds
128+ # Phase 2: Pause the agent
129+ print ()
130+ print (" Phase 2: Pausing the agent..." )
131+ conversation.pause()
104132```
105133
106- ### Resuming Execution
107134
108- Resume the paused conversation after performing operations:
135+ ### Resuming Execution
109136
110- ``` python highlight={4-5}
111- # Agent is paused, perform operations
112- process_results()
137+ Resume the paused conversation after performing operations by calling ` conversation.run() ` again:
113138
114- conversation.resume()
115- conversation.run() # Continues from where it paused
139+ ``` python
140+ conversation.run()
116141```
117142
118143## Next Steps
0 commit comments