|
| 1 | +--- |
| 2 | +title: Agent Settings |
| 3 | +description: Configure, serialize, and recreate agents from structured settings. |
| 4 | +--- |
| 5 | + |
| 6 | +import RunExampleCode from "/sdk/shared-snippets/how-to-run-example.mdx"; |
| 7 | + |
| 8 | +> A ready-to-run example is available [here](#ready-to-run-example)! |
| 9 | +
|
| 10 | +`AgentSettings` gives you a structured, serializable way to define an agent's model, tools, and optional subsystems like the condenser. Use it when you want to store agent configuration in JSON, send it over an API, or rebuild agents from validated settings later. |
| 11 | + |
| 12 | +## Why Use AgentSettings |
| 13 | + |
| 14 | +- Keep agent configuration as data instead of wiring everything together imperatively. |
| 15 | +- Validate settings with Pydantic before creating an agent. |
| 16 | +- Serialize and deserialize settings for storage, transport, or UI-driven configuration. |
| 17 | +- Create different agent variants by changing only the settings payload. |
| 18 | + |
| 19 | +## Build Settings |
| 20 | + |
| 21 | +Create an `AgentSettings` object with the same ingredients you would normally pass to an `Agent`. |
| 22 | + |
| 23 | +```python icon="python" focus={8, 11, 12, 13} |
| 24 | +from pydantic import SecretStr |
| 25 | + |
| 26 | +from openhands.sdk import AgentSettings, LLM, Tool |
| 27 | +from openhands.sdk.settings import CondenserSettings |
| 28 | +from openhands.tools.file_editor import FileEditorTool |
| 29 | +from openhands.tools.terminal import TerminalTool |
| 30 | + |
| 31 | +settings = AgentSettings( |
| 32 | + llm=LLM( |
| 33 | + model="anthropic/claude-sonnet-4-5-20250929", |
| 34 | + api_key=SecretStr("your-api-key"), |
| 35 | + ), |
| 36 | + tools=[ |
| 37 | + Tool(name=TerminalTool.name), |
| 38 | + Tool(name=FileEditorTool.name), |
| 39 | + ], |
| 40 | + condenser=CondenserSettings(enabled=True, max_size=50), |
| 41 | +) |
| 42 | +``` |
| 43 | + |
| 44 | +## Serialize and Restore Settings |
| 45 | + |
| 46 | +Because `AgentSettings` is a Pydantic model, you can dump it to JSON-compatible data and restore it later. |
| 47 | + |
| 48 | +```python icon="python" focus={1, 2} |
| 49 | +payload = settings.model_dump(mode="json") |
| 50 | +restored = AgentSettings.model_validate(payload) |
| 51 | +``` |
| 52 | + |
| 53 | +This is useful when: |
| 54 | + |
| 55 | +- Saving agent configuration in a database |
| 56 | +- Sending settings through an API |
| 57 | +- Letting users edit agent configuration in a form-based UI |
| 58 | +- Rehydrating the same agent setup in another process |
| 59 | + |
| 60 | +## Create an Agent from Settings |
| 61 | + |
| 62 | +Once validated, create a working agent directly from the settings object. |
| 63 | + |
| 64 | +```python icon="python" focus={1} |
| 65 | +agent = settings.create_agent() |
| 66 | +``` |
| 67 | + |
| 68 | +You can then pass that agent into a `Conversation`, or derive another agent by changing the settings payload. For example, the full example below also shows how removing `FileEditorTool` and disabling the condenser produces a different agent configuration without rewriting the rest of the setup. |
| 69 | + |
| 70 | +## Ready-to-run Example |
| 71 | + |
| 72 | +<Note> |
| 73 | +This example is available on GitHub: [examples/01_standalone_sdk/46_agent_settings.py](https://github.com/OpenHands/software-agent-sdk/blob/main/examples/01_standalone_sdk/46_agent_settings.py) |
| 74 | +</Note> |
| 75 | + |
| 76 | +```python icon="python" expandable examples/01_standalone_sdk/46_agent_settings.py |
| 77 | +"""Create, serialize, and deserialize AgentSettings, then build a working agent. |
| 78 | +
|
| 79 | +Demonstrates: |
| 80 | +1. Configuring an agent entirely through AgentSettings (LLM, tools, condenser). |
| 81 | +2. Serializing settings to JSON and restoring them. |
| 82 | +3. Building an Agent from settings via ``create_agent()``. |
| 83 | +4. Running a short conversation to prove the settings take effect. |
| 84 | +5. Changing the tool list and showing the agent's capabilities change. |
| 85 | +""" |
| 86 | + |
| 87 | +import json |
| 88 | +import os |
| 89 | + |
| 90 | +from pydantic import SecretStr |
| 91 | + |
| 92 | +from openhands.sdk import LLM, AgentSettings, Conversation, Tool |
| 93 | +from openhands.sdk.settings import CondenserSettings |
| 94 | +from openhands.tools.file_editor import FileEditorTool |
| 95 | +from openhands.tools.terminal import TerminalTool |
| 96 | + |
| 97 | + |
| 98 | +# ── 1. Build settings ──────────────────────────────────────────────────── |
| 99 | +api_key = os.getenv("LLM_API_KEY") |
| 100 | +assert api_key is not None, "LLM_API_KEY environment variable is not set." |
| 101 | + |
| 102 | +settings = AgentSettings( |
| 103 | + llm=LLM( |
| 104 | + model=os.getenv("LLM_MODEL", "anthropic/claude-sonnet-4-5-20250929"), |
| 105 | + api_key=SecretStr(api_key), |
| 106 | + base_url=os.getenv("LLM_BASE_URL"), |
| 107 | + ), |
| 108 | + tools=[ |
| 109 | + Tool(name=TerminalTool.name), |
| 110 | + Tool(name=FileEditorTool.name), |
| 111 | + ], |
| 112 | + condenser=CondenserSettings(enabled=True, max_size=50), |
| 113 | +) |
| 114 | + |
| 115 | +# ── 2. Serialize → JSON → deserialize ──────────────────────────────────── |
| 116 | +payload = settings.model_dump(mode="json") |
| 117 | +print("Serialized settings (JSON):") |
| 118 | +print(json.dumps(payload, indent=2, default=str)[:800], "…") |
| 119 | +print() |
| 120 | + |
| 121 | +restored = AgentSettings.model_validate(payload) |
| 122 | +assert restored.condenser.enabled is True |
| 123 | +assert restored.condenser.max_size == 50 |
| 124 | +assert len(restored.tools) == 2 |
| 125 | +print("✓ Roundtrip deserialization successful — all fields preserved") |
| 126 | +print() |
| 127 | + |
| 128 | +# ── 3. Create agent from settings and run a task ───────────────────────── |
| 129 | +agent = settings.create_agent() |
| 130 | +print(f"Agent created: llm.model={agent.llm.model}") |
| 131 | +print(f" tools={[t.name for t in agent.tools]}") |
| 132 | +print(f" condenser={type(agent.condenser).__name__}") |
| 133 | +print() |
| 134 | + |
| 135 | +cwd = os.getcwd() |
| 136 | +conversation = Conversation(agent=agent, workspace=cwd) |
| 137 | +conversation.send_message( |
| 138 | + "Create a file called hello_settings.txt containing " |
| 139 | + "'Agent settings work!' then confirm the file exists with ls." |
| 140 | +) |
| 141 | +conversation.run() |
| 142 | + |
| 143 | +# Verify the agent actually wrote the file |
| 144 | +assert os.path.exists(os.path.join(cwd, "hello_settings.txt")), ( |
| 145 | + "Agent should have created hello_settings.txt" |
| 146 | +) |
| 147 | +print("✓ Agent created hello_settings.txt — settings drove real behavior") |
| 148 | +print() |
| 149 | + |
| 150 | +# ── 4. Different settings → different behavior ─────────────────────────── |
| 151 | +# Now create settings with ONLY the terminal tool and condenser disabled. |
| 152 | +terminal_only_settings = AgentSettings( |
| 153 | + llm=settings.llm, |
| 154 | + tools=[Tool(name=TerminalTool.name)], |
| 155 | + condenser=CondenserSettings(enabled=False), |
| 156 | +) |
| 157 | + |
| 158 | +terminal_agent = terminal_only_settings.create_agent() |
| 159 | +print(f"Terminal-only agent tools: {[t.name for t in terminal_agent.tools]}") |
| 160 | +assert len(terminal_agent.tools) == 1 |
| 161 | +assert terminal_agent.condenser is None # condenser disabled in these settings |
| 162 | +print("✓ Different settings produce different agent configuration") |
| 163 | +print() |
| 164 | + |
| 165 | +# ── Cleanup ────────────────────────────────────────────────────────────── |
| 166 | +os.remove(os.path.join(cwd, "hello_settings.txt")) |
| 167 | + |
| 168 | +# Report cost |
| 169 | +cost = conversation.conversation_stats.get_combined_metrics().accumulated_cost |
| 170 | +print(f"\nEXAMPLE_COST: {cost}") |
| 171 | +``` |
| 172 | + |
| 173 | +<RunExampleCode path_to_script="examples/01_standalone_sdk/46_agent_settings.py"/> |
| 174 | + |
| 175 | +## Next Steps |
| 176 | + |
| 177 | +- **[Getting Started](/sdk/getting-started)** - Start from a minimal agent and conversation setup |
| 178 | +- **[Context Condenser](/sdk/guides/context-condenser)** - Control conversation compaction behavior |
| 179 | +- **[Agent Delegation](/sdk/guides/agent-delegation)** - Compose specialized agents for larger tasks |
0 commit comments