Start a conversation in the local environment.
Payload to create a new conversation.
Contains an Agent configuration along with conversation-specific options.
Main agent implementation for OpenHands.
The Agent class provides the core functionality for running AI agents that can interact with tools, process messages, and execute actions. It inherits from AgentBase and implements the agent execution logic. Critic-related functionality is provided by CriticMixin.
Example: >>> from openhands.sdk import LLM, Agent, Tool >>> llm = LLM(model="claude-sonnet-4-20250514", api_key=SecretStr("key")) >>> tools = [Tool(name="TerminalTool"), Tool(name="FileEditorTool")] >>> agent = Agent(llm=llm, tools=tools)
Working directory for agent operations and tool execution
Optional conversation ID. If not provided, a random UUID will be generated.
Controls when the conversation will prompt the user before continuing. Defaults to never.
Initial message to pass to the LLM
If set, the max number of iterations the agent will run before stopping. This is useful to prevent infinite loops.
x >= 1If true, the conversation will use stuck detection to prevent infinite loops.
Secrets available in the conversation
Mapping of tool names to their module qualnames from the client's registry. These modules will be dynamically imported on the server to register the tools for this conversation.
List of plugins to load for this conversation. Plugins are loaded and their skills/MCP config are merged into the agent. Hooks are extracted and stored for runtime execution.
Optional hook configuration for this conversation. Hooks are shell scripts that run at key lifecycle events (PreToolUse, PostToolUse, UserPromptSubmit, Stop, etc.). If both hook_config and plugins are provided, they are merged with explicit hooks running before plugin hooks.
Successful Response
Information about a conversation running locally without a Runtime sandbox.
Unique conversation ID
The agent running in the conversation. This is persisted to allow resuming conversations and check agent configuration to handle e.g., tool changes, LLM changes, etc.
Workspace used by the agent to execute commands and read/write files. Not the process working directory.
Directory for persisting conversation state and events. If None, conversation will not be persisted.
Maximum number of iterations the agent can perform in a single run.
Whether to enable stuck detection for the agent.
Enum representing the current execution state of the conversation.
idle, running, paused, waiting_for_confirmation, finished, error, stuck, deleting Optional security analyzer to evaluate action risks.
List of activated knowledge skills name
Actions blocked by PreToolUse hooks, keyed by action ID
Messages blocked by UserPromptSubmit hooks, keyed by message ID
Conversation statistics for tracking LLM metrics
Registry for handling secrets and sensitive data
Dictionary for agent-specific runtime state that persists across iterations. Agents can store feature-specific state using string keys. To trigger autosave, always reassign: state.agent_state = {**state.agent_state, key: value}. See https://docs.openhands.dev/sdk/guides/convo-persistence#how-state-persistence-works
User-defined title for the conversation
A snapshot of metrics at a point in time.
Does not include lists of individual costs, latencies, or token usages.