Skip to main content
Glama
wn01011

llm-token-tracker

get_current_session

Retrieve current LLM session usage details including remaining tokens, costs, and input/output counts to monitor API consumption.

Instructions

Get current session usage with intuitive format (remaining, used, input/output tokens, cost)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
user_idNoUser ID (defaults to current-session)current-session
total_budgetNoTotal token budget (optional)

Implementation Reference

  • Main handler function for 'get_current_session' tool. Retrieves user usage from TokenTracker, computes input/output tokens by summing history, calculates remaining budget and progress bar, formats a comprehensive text response with emojis, stats, and optional model breakdown.
    private getCurrentSession(args: any) {
      const { user_id = 'current-session', total_budget = 190000 } = args;
      
      const usage = this.tracker.getUserUsage(user_id);
      
      if (!usage) {
        return {
          content: [{
            type: 'text',
            text: `πŸ’° Current Session\n` +
                  `━━━━━━━━━━━━━━━━━━━━━━\n` +
                  `πŸ“Š Used: 0 tokens\n` +
                  `✨ Remaining: ${total_budget.toLocaleString()} tokens\n` +
                  `πŸ“₯ Input: 0 tokens\n` +
                  `πŸ“€ Output: 0 tokens\n` +
                  `πŸ’΅ Cost: $0.0000\n` +
                  `━━━━━━━━━━━━━━━━━━━━━━\n` +
                  `No usage recorded yet.`
          }]
        };
      }
    
      // Calculate input/output from model breakdown
      let totalInput = 0;
      let totalOutput = 0;
      
      const history = this.tracker.getUserHistory(user_id);
      history.forEach(record => {
        totalInput += record.inputTokens || 0;
        totalOutput += record.outputTokens || 0;
      });
    
      const usedTokens = usage.totalTokens;
      const remaining = Math.max(0, total_budget - usedTokens);
      const percentUsed = ((usedTokens / total_budget) * 100).toFixed(1);
      
      // Progress bar
      const barLength = 20;
      const filledLength = Math.round((usedTokens / total_budget) * barLength);
      const progressBar = 'β–ˆ'.repeat(filledLength) + 'β–‘'.repeat(barLength - filledLength);
      
      let result = `πŸ’° Current Session\n`;
      result += `━━━━━━━━━━━━━━━━━━━━━━\n`;
      result += `πŸ“Š Used: ${usedTokens.toLocaleString()} tokens (${percentUsed}%)\n`;
      result += `✨ Remaining: ${remaining.toLocaleString()} tokens\n`;
      result += `[${progressBar}]\n\n`;
      result += `πŸ“₯ Input: ${totalInput.toLocaleString()} tokens\n`;
      result += `πŸ“€ Output: ${totalOutput.toLocaleString()} tokens\n`;
      result += `πŸ’΅ Cost: ${formatCost(usage.totalCost)}\n`;
      result += `━━━━━━━━━━━━━━━━━━━━━━\n`;
      
      // Model breakdown
      if (Object.keys(usage.usageByModel).length > 0) {
        result += `\nπŸ“‹ Model Breakdown:\n`;
        Object.entries(usage.usageByModel).forEach(([model, data]) => {
          result += `  β€’ ${model}: ${data.tokens.toLocaleString()} tokens (${formatCost(data.cost)})\n`;
        });
      }
      
      return {
        content: [{ type: 'text', text: result }]
      };
    }
  • Input schema definition specifying optional user_id (defaults to 'current-session') and total_budget (defaults to 190000) parameters for the tool.
    inputSchema: {
      type: 'object',
      properties: {
        user_id: {
          type: 'string',
          description: 'User ID (defaults to current-session)',
          default: 'current-session'
        },
        total_budget: {
          type: 'number',
          description: 'Total token budget (optional)',
          default: 190000
        }
      }
    }
  • Registration of the tool in the ListToolsRequestSchema handler, providing name, description, and input schema.
    {
      name: 'get_current_session',
      description: 'Get current session usage with intuitive format (remaining, used, input/output tokens, cost)',
      inputSchema: {
        type: 'object',
        properties: {
          user_id: {
            type: 'string',
            description: 'User ID (defaults to current-session)',
            default: 'current-session'
          },
          total_budget: {
            type: 'number',
            description: 'Total token budget (optional)',
            default: 190000
          }
        }
      }
    },
  • Handler registration/dispatch in the CallToolRequestSchema switch statement, mapping tool calls to the getCurrentSession method.
    case 'get_current_session':
      return this.getCurrentSession(request.params.arguments);

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