Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions openhands-sdk/openhands/sdk/settings/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,22 @@ class ConversationSettings(BaseModel):
).model_dump()
},
)
max_budget_per_run: float | None = Field(
default=None,
gt=0,
description=(
"Default maximum accumulated LLM cost, in USD, for new "
"conversations. Each new conversation stops once its cost reaches "
"it. Changing it does not affect existing conversations. Leave "
"empty for no limit."
),
json_schema_extra={
SETTINGS_METADATA_KEY: SettingsFieldMetadata(
label="Default budget per conversation (USD)",
prominence=SettingProminence.MAJOR,
).model_dump()
},
)
confirmation_mode: bool = Field(
default=False,
description="Require user confirmation before executing risky actions.",
Expand Down Expand Up @@ -1475,6 +1491,7 @@ def _start_request_kwargs(self, **kwargs: Any) -> dict[str, Any]:
payload.setdefault("confirmation_policy", self._build_confirmation_policy())
payload.setdefault("security_analyzer", self._build_security_analyzer())
payload.setdefault("max_iterations", self.max_iterations)
payload.setdefault("max_budget_per_run", self.max_budget_per_run)
return payload

def create_request(
Expand Down
1 change: 1 addition & 0 deletions tests/agent_server/test_settings_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ def test_get_settings_migrates_legacy_openhands_settings_and_resaves_current(
assert body["conversation_settings"] == {
"schema_version": CONVERSATION_SETTINGS_SCHEMA_VERSION,
"max_iterations": 42,
"max_budget_per_run": None,
"confirmation_mode": True,
"security_analyzer": "llm",
"default_runtime": "local",
Expand Down
47 changes: 46 additions & 1 deletion tests/sdk/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,18 @@ def test_conversation_settings_export_schema_groups_sections() -> None:

sections = {s.key: s for s in schema.sections}
general_fields = {f.key: f for f in sections["general"].fields}
assert set(general_fields) == {"max_iterations"}
assert set(general_fields) == {"max_iterations", "max_budget_per_run"}
assert general_fields["max_iterations"].default == 500
assert general_fields["max_iterations"].prominence is SettingProminence.MAJOR
assert general_fields["max_budget_per_run"].default is None
assert general_fields["max_budget_per_run"].prominence is SettingProminence.MAJOR
assert (
general_fields["max_budget_per_run"].label
== "Default budget per conversation (USD)"
)
assert "new conversations" in (
general_fields["max_budget_per_run"].description or ""
)

verification_fields = {f.key: f for f in sections["verification"].fields}
assert set(verification_fields) == {
Expand Down Expand Up @@ -339,6 +348,7 @@ def test_conversation_settings_validates_observability_metadata() -> None:
def test_conversation_settings_model_dump_roundtrip() -> None:
settings = ConversationSettings(
max_iterations=42,
max_budget_per_run=2.5,
confirmation_mode=True,
security_analyzer="none",
)
Expand Down Expand Up @@ -383,6 +393,41 @@ def test_conversation_settings_create_request() -> None:
assert overridden_request.security_analyzer is None


def test_conversation_settings_create_request_forwards_budget() -> None:
workspace = LocalWorkspace(working_dir="/tmp")
agent = OpenHandsAgentSettings(llm=LLM(model="test-model")).create_agent()

unset = ConversationSettings().create_request(
StartConversationRequest, agent=agent, workspace=workspace
)
budgeted = ConversationSettings(max_budget_per_run=2.5).create_request(
StartConversationRequest, agent=agent, workspace=workspace
)
overridden = ConversationSettings(max_budget_per_run=2.5).create_request(
StartConversationRequest,
agent=agent,
workspace=workspace,
max_budget_per_run=None,
)
raised = ConversationSettings(max_budget_per_run=2.5).create_request(
StartConversationRequest,
agent=agent,
workspace=workspace,
max_budget_per_run=5.0,
)

assert unset.max_budget_per_run is None
assert budgeted.max_budget_per_run == 2.5
assert overridden.max_budget_per_run is None
Comment thread
neubig marked this conversation as resolved.
Resolved
assert raised.max_budget_per_run == 5.0


@pytest.mark.parametrize("budget", [0, -1])
def test_conversation_settings_rejects_non_positive_budget(budget: float) -> None:
with pytest.raises(ValidationError):
ConversationSettings(max_budget_per_run=budget)


def test_conversation_settings_create_request_with_acp_agent() -> None:
settings = ConversationSettings(
max_iterations=77,
Expand Down
Loading