This is the most basic example showing how to set up and run an OpenHands agent:
examples/01_standalone_sdk/01_hello_world.py
import osfrom pydantic import SecretStrfrom openhands.sdk import LLM, Conversationfrom openhands.tools.preset.default import get_default_agent# Configure LLM and agent# You can get an API key from https://app.all-hands.dev/settings/api-keysapi_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( model=model, api_key=SecretStr(api_key), base_url=base_url, usage_id="agent",)agent = get_default_agent(llm=llm, cli_mode=True)# Start a conversation and send some messagescwd = os.getcwd()conversation = Conversation(agent=agent, workspace=cwd)# Send a message and let the agent runconversation.send_message("Write 3 facts about the current project into FACTS.txt.")conversation.run()
Running the Example
export LLM_API_KEY="your-api-key"cd agent-sdkuv run python examples/01_standalone_sdk/01_hello_world.py
Start a conversation to manage the agent’s lifecycle:
conversation = Conversation(agent=agent, workspace=cwd)conversation.send_message("Write 3 facts about the current project into FACTS.txt.")conversation.run()
FACTS.txt---------1. This is a Python project using the OpenHands Agent SDK.2. The project includes examples demonstrating various agent capabilities.3. The SDK provides tools for file manipulation, bash execution, and more.