Skip to main content
Glama
wn01011

llm-token-tracker

track_usage

Monitor AI API token consumption by recording provider, model, and input/output token counts to track usage patterns and costs.

Instructions

Track token usage for an AI API call

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
providerYesAI provider
modelYesModel name
input_tokensYesInput tokens used
output_tokensYesOutput tokens used
user_idNoOptional user ID

Implementation Reference

  • The main handler function that executes the 'track_usage' tool logic. It tracks input/output tokens for a given provider/model/user using the TokenTracker, calculates session cost, and returns a formatted text response.
    private trackUsage(args: any) {
      const { provider, model, input_tokens, output_tokens, user_id = 'current-session' } = args;
      
      const trackingId = this.tracker.startTracking(user_id);
      this.tracker.endTracking(trackingId, {
        provider: provider as 'openai' | 'anthropic' | 'gemini',
        model,
        inputTokens: input_tokens,
        outputTokens: output_tokens,
        totalTokens: input_tokens + output_tokens
      });
    
      const usage = this.tracker.getUserUsage(user_id);
      const totalTokens = input_tokens + output_tokens;
      const cost = usage?.totalCost || 0;
      
      return {
        content: [
          {
            type: 'text',
            text: `✅ Tracked ${totalTokens.toLocaleString()} tokens for ${model}\n` +
                  `💰 Session Cost: ${formatCost(cost)}\n` +
                  `📊 Total: ${usage?.totalTokens.toLocaleString() || 0} tokens`
          }
        ]
      };
    }
  • Input schema definition for the 'track_usage' tool, specifying required properties like provider, model, input_tokens, output_tokens.
    inputSchema: {
      type: 'object',
      properties: {
        provider: {
          type: 'string',
          enum: ['openai', 'anthropic', 'gemini'],
          description: 'AI provider'
        },
        model: {
          type: 'string',
          description: 'Model name'
        },
        input_tokens: {
          type: 'number',
          description: 'Input tokens used'
        },
        output_tokens: {
          type: 'number',
          description: 'Output tokens used'
        },
        user_id: {
          type: 'string',
          description: 'Optional user ID'
        }
      },
      required: ['provider', 'model', 'input_tokens', 'output_tokens']
    }
  • Registers the 'track_usage' tool in the MCP server's listTools response, including name, description, and input schema.
    {
      name: 'track_usage',
      description: 'Track token usage for an AI API call',
      inputSchema: {
        type: 'object',
        properties: {
          provider: {
            type: 'string',
            enum: ['openai', 'anthropic', 'gemini'],
            description: 'AI provider'
          },
          model: {
            type: 'string',
            description: 'Model name'
          },
          input_tokens: {
            type: 'number',
            description: 'Input tokens used'
          },
          output_tokens: {
            type: 'number',
            description: 'Output tokens used'
          },
          user_id: {
            type: 'string',
            description: 'Optional user ID'
          }
        },
        required: ['provider', 'model', 'input_tokens', 'output_tokens']
      }
    },
  • Dispatches 'track_usage' tool calls to the trackUsage handler method in the CallToolRequestHandler switch statement.
    case 'track_usage':
      return this.trackUsage(request.params.arguments);
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries full burden. 'Track' implies a write or logging operation, but the description doesn't disclose whether this creates records, updates a database, requires authentication, has side effects, or returns any confirmation. For a mutation-like tool with zero annotation coverage, this is a significant gap in behavioral context.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that directly states the tool's purpose without any wasted words. It's appropriately sized and front-loaded, making it easy to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity (a tool with 5 parameters, no annotations, and no output schema), the description is incomplete. It doesn't explain what 'track' entails operationally, what happens after invocation, or how this differs from sibling tools. For a tool that likely modifies state, more context is needed to guide proper usage.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema fully documents all 5 parameters. The description adds no additional meaning beyond implying these parameters are used for tracking token usage, which is already evident from the schema. This meets the baseline of 3 when the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('track') and resource ('token usage for an AI API call'), making the purpose immediately understandable. However, it doesn't differentiate this tool from sibling tools like 'get_usage' or 'clear_usage', which likely handle related aspects of usage data.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. With sibling tools like 'get_usage' (likely for retrieval) and 'clear_usage' (likely for deletion), the agent has no indication whether this is for logging, monitoring, or another purpose, or what prerequisites might exist.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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