Skip to content

SWE-agent takes a GitHub issue and tries to automatically fix it, using your LM of choice. It can also be employed for offensive cybersecurity or competitive coding challenges. [NeurIPS 2024]

Feat(model): Limit of calls

Kilian Lieret4ccd076d21e8
diff --git a/sweagent/agent/models.py b/sweagent/agent/models.py
index 8ff7cdcb..98061860 100644
--- a/sweagent/agent/models.py
+++ b/sweagent/agent/models.py
@@ -30,6 +30,7 @@ from sweagent.exceptions import (
ContextWindowExceededError,
CostLimitExceededError,
FunctionCallingFormatError,
+ InstanceCallLimitExceededError,
InstanceCostLimitExceededError,
TotalCostLimitExceededError,
)
@@ -72,6 +73,7 @@ class GenericAPIModelConfig(PydanticBaseModel):
description="Cost limit for every instance (task).",
)
total_cost_limit: float = Field(default=0.0, description="Total cost limit.")
+ per_instance_call_limit: int = Field(default=0, description="Per instance call limit.")
temperature: float = 0.0
"""Sampling temperature"""
top_p: float | None = 1.0
@@ -576,18 +578,23 @@ class LiteLLMModel(AbstractModel):
)
# Check whether total cost or instance cost limits have been exceeded
- if 0 < self.config.total_cost_limit <= GLOBAL_STATS.total_cost:
+ if 0 < self.config.total_cost_limit < GLOBAL_STATS.total_cost:
self.logger.warning(f"Cost {GLOBAL_STATS.total_cost:.2f} exceeds limit {self.config.total_cost_limit:.2f}")
msg = "Total cost limit exceeded"
raise TotalCostLimitExceededError(msg)
- if 0 < self.config.per_instance_cost_limit <= self.stats.instance_cost:
+ if 0 < self.config.per_instance_cost_limit < self.stats.instance_cost:
self.logger.warning(
f"Cost {self.stats.instance_cost:.2f} exceeds limit {self.config.per_instance_cost_limit:.2f}"
)
msg = "Instance cost limit exceeded"
raise InstanceCostLimitExceededError(msg)
+ if 0 < self.config.per_instance_call_limit < self.stats.api_calls:
+ self.logger.warning(f"API calls {self.stats.api_calls} exceeds limit {self.config.per_instance_call_limit}")
+ msg = "Per instance call limit exceeded"
+ raise InstanceCallLimitExceededError(msg)
+
def _sleep(self) -> None:
elapsed_time = time.time() - GLOBAL_STATS.last_query_timestamp
if elapsed_time < self.config.delay:
diff --git a/sweagent/exceptions.py b/sweagent/exceptions.py
index a2b70ce6..782e79ca 100644
--- a/sweagent/exceptions.py
+++ b/sweagent/exceptions.py
@@ -40,5 +40,9 @@ class TotalCostLimitExceededError(CostLimitExceededError):
"""Raised when we exceed the total cost limit"""
+class InstanceCallLimitExceededError(CostLimitExceededError):
+ """Raised when we exceed the per instance call limit"""
+
+
class ContentPolicyViolationError(Exception):
"""Raised when the model response violates a content policy"""