Skip to content

Agent Zero AI framework

Skip rate-limiter token counting without limits

Alessandro827f9176d27c
diff --git a/AGENTS.md b/AGENTS.md
index 9e075e34b..4e23dace2 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -21,6 +21,7 @@
- Native function calls use canonical call content for history and repeat comparison, even when accompanied by commentary. The canonical content passes through the normal history template/masking hook; provider output metadata remains intact.
- `initialize.py` owns framework initialization.
- `models.py` owns model-provider configuration and LiteLLM integration. `get_api_key_raw()` reads the stored key or key list for settings/editing; `get_api_key()` selects a runtime key and runs provider extension hooks.
+- `models.apply_rate_limiter` skips token counting and returns no limiter when all request, input, and output limits are unset; any configured limit retains normal accounting.
- `requirements.txt` includes HTTPX's SOCKS extra so framework clients support SOCKS proxy environment variables before plugin setup.
- Native agent-turn response callbacks may receive a display-only commentary preview as their full text; legacy callbacks, accumulated model output, usage and native dispatch retain the original text and result metadata.
- `run_ui.py` is the WebUI entry point.
diff --git a/models.py b/models.py
index 7fe095495..b43a02f7c 100644
--- a/models.py
+++ b/models.py
@@ -336,7 +336,10 @@ async def apply_rate_limiter(
Callable[[str, str, int, int], Awaitable[bool]] | None
) = None,
):
- if not model_config:
+ # skip token counting of the whole prompt when no limit is set
+ if not model_config or not (
+ model_config.limit_requests or model_config.limit_input or model_config.limit_output
+ ):
return
limiter = get_rate_limiter(
model_config.provider,
diff --git a/tests/test_model_rate_limiter.py b/tests/test_model_rate_limiter.py
new file mode 100644
index 000000000..c88792186
--- /dev/null
+++ b/tests/test_model_rate_limiter.py
@@ -0,0 +1,32 @@
+import sys
+from pathlib import Path
+
+import pytest
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+import models
+
+
[email protected]("limit", [None, "none", "requests", "input", "output"])
+async def test_rate_limiter_counts_tokens_only_when_limits_are_configured(monkeypatch, limit):
+ monkeypatch.setattr(models, "rate_limiters", {})
+ counted = []
+ monkeypatch.setattr(models, "approximate_tokens", lambda text: counted.append(text) or 3)
+ config = models.ModelConfig(
+ type=models.ModelType.CHAT, provider="openai", name="test",
+ ) if limit is not None else None
+ if limit in ("requests", "input", "output"):
+ setattr(config, f"limit_{limit}", 10)
+
+ limiter = await models.apply_rate_limiter(config, "prompt")
+
+ if limit in (None, "none"):
+ assert limiter is None
+ assert counted == []
+ assert models.rate_limiters == {}
+ else:
+ assert counted == ["prompt"]
+ assert limiter.limits[limit] == 10
+ assert await limiter.get_total("input") == 3
+ assert await limiter.get_total("requests") == 1