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