get_usage
Track and monitor token usage across OpenAI and Claude APIs to manage costs and optimize API consumption.
Instructions
Get usage summary
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| user_id | No | User ID (optional) |
Implementation Reference
- src/mcp-server.ts:270-301 (handler)The main handler function for the 'get_usage' tool. Retrieves and formats usage summary for a specific user_id or all users if not provided.private getUsage(args: any) { const { user_id } = args; if (user_id) { const usage = this.tracker.getUserUsage(user_id); if (!usage) { return { content: [{ type: 'text', text: `No usage data for ${user_id}` }] }; } let summary = `π Usage Summary for ${user_id}\n`; summary += `Total: ${usage.totalTokens} tokens (${formatCost(usage.totalCost)})\n\n`; Object.entries(usage.usageByModel).forEach(([model, data]) => { summary += `${model}: ${data.tokens} tokens (${formatCost(data.cost)})\n`; }); return { content: [{ type: 'text', text: summary }] }; } else { const allUsage = this.tracker.getAllUsersUsage(); let summary = 'π All Users:\n'; allUsage.forEach(user => { summary += `${user.userId}: ${user.totalTokens} tokens (${formatCost(user.totalCost)})\n`; }); return { content: [{ type: 'text', text: summary || 'No usage data' }] }; } }
- src/mcp-server.ts:100-112 (schema)Defines the tool name, description, and input schema (optional user_id) for 'get_usage' in the ListTools response.{ name: 'get_usage', description: 'Get usage summary', inputSchema: { type: 'object', properties: { user_id: { type: 'string', description: 'User ID (optional)' } } } },
- src/mcp-server.ts:164-165 (registration)Dispatches to the getUsage handler method in the CallToolRequestHandler switch statement.case 'get_usage': return this.getUsage(request.params.arguments);