ccusage

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package ccusage provides integration with the ccusage CLI for fetching Claude Code API usage metrics. Handles subprocess execution, JSON parsing, and graceful degradation when ccusage is unavailable.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsAvailable

func IsAvailable() bool

IsAvailable checks if ccusage CLI is available (binary or via npx)

Types

type Granularity

type Granularity int

Granularity represents time granularity for ccusage reports

const (
	Daily Granularity = iota
	Weekly
	Monthly
)

type Metrics

type Metrics struct {
	InputTokens         uint64  `json:"inputTokens"`
	OutputTokens        uint64  `json:"outputTokens"`
	CacheCreationTokens uint64  `json:"cacheCreationTokens"`
	CacheReadTokens     uint64  `json:"cacheReadTokens"`
	TotalTokens         uint64  `json:"totalTokens"`
	TotalCost           float64 `json:"totalCost"`
}

Metrics represents usage metrics from ccusage for a single period

type Period

type Period struct {
	Key     string // "2026-01-30" (daily), "2026-01" (monthly), "2026-01-20" (weekly ISO monday)
	Metrics Metrics
}

Period represents period data with key (date/month/week) and metrics

func Fetch

func Fetch(granularity Granularity) ([]Period, error)

Fetch retrieves usage data from ccusage.

Returns nil if ccusage is unavailable (graceful degradation). Returns parsed data on success. Returns error only on unexpected failures (JSON parse, etc.).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL