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
| Name | Required | Description | Default |
|---|---|---|---|
| groupBy | Yes | How to group results | |
| days | No | Days to look back (default 30) | |
| provider | No | Filter by provider | |
| model | No | Filter by model |
Output Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| days | Yes | ||
| groupBy | Yes | ||
| breakdown | Yes |
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 }, }, - packages/mcp-server/src/tools.ts:291-306 (registration)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, };