-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_token_fix.py
More file actions
74 lines (60 loc) · 2.24 KB
/
test_token_fix.py
File metadata and controls
74 lines (60 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""Test script to verify token counting fix."""
import asyncio
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from mcp_use import MCPAgent, MCPClient
from mcp_use.token_counting import TokenUsageSnapshot
# Load environment variables
load_dotenv()
def token_usage_callback(snapshot: TokenUsageSnapshot):
"""Test callback for token usage."""
print(f"\n🔢 Token Usage Test:")
print(f" Operation: {snapshot.operation}")
print(f" Input tokens: {snapshot.usage.input_tokens}")
print(f" Output tokens: {snapshot.usage.output_tokens}")
print(f" Total tokens: {snapshot.usage.total_tokens}")
print(f" Cumulative: {snapshot.cumulative_usage.total_tokens}")
async def test_token_counting():
"""Test token counting functionality."""
# Simple test config
config = {
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/tmp"]
}
}
}
client = MCPClient.from_dict(config)
try:
# Create agent with token counting
llm = ChatOpenAI(model="gpt-4o-mini", api_key=os.getenv("OPENAI_API_KEY"))
agent = MCPAgent(
llm=llm,
client=client,
enable_token_counting=True,
token_tracking_callbacks=[token_usage_callback],
max_steps=3
)
print("🚀 Testing token counting...")
# Simple test query
result = await agent.run("What is 2+2?")
print(f"\n✅ Result: {result}")
# Check token stats
stats = agent.get_token_usage()
if stats:
print(f"\n📊 Final Stats:")
print(f" Total operations: {stats.get('total_operations', 0)}")
print(f" Total tokens: {stats.get('cumulative_usage', {}).get('total_tokens', 0)}")
else:
print("\n❌ No token stats available")
except Exception as e:
print(f"❌ Error: {e}")
import traceback
traceback.print_exc()
finally:
await client.close_all_sessions()
if __name__ == "__main__":
asyncio.run(test_token_counting())