Export ccusage data in structured JSON format for programmatic consumption
All ccusage commands support the --json flag to output structured JSON data instead of formatted tables. This is useful for integrating ccusage into scripts, dashboards, or other tooling.
The daily command outputs an array of daily usage objects:
type DailyProjectOutput = { date: string; // Date in YYYY-MM-DD format inputTokens: number; // Total input tokens outputTokens: number; // Total output tokens cacheCreationTokens: number; // Cache creation tokens cacheReadTokens: number; // Cache read tokens totalTokens: number; // Sum of all token types totalCost: number; // Total cost in USD modelsUsed: string[]; // Array of model names used modelBreakdowns: ModelBreakdown[]; // Per-model cost breakdown};
type ModelBreakdown = { model: string; // Model name inputTokens: number; // Input tokens for this model outputTokens: number; // Output tokens for this model cacheCreationTokens: number; // Cache creation tokens cacheReadTokens: number; // Cache read tokens totalCost: number; // Total cost for this model};
JSON output is sent to stdout, while any warnings or errors go to stderr. This allows you to pipe JSON data while still seeing diagnostic messages.
When using --instances flag, the output structure changes from an array to an object with project names as keys. Make sure your scripts handle both formats appropriately.