get_claude_usage
Fetch Claude.ai usage data to monitor daily token consumption and track session limits through automated dashboard retrieval.
Instructions
Fetch Claude usage data from the dashboard. Returns daily token usage information.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- server.py:226-246 (handler)The handler implementation for the `get_claude_usage` tool, which fetches the browser context, checks for authentication, and triggers the scraping of the usage data.
if name == "get_claude_usage": context = await get_browser_context() # Check if authenticated if not await check_authenticated(context): return [TextContent( type="text", text=json.dumps({ "success": False, "error": "Not authenticated. Please run claude_login first." }, indent=2) )] result = await scrape_usage_data(context) await save_auth_state() return [TextContent( type="text", text=json.dumps(result, indent=2) )] - server.py:192-200 (registration)The definition of the `get_claude_usage` tool, registered within the `list_tools` function.
Tool( name="get_claude_usage", description="Fetch Claude usage data from the dashboard. Returns daily token usage information.", inputSchema={ "type": "object", "properties": {}, "required": [] } ), - server.py:82-127 (helper)The helper function `scrape_usage_data` that performs the actual web scraping on the Claude settings page.
async def scrape_usage_data(context: BrowserContext) -> dict[str, Any]: """Scrape usage data from the Claude dashboard.""" page = await context.new_page() try: await page.goto(USAGE_URL, wait_until="domcontentloaded", timeout=60000) # Wait for the page to fully load await page.wait_for_timeout(2000) # Extract usage data from the page # The structure may vary - we'll try to get what's available usage_data = await page.evaluate(""" () => { const data = { raw_text: document.body.innerText, title: document.title, url: window.location.href }; // Try to find usage-related elements const usageElements = document.querySelectorAll('[class*="usage"], [class*="Usage"]'); data.usage_elements = Array.from(usageElements).map(el => el.innerText); // Try to find any tables or data displays const tables = document.querySelectorAll('table'); data.tables = Array.from(tables).map(table => table.innerText); // Try to find any chart or graph data const charts = document.querySelectorAll('[class*="chart"], [class*="Chart"], svg'); data.chart_count = charts.length; return data; } """) return { "success": True, "data": usage_data } except Exception as e: return { "success": False, "error": str(e) } finally: await page.close()