Pause agent execution mid-task by calling conversation.pause():
examples/01_standalone_sdk/09_pause_example.py
Copy
Ask AI
import osimport threadingimport timefrom pydantic import SecretStrfrom openhands.sdk import ( LLM, Agent, Conversation,)from openhands.sdk.tool import Tool, register_toolfrom openhands.tools.execute_bash import BashToolfrom openhands.tools.file_editor import FileEditorTool# Configure LLMapi_key = os.getenv("LLM_API_KEY")assert api_key is not None, "LLM_API_KEY environment variable is not set."model = os.getenv("LLM_MODEL", "openhands/claude-sonnet-4-5-20250929")base_url = os.getenv("LLM_BASE_URL")llm = LLM( usage_id="agent", model=model, base_url=base_url, api_key=SecretStr(api_key),)# Toolsregister_tool("BashTool", BashTool)register_tool("FileEditorTool", FileEditorTool)tools = [ Tool( name="BashTool", ), Tool(name="FileEditorTool"),]# Agentagent = Agent(llm=llm, tools=tools)conversation = Conversation(agent, workspace=os.getcwd())print("=" * 60)print("Pause and Continue Example")print("=" * 60)print()# Phase 1: Start a long-running taskprint("Phase 1: Starting agent with a task...")conversation.send_message( "Create a file called countdown.txt and write numbers from 100 down to 1, " "one number per line. After you finish, summarize what you did.")print(f"Initial status: {conversation.state.agent_status}")print()# Start the agent in a background threadthread = threading.Thread(target=conversation.run)thread.start()# Let the agent work for a few secondsprint("Letting agent work for 2 seconds...")time.sleep(2)# Phase 2: Pause the agentprint()print("Phase 2: Pausing the agent...")conversation.pause()# Wait for the thread to finish (it will stop when paused)thread.join()print(f"Agent status after pause: {conversation.state.agent_status}")print()# Phase 3: Send a new message while pausedprint("Phase 3: Sending a new message while agent is paused...")conversation.send_message( "Actually, stop working on countdown.txt. Instead, create a file called " "hello.txt with just the text 'Hello, World!' in it.")print()# Phase 4: Resume the agent with .run()print("Phase 4: Resuming agent with .run()...")print(f"Status before resume: {conversation.state.agent_status}")# Resume executionconversation.run()print(f"Final status: {conversation.state.agent_status}")
Running the Example
Copy
Ask AI
export LLM_API_KEY="your-api-key"cd agent-sdkuv run python examples/01_standalone_sdk/09_pause_example.py
Pause the agent from another thread or after a delay:
Copy
Ask AI
thread = threading.Thread(target=conversation.run)thread.start()# Let the agent work for a few secondsprint("Letting agent work for 5 seconds...")time.sleep(5)# Phase 2: Pause the agentprint()print("Phase 2: Pausing the agent...")conversation.pause()