Skip to content

Commit c2efa65

Browse files
committed
feat(ccusage): add Gemini CLI usage adapter
Add a Gemini CLI adapter that discovers JSON and JSONL chat logs under GEMINI_DATA_DIR, defaulting to ~/.gemini/tmp. The parser normalises Gemini token records into ccusage rows, separates cache-inclusive input into cache read tokens, keeps reasoning and tool token metadata, and supports daily, monthly, and session reports. Wire the gemini namespace into unified agent detection, focused commands, config schema generation, and docs. Pricing uses LiteLLM Gemini entries, including Google, Vertex AI, and OpenRouter prefixes, and applies Gemini 128k input/output tiers when available. Update table rendering to use explicit source total tokens when adapters provide them, so hidden reasoning tokens remain reflected in totals without inflating visible output tokens.
1 parent 3264ec6 commit c2efa65

22 files changed

Lines changed: 1026 additions & 15 deletions

File tree

‎apps/ccusage/config-schema.json‎

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,153 @@
631631
}
632632
},
633633
"additionalProperties": false
634+
},
635+
"gemini:daily": {
636+
"type": "object",
637+
"properties": {
638+
"json": {
639+
"type": "boolean",
640+
"description": "Output in JSON format",
641+
"markdownDescription": "Output in JSON format",
642+
"default": false
643+
},
644+
"since": {
645+
"type": "string",
646+
"description": "Filter from date (YYYY-MM-DD or YYYYMMDD)",
647+
"markdownDescription": "Filter from date (YYYY-MM-DD or YYYYMMDD)"
648+
},
649+
"until": {
650+
"type": "string",
651+
"description": "Filter until date (inclusive)",
652+
"markdownDescription": "Filter until date (inclusive)"
653+
},
654+
"timezone": {
655+
"type": "string",
656+
"description": "Timezone for date grouping (IANA)",
657+
"markdownDescription": "Timezone for date grouping (IANA)"
658+
},
659+
"offline": {
660+
"type": "boolean",
661+
"description": "Use cached pricing data instead of fetching from LiteLLM",
662+
"markdownDescription": "Use cached pricing data instead of fetching from LiteLLM",
663+
"default": false
664+
},
665+
"compact": {
666+
"type": "boolean",
667+
"description": "Force compact table layout for narrow terminals",
668+
"markdownDescription": "Force compact table layout for narrow terminals",
669+
"default": false
670+
},
671+
"color": {
672+
"type": "boolean",
673+
"description": "Enable colored output (default: auto). FORCE_COLOR=1 has the same effect.",
674+
"markdownDescription": "Enable colored output (default: auto). FORCE_COLOR=1 has the same effect."
675+
},
676+
"noColor": {
677+
"type": "boolean",
678+
"description": "Disable colored output (default: auto). NO_COLOR=1 has the same effect.",
679+
"markdownDescription": "Disable colored output (default: auto). NO_COLOR=1 has the same effect."
680+
}
681+
},
682+
"additionalProperties": false
683+
},
684+
"gemini:monthly": {
685+
"type": "object",
686+
"properties": {
687+
"json": {
688+
"type": "boolean",
689+
"description": "Output in JSON format",
690+
"markdownDescription": "Output in JSON format",
691+
"default": false
692+
},
693+
"since": {
694+
"type": "string",
695+
"description": "Filter from date (YYYY-MM-DD or YYYYMMDD)",
696+
"markdownDescription": "Filter from date (YYYY-MM-DD or YYYYMMDD)"
697+
},
698+
"until": {
699+
"type": "string",
700+
"description": "Filter until date (inclusive)",
701+
"markdownDescription": "Filter until date (inclusive)"
702+
},
703+
"timezone": {
704+
"type": "string",
705+
"description": "Timezone for date grouping (IANA)",
706+
"markdownDescription": "Timezone for date grouping (IANA)"
707+
},
708+
"offline": {
709+
"type": "boolean",
710+
"description": "Use cached pricing data instead of fetching from LiteLLM",
711+
"markdownDescription": "Use cached pricing data instead of fetching from LiteLLM",
712+
"default": false
713+
},
714+
"compact": {
715+
"type": "boolean",
716+
"description": "Force compact table layout for narrow terminals",
717+
"markdownDescription": "Force compact table layout for narrow terminals",
718+
"default": false
719+
},
720+
"color": {
721+
"type": "boolean",
722+
"description": "Enable colored output (default: auto). FORCE_COLOR=1 has the same effect.",
723+
"markdownDescription": "Enable colored output (default: auto). FORCE_COLOR=1 has the same effect."
724+
},
725+
"noColor": {
726+
"type": "boolean",
727+
"description": "Disable colored output (default: auto). NO_COLOR=1 has the same effect.",
728+
"markdownDescription": "Disable colored output (default: auto). NO_COLOR=1 has the same effect."
729+
}
730+
},
731+
"additionalProperties": false
732+
},
733+
"gemini:session": {
734+
"type": "object",
735+
"properties": {
736+
"json": {
737+
"type": "boolean",
738+
"description": "Output in JSON format",
739+
"markdownDescription": "Output in JSON format",
740+
"default": false
741+
},
742+
"since": {
743+
"type": "string",
744+
"description": "Filter from date (YYYY-MM-DD or YYYYMMDD)",
745+
"markdownDescription": "Filter from date (YYYY-MM-DD or YYYYMMDD)"
746+
},
747+
"until": {
748+
"type": "string",
749+
"description": "Filter until date (inclusive)",
750+
"markdownDescription": "Filter until date (inclusive)"
751+
},
752+
"timezone": {
753+
"type": "string",
754+
"description": "Timezone for date grouping (IANA)",
755+
"markdownDescription": "Timezone for date grouping (IANA)"
756+
},
757+
"offline": {
758+
"type": "boolean",
759+
"description": "Use cached pricing data instead of fetching from LiteLLM",
760+
"markdownDescription": "Use cached pricing data instead of fetching from LiteLLM",
761+
"default": false
762+
},
763+
"compact": {
764+
"type": "boolean",
765+
"description": "Force compact table layout for narrow terminals",
766+
"markdownDescription": "Force compact table layout for narrow terminals",
767+
"default": false
768+
},
769+
"color": {
770+
"type": "boolean",
771+
"description": "Enable colored output (default: auto). FORCE_COLOR=1 has the same effect.",
772+
"markdownDescription": "Enable colored output (default: auto). FORCE_COLOR=1 has the same effect."
773+
},
774+
"noColor": {
775+
"type": "boolean",
776+
"description": "Disable colored output (default: auto). NO_COLOR=1 has the same effect.",
777+
"markdownDescription": "Disable colored output (default: auto). NO_COLOR=1 has the same effect."
778+
}
779+
},
780+
"additionalProperties": false
634781
}
635782
},
636783
"additionalProperties": false,
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import type { AgentPricingContext } from '../shared.ts';
2+
import type { AdapterContext, AdapterOptions } from '../types.ts';
3+
import { LiteLLMPricingFetcher } from '@ccusage/internal/pricing';
4+
import { createFixture } from 'fs-fixture';
5+
import { logger } from '../../logger.ts';
6+
import { createAgentPricingContext, defineAgentLogLoader } from '../shared.ts';
7+
import { loadGeminiUsageEvents } from './parser.ts';
8+
import { detectGeminiLogFiles, GEMINI_DATA_DIR_ENV } from './paths.ts';
9+
import {
10+
calculateGeminiCost,
11+
GEMINI_PROVIDER_PREFIXES,
12+
loadOfflineGeminiPricing,
13+
} from './pricing.ts';
14+
15+
export async function detectGemini(): Promise<boolean> {
16+
return detectGeminiLogFiles();
17+
}
18+
19+
function createGeminiPricingContext(
20+
options: AdapterOptions,
21+
context: AdapterContext,
22+
): AgentPricingContext {
23+
return createAgentPricingContext(
24+
context,
25+
() =>
26+
new LiteLLMPricingFetcher({
27+
offline: options.offline === true,
28+
offlineLoader: loadOfflineGeminiPricing,
29+
logger,
30+
providerPrefixes: GEMINI_PROVIDER_PREFIXES,
31+
}),
32+
);
33+
}
34+
35+
export const loadGeminiRows = defineAgentLogLoader({
36+
agent: 'gemini',
37+
loadEntries: async () => loadGeminiUsageEvents(),
38+
prepare: createGeminiPricingContext,
39+
disposePrepared: (prepared) => {
40+
prepared.dispose();
41+
},
42+
getTimestamp: (entry) => entry.timestamp,
43+
getSessionId: (entry) => entry.sessionId,
44+
getModels: (entry) => [entry.model],
45+
getUsage: async (entry, prepared) => ({
46+
inputTokens: entry.inputTokens,
47+
outputTokens: entry.outputTokens,
48+
cacheCreationTokens: 0,
49+
cacheReadTokens: entry.cacheReadTokens,
50+
totalTokens: entry.totalTokens,
51+
totalCost: await calculateGeminiCost(entry, prepared.fetcher),
52+
}),
53+
getMetadata: (entries) => ({
54+
reasoningTokens: entries.reduce((total, entry) => total + entry.reasoningTokens, 0),
55+
toolTokens: entries.reduce((total, entry) => total + entry.toolTokens, 0),
56+
}),
57+
});
58+
59+
if (import.meta.vitest != null) {
60+
describe('loadGeminiRows', () => {
61+
afterEach(() => {
62+
vi.unstubAllEnvs();
63+
});
64+
65+
it('aggregates Gemini usage with cached input and reasoning tokens', async () => {
66+
await using fixture = await createFixture({
67+
project: {
68+
chats: {
69+
'session-a.jsonl': [
70+
JSON.stringify({
71+
sessionId: 'session-a',
72+
projectHash: 'project-a',
73+
startTime: '2026-05-17T11:07:00.000Z',
74+
}),
75+
JSON.stringify({
76+
id: 'msg-a',
77+
timestamp: '2026-05-17T11:07:32.000Z',
78+
type: 'gemini',
79+
model: 'gemini-3-flash-preview',
80+
tokens: {
81+
input: 15_327,
82+
output: 23,
83+
cached: 11_526,
84+
thoughts: 919,
85+
tool: 0,
86+
total: 16_269,
87+
},
88+
}),
89+
].join('\n'),
90+
},
91+
},
92+
});
93+
vi.stubEnv(GEMINI_DATA_DIR_ENV, fixture.path);
94+
95+
const rows = await loadGeminiRows(
96+
'daily',
97+
{ offline: true, timezone: 'UTC' },
98+
{
99+
pricingFetcher: new LiteLLMPricingFetcher({
100+
offline: true,
101+
offlineLoader: async () => ({
102+
'gemini-3-flash-preview': {
103+
input_cost_per_token: 1e-6,
104+
output_cost_per_token: 2e-6,
105+
cache_read_input_token_cost: 1e-7,
106+
},
107+
}),
108+
}),
109+
},
110+
);
111+
112+
expect(rows).toMatchObject([
113+
{
114+
period: '2026-05-17',
115+
agent: 'gemini',
116+
modelsUsed: ['gemini-3-flash-preview'],
117+
inputTokens: 3_801,
118+
outputTokens: 23,
119+
cacheReadTokens: 11_526,
120+
totalTokens: 16_269,
121+
metadata: { reasoningTokens: 919, toolTokens: 0 },
122+
},
123+
]);
124+
expect(rows[0]!.totalCost).toBeCloseTo(0.0068376);
125+
});
126+
});
127+
}

0 commit comments

Comments
 (0)