Skip to main content
This example is available on GitHub: examples/01_standalone_sdk/01_hello_world.py
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 os

from pydantic import SecretStr

from openhands.sdk import LLM, Conversation
from 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-keys
api_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 messages
cwd = os.getcwd()
conversation = Conversation(agent=agent, workspace=cwd)

# Send a message and let the agent run
conversation.send_message("Write 3 facts about the current project into FACTS.txt.")
conversation.run()

# Report cost
cost = llm.metrics.accumulated_cost
print(f"EXAMPLE_COST: {cost}")
Running the Example
export LLM_API_KEY="your-api-key"
cd agent-sdk
uv run python examples/01_standalone_sdk/01_hello_world.py

LLM Configuration

Configure the language model that will power your agent:
llm = LLM(
    model=model,
    api_key=SecretStr(api_key),
    base_url=base_url,  # Optional
    service_id="agent"
)

Default Agent

Use the preset agent with common built-in tools:
agent = get_default_agent(llm=llm, cli_mode=True)
The default agent includes BashTool, FileEditorTool, etc. See the tools package source code for the complete list of available tools.

Conversation

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()

Expected Behavior

When you run this example:
  1. The agent analyzes the current directory
  2. Gathers information about the project
  3. Creates FACTS.txt with 3 relevant facts
  4. Completes and exits
Example output file:
FACTS.txt
---------
1. This is a Python project using the OpenHands Software Agent SDK.
2. The project includes examples demonstrating various agent capabilities.
3. The SDK provides tools for file manipulation, bash execution, and more.

Next Steps