get_cost_analytics
Analyze cost time-series data for spend analysis and spike detection. Returns summary total cost, average cost per request, and per-bucket costs.
Instructions
Get cost time-series data with summary.total_cost, summary.average_cost_per_request, and per-bucket total/avg cost. Use this for spend analysis and spike detection; use get_token_analytics when you need token volume instead of monetary cost. Enterprise-gated. Returns 403 on non-Enterprise Portkey plans.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| time_of_generation_min | Yes | Start time for the analytics period (ISO8601 format, e.g., '2024-01-01T00:00:00Z') | |
| time_of_generation_max | Yes | End time for the analytics period (ISO8601 format, e.g., '2024-02-01T00:00:00Z') | |
| total_units_min | No | Minimum number of total tokens to filter by | |
| total_units_max | No | Maximum number of total tokens to filter by | |
| cost_min | No | Minimum cost in cents to filter by | |
| cost_max | No | Maximum cost in cents to filter by | |
| prompt_token_min | No | Minimum number of prompt tokens | |
| prompt_token_max | No | Maximum number of prompt tokens | |
| completion_token_min | No | Minimum number of completion tokens | |
| completion_token_max | No | Maximum number of completion tokens | |
| status_code | No | Legacy Portkey query param for HTTP status codes. Comma-separated string; prefer status_codes for structured inputs. | |
| weighted_feedback_min | No | Minimum weighted feedback score (-10 to 10) | |
| weighted_feedback_max | No | Maximum weighted feedback score (-10 to 10) | |
| virtual_keys | No | Legacy Portkey query param for virtual key slugs. Comma-separated string; prefer virtual_key_slugs for structured inputs. | |
| configs | No | Legacy Portkey query param for config slugs. Comma-separated string; prefer config_slugs for structured inputs. | |
| status_codes | No | Structured alias for status_code. Use an array of HTTP status codes; normalized to the legacy comma-separated Portkey query param. | |
| virtual_key_slugs | No | Structured alias for virtual_keys. Use an array of virtual key slugs; normalized to the legacy comma-separated Portkey query param. | |
| config_slugs | No | Structured alias for configs. Use an array of config slugs; normalized to the legacy comma-separated Portkey query param. | |
| workspace_slug | No | Filter by specific workspace | |
| api_key_ids | Yes | Legacy Portkey query param for API key UUIDs. Comma-separated string; request_analytics also accepts an array and normalizes it to this form. | |
| metadata | No | Legacy Portkey query param for metadata filtering. Stringified JSON object, e.g. '{"env":"prod","app":"myapp"}'; prefer metadata_filter for structured inputs. | |
| ai_org_model | No | Legacy Portkey query param for provider/model pairs. Format: 'provider__model' with double underscore, e.g. 'openai__gpt-4' or 'anthropic__claude-3-opus'. Comma-separated string; prefer provider_models for structured inputs. | |
| provider_models | No | Structured alias for ai_org_model. Use provider__model strings in an array; normalized to the legacy comma-separated Portkey query param. | |
| trace_id | No | Legacy Portkey query param for trace IDs. Comma-separated string; prefer trace_ids for structured inputs. | |
| trace_ids | No | Structured alias for trace_id. Use an array of trace IDs; normalized to the legacy comma-separated Portkey query param. | |
| span_id | No | Legacy Portkey query param for span IDs. Comma-separated string; prefer span_ids for structured inputs. | |
| span_ids | No | Structured alias for span_id. Use an array of span IDs; normalized to the legacy comma-separated Portkey query param. | |
| metadata_filter | No | Structured alias for metadata. Use an object such as { env: 'prod' }; normalized to a JSON string before the request is sent. | |
| prompt_slug | No | Filter by prompt slug |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| ok | Yes | Whether the tool call succeeded and returned structured data | |
| data | No | Structured success payload when ok is true | |
| error | No | Structured error payload when ok is false |
Implementation Reference
- src/tools/analytics.tools.ts:410-441 (handler)The MCP tool handler (callback) for 'get_cost_analytics'. It calls service.analytics.getCostAnalytics, maps the response data points to {timestamp, total_cost, average_cost}, then formats and returns the result via formatGraphAnalytics.
server.tool( "get_cost_analytics", "Get cost time-series data with summary.total_cost, summary.average_cost_per_request, and per-bucket total/avg cost. Use this for spend analysis and spike detection; use get_token_analytics when you need token volume instead of monetary cost.", baseAnalyticsSchema, async (params) => { const analytics = await service.analytics.getCostAnalytics( normalizeAnalyticsParams(params as Record<string, unknown>), ); const dataPoints = analytics.data_points.map((point) => ({ timestamp: point.timestamp, total_cost: point.total, average_cost: point.avg, })); return { content: [ { type: "text", text: JSON.stringify( formatGraphAnalytics( { total_cost: analytics.summary.total, average_cost_per_request: analytics.summary.avg, }, dataPoints, ), null, 2, ), }, ], }; }, - src/tools/analytics.tools.ts:264-277 (helper)The formatGraphAnalytics helper function used by the handler to structure the response with summary, point_count, and data_points.
function formatGraphAnalytics( summary: Record<string, unknown>, dataPoints: Record<string, unknown>[], ): { summary: Record<string, unknown>; point_count: number; data_points: Record<string, unknown>[]; } { return { summary, point_count: dataPoints.length, data_points: dataPoints, }; } - The actual service method getCostAnalytics that makes the HTTP GET request to '/analytics/graphs/cost' with the built analytics parameters.
async getCostAnalytics( params: CostAnalyticsParams, ): Promise<CostAnalyticsResponse> { return this.get<CostAnalyticsResponse>( "/analytics/graphs/cost", this.buildAnalyticsParams(params), ); } - Type definitions for CostAnalyticsParams, CostAnalyticsResponse, CostDataPoint, and CostSummary - the input/output shapes for the cost analytics endpoint.
// ==================== Cost Analytics Types ==================== export interface CostDataPoint { timestamp: string; total: number; avg: number; } export interface CostSummary { total: number; avg: number; } export interface CostAnalyticsResponse { object: "analytics-graph"; data_points: CostDataPoint[]; summary: CostSummary; } export interface CostAnalyticsParams extends BaseAnalyticsParams {} - src/tools/index.ts:30-39 (registration)The tool registration pipeline: registerAnalyticsTools is called from index.ts as part of the analytics domain (line 37). The tool name 'get_cost_analytics' is also listed in ENTERPRISE_GATED_TOOL_NAMES (line 111) for access control.
const TOOL_DOMAIN_REGISTRARS = [ ["users", registerUsersTools], ["workspaces", registerWorkspacesTools], ["configs", registerConfigsTools], ["keys", registerKeysTools], ["collections", registerCollectionsTools], ["prompts", registerPromptsTools], ["analytics", registerAnalyticsTools], ["guardrails", registerGuardrailsTools], ["limits", registerLimitsTools],