Skip to main content
Glama

llmkit_cost_query

Read-onlyIdempotent

Query and analyze AI usage costs grouped by provider, model, session, or day to track spending and monitor budgets across multiple AI services.

Instructions

Query cost breakdown grouped by provider, model, session, or day

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
groupByYesHow to group results
daysNoDays to look back (default 30)
providerNoFilter by provider
modelNoFilter by model

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
daysYes
groupByYes
breakdownYes

Implementation Reference

  • The handler function 'handleCostQuery' that performs the cost query logic by calling 'getCosts' and formatting the output.
    export async function handleCostQuery(args: Record<string, unknown> | undefined) {
      const groupBy = (args?.groupBy as string) || 'provider';
      const days = (args?.days as number) || 30;
      const costs = await getCosts(groupBy, days, args?.provider as string, args?.model as string);
    
      const breakdown = costs.breakdown.map(g => ({
        key: g.key, costUsd: cents(g.costCents), requests: g.count, inputTokens: g.inputTokens, outputTokens: g.outputTokens,
      }));
    
      return ok([
        `Cost breakdown by ${groupBy} (${days}d)`,
        '\u2500'.repeat(25),
        ...breakdown.map(g => `${g.key}: $${g.costUsd.toFixed(2)} (${g.requests} reqs, ${g.inputTokens.toLocaleString()} in / ${g.outputTokens.toLocaleString()} out)`),
      ].join('\n'), { groupBy, days, breakdown });
    }
  • Schema definition for the 'llmkit_cost_query' tool, including input/output schemas and description.
    {
      name: 'llmkit_cost_query',
      description: 'Query cost breakdown grouped by provider, model, session, or day',
      inputSchema: {
        type: 'object' as const,
        properties: {
          groupBy: { type: 'string', enum: ['provider', 'model', 'session', 'day'], description: 'How to group results' },
          days: { type: 'number', description: 'Days to look back (default 30)' },
          provider: { type: 'string', description: 'Filter by provider' },
          model: { type: 'string', description: 'Filter by model' },
        },
        required: ['groupBy'],
      },
      outputSchema: {
        type: 'object' as const,
        properties: {
          groupBy: { type: 'string' },
          days: { type: 'number' },
          breakdown: { type: 'array', items: { type: 'object', properties: { key: { type: 'string' }, costUsd: { type: 'number' }, requests: { type: 'number' }, inputTokens: { type: 'number' }, outputTokens: { type: 'number' } } } },
        },
        required: ['groupBy', 'days', 'breakdown'],
      },
      annotations: { title: 'Cost Breakdown', ...HINTS },
    },
  • Tool registration in 'HANDLER_MAP' which maps the tool name to its corresponding handler function.
    const HANDLER_MAP: Record<string, Handler> = {
      llmkit_usage_stats: handleUsageStats,
      llmkit_cost_query: handleCostQuery,
      llmkit_list_keys: () => handleListKeys(),
      llmkit_budget_status: handleBudgetStatus,
      llmkit_health: () => handleHealth(),
      llmkit_session_summary: handleSessionSummary,
      llmkit_local_session: () => handleLocalSession(),
      llmkit_local_projects: () => handleLocalProjects(),
      llmkit_local_cache: () => handleLocalCache(),
      llmkit_local_forecast: () => handleLocalForecast(),
      llmkit_local_agents: () => handleLocalAgents(),
      llmkit_notion_cost_snapshot: handleNotionCostSnapshot,
      llmkit_notion_budget_check: handleNotionBudgetCheck,
      llmkit_notion_session_report: handleNotionSessionReport,
    };

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/smigolsmigol/llmkit-mcp-server'

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