diff --git a/tests/agent/test_model_metadata.py b/tests/agent/test_model_metadata.py index d450580cd3c..47a95575555 100644 --- a/tests/agent/test_model_metadata.py +++ b/tests/agent/test_model_metadata.py @@ -30,6 +30,7 @@ from agent.model_metadata import ( save_context_length, fetch_model_metadata, _MODEL_CACHE_TTL, + estimate_request_tokens_rough, ) @@ -120,6 +121,28 @@ class TestEstimateMessagesTokensRough: assert result < 5000 +class TestEstimateRequestTokensRough: + def test_caches_tools_estimate(self): + messages = [{"role": "user", "content": "hello"}] + tools = [ + { + "type": "function", + "function": { + "name": "terminal", + "description": "Run a command", + "parameters": {"type": "object", "properties": {"command": {"type": "string"}}}, + }, + } + ] + + # json.dumps is used for params sizing; ensure the tools estimate is cached + # so repeated calls don't keep re-serializing the same schema list. + with patch("agent.model_metadata.json.dumps", wraps=__import__("json").dumps) as dumps: + estimate_request_tokens_rough(messages, system_prompt="x" * 8, tools=tools) + estimate_request_tokens_rough(messages, system_prompt="x" * 8, tools=tools) + assert dumps.call_count == 1 + + # ========================================================================= # Default context lengths # =========================================================================