test(model_metadata): cache tools schema token estimate

Adds a regression test that repeated request-token estimates do not re-serialize the same tool schema list.
This commit is contained in:
infinitycrew39 2026-07-08 23:53:58 +07:00 committed by kshitij
parent 32a0f9e17a
commit f4d5cfd0fd

View file

@ -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
# =========================================================================