Check token usage accumulation with tool/function-call
(fn, stream)
| 97 | @pytest.mark.parametrize("fn", [True, False]) |
| 98 | @pytest.mark.parametrize("stream", [True, False]) |
| 99 | def 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 |
nothing calls this directly
no test coverage detected
searching dependent graphs…