Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(config): use the longest matching model_context_limits prefix (#4288)

Rudy Mizrahi Celekli603d873992b4
diff --git a/headroom/config.py b/headroom/config.py
index b42617575..5688ff04f 100644
--- a/headroom/config.py
+++ b/headroom/config.py
@@ -982,10 +982,12 @@ class HeadroomConfig:
"""
if model in self.model_context_limits:
return self.model_context_limits[model]
- # Try prefix matching for versioned model names
- for known_model, limit in self.model_context_limits.items():
- if model.startswith(known_model):
- return limit
+ # Try prefix matching for versioned model names. The longest prefix
+ # wins, so "gpt-4o-2024-08-06" uses a "gpt-4o" override rather than a
+ # "gpt-4" one that happens to come first in the dict.
+ matches = [known for known in self.model_context_limits if model.startswith(known)]
+ if matches:
+ return self.model_context_limits[max(matches, key=len)]
return None
diff --git a/tests/test_config.py b/tests/test_config.py
index 5d413681d..68f85d48a 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -173,6 +173,22 @@ class TestHeadroomConfig:
assert config.get_context_limit("claude-3-opus") == 200000
assert config.get_context_limit("claude-3-sonnet") == 200000
+ def test_get_context_limit_prefers_longest_prefix(self):
+ """A more specific override wins over a shorter one, whatever the dict order."""
+ config = HeadroomConfig(
+ model_context_limits={
+ "gpt-4": 8192,
+ "gpt-4o": 128000,
+ "claude-3": 100000,
+ "claude-3-5-sonnet": 200000,
+ }
+ )
+ assert config.get_context_limit("gpt-4o-2024-08-06") == 128000
+ assert config.get_context_limit("claude-3-5-sonnet-20241022") == 200000
+ # The shorter prefix still applies when it is the only match.
+ assert config.get_context_limit("gpt-4-0613") == 8192
+ assert config.get_context_limit("claude-3-opus") == 100000
+
def test_get_context_limit_not_found(self):
"""get_context_limit returns None for unknown model."""
config = HeadroomConfig(model_context_limits={"gpt-4": 128000})