Skip to main content
Glama
wn01011

llm-token-tracker

get_usage

Retrieve token usage summaries for OpenAI and Claude APIs to monitor consumption and manage costs.

Instructions

Get usage summary

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
user_idNoUser ID (optional)

Implementation Reference

  • The primary handler function for the 'get_usage' tool. Processes arguments to retrieve and format usage summary for a specific user_id or all users, returning structured content with token counts and costs.
    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' }]
        };
      }
    }
  • Tool registration in ListToolsRequestSchema handler, defining name, description, and input schema for 'get_usage'.
    {
      name: 'get_usage',
      description: 'Get usage summary',
      inputSchema: {
        type: 'object',
        properties: {
          user_id: {
            type: 'string',
            description: 'User ID (optional)'
          }
        }
      }
    },
  • Dispatch routing in CallToolRequestSchema handler that maps 'get_usage' tool calls to the getUsage method.
    case 'get_usage':
      return this.getUsage(request.params.arguments);
  • Input schema definition for the get_usage tool, specifying optional user_id parameter.
    inputSchema: {
      type: 'object',
      properties: {
        user_id: {
          type: 'string',
          description: 'User ID (optional)'
        }
      }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/wn01011/llm-token-tracker'

If you have feedback or need assistance with the MCP directory API, please join our Discord server