MCPcopy
hub / github.com/langroid/langroid / test_token_usage_tool

Function test_token_usage_tool

tests/main/test_token_usage.py:99–124  ·  view source on GitHub ↗

Check token usage accumulation with tool/function-call

(fn, stream)

Source from the content-addressed store, hash-verified

97@pytest.mark.parametrize("fn", [True, False])
98@pytest.mark.parametrize("stream", [True, False])
99def test_token_usage_tool(fn, stream):
100 """Check token usage accumulation with tool/function-call"""
101 set_global(Settings(cache=False, stream=stream))
102 cfg = _TestChatAgentConfig(
103 llm=config,
104 use_functions_api=fn,
105 use_tools=not fn,
106 system_message="Use the `capital` tool to tell me the capital of a country",
107 )
108 agent = ChatAgent(cfg)
109 agent.llm.reset_usage_cost()
110 agent.enable_message(CapitalTool, use=True, handle=True)
111
112 question = "What is the capital of China?"
113 response1 = agent.llm_response(question)
114 result = agent.agent_response(response1)
115 agent.llm_response(result)
116 response3 = agent.llm_response(question)
117
118 assert (
119 response3.metadata.usage.prompt_tokens
120 >= response1.metadata.usage.prompt_tokens
121 + response1.metadata.usage.completion_tokens
122 + agent.num_tokens(question)
123 + agent.num_tokens(result.content)
124 )
125
126
127@pytest.mark.asyncio

Callers

nothing calls this directly

Calls 9

enable_messageMethod · 0.95
llm_responseMethod · 0.95
set_globalFunction · 0.90
SettingsClass · 0.90
ChatAgentClass · 0.90
reset_usage_costMethod · 0.80
agent_responseMethod · 0.45
num_tokensMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…