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]

Enh: custom model registry (#1200)

carlos3eebd199b8ea
diff --git a/docs/config/models.md b/docs/config/models.md
index c2114b84..a3dd986a 100644
--- a/docs/config/models.md
+++ b/docs/config/models.md
@@ -16,6 +16,10 @@
See [the model section](../installation/keys.md) in the installation guide.
Remember to unset spending limits and configure the action parser if you cannot support function calling.
+For "cost" tracking with local models, you can optionally provide a custom `litellm_model_registry` file in your configuration.
+This allows you to define custom pricing information for your local models instead of disabling cost limits entirely.
+See the [local models section](../installation/keys.md#custom-model-registry-for-cost-tracking) for detailed instructions.
+
### Anthropic Claude
Prompt caching makes SWE-agent several times more affordable. While this is done automatically for models like `gpt-4o`,
diff --git a/docs/installation/keys.md b/docs/installation/keys.md
index 82923e84..062c3876 100644
--- a/docs/installation/keys.md
+++ b/docs/installation/keys.md
@@ -36,6 +36,10 @@ See the following links for tutorials on obtaining [Anthropic](https://docs.anth
We support all models supported by [litellm](https://github.com/BerriAI/litellm), see their list [here](https://docs.litellm.ai/docs/providers).
+!!! tip "Custom model registries"
+
+ If you're using a model that's not in the default litellm model registry (e.g., custom local models or new models), you can provide a custom model registry file using the `litellm_model_registry` configuration option. This allows proper cost tracking for any model. See the [custom model registry section](#custom-model-registry-for-cost-tracking) below for details.
+
Here are a few options for `--agent.model.name`:
| Model | API key | Comment |
@@ -103,8 +107,55 @@ and set `agent.model.api_key` to the key you've configured for your proxy (or a
!!! warning "Cost/token limits"
If you do not disable the default cost limits, you will see an error because the cost calculator will not be able to find the model in the `litellm` model cost dictionary.
- Please make sure to the set the `per_instance_cost_limit` to 0 and use the `per_instance_call_limit` instead to limit the runtime per issue.
- Please also make sure to set `max_input_tokens` to a non-`None` value to avoid a lot of warnings.
+
+ You have two options:
+
+ 1. **Disable cost tracking** (recommended for most users): Set `per_instance_cost_limit` to 0 and use the `per_instance_call_limit` instead to limit the runtime per issue.
+ 2. **Use a custom model registry**: If you want to track costs for your local model, you can provide a custom `litellm_model_registry` file with cost information for your model (see below).
+
+ Please also make sure to set `max_input_tokens` to a non-`None` value to avoid other warnings.
+
+### Custom model registry for cost tracking
+
+If you want to track costs for models not in the default litellm registry, you can provide a custom model registry file. This is particularly useful for:
+
+- New models not yet supported by litellm's default registry
+- Overriding default / old cost values in litellm
+- Local models that you want to track "costs" for, to compare to other results
+
+This file will override entries in the [litellm community model cost file](https://github.com/BerriAI/litellm/blob/main/model_prices_and_context_window.json).
+
+Create a JSON file with your model's cost information following the litellm model registry format:
+
+```json title="my_model_registry.json"
+{
+ "ollama/llama2": {
+ "max_tokens": 8192,
+ "input_cost_per_token": 0.00002,
+ "output_cost_per_token": 0.00006,
+ "litellm_provider": "ollama",
+ "mode": "chat"
+ },
+ "my-custom-provider/my-new-model": {
+ "max_tokens": 8192,
+ "max_input_tokens": 8192,
+ "max_output_tokens": 8192,
+ "input_cost_per_token": 0.000001,
+ "output_cost_per_token": 0.000002,
+ "litellm_provider": "openai",
+ "mode": "chat"
+ }
+}
+```
+
+Then specify this registry in your config:
+
+```yaml title="config/your_config.yaml"
+agent:
+ model:
+ litellm_model_registry: "my_model_registry.json" # Path to your custom registry
+ ...
+```
!!! warning "Parsing functions"
diff --git a/sweagent/agent/models.py b/sweagent/agent/models.py
index 66659cea..b8cb07c4 100644
--- a/sweagent/agent/models.py
+++ b/sweagent/agent/models.py
@@ -138,6 +138,11 @@ class GenericAPIModelConfig(PydanticBaseModel):
Set this to 0 to disable this check.
"""
+ litellm_model_registry: str | None = None
+ """If set, this will override the default model registry for litellm.
+ Use this for local models or models not (yet) in the default litellm model registry for tracking costs.
+ """
+
# pydantic
model_config = ConfigDict(extra="forbid")
@@ -574,7 +579,10 @@ class LiteLLMModel(AbstractModel):
"See https://swe-agent.com/latest/faq/ for more information."
)
self.logger.warning(msg)
-
+ if self.config.litellm_model_registry is not None:
+ with open(self.config.litellm_model_registry) as f:
+ model_costs = json.load(f)
+ litellm.register_model(model_costs)
if self.config.max_input_tokens is not None:
self.model_max_input_tokens = self.config.max_input_tokens
else: