nyk commited on
Commit
a69dc45
·
unverified ·
2 Parent(s): e24dd1e473b701

Merge pull request #59 from builderz-labs/feat/agent-cost-panel

Browse files
src/app/api/tokens/route.ts CHANGED
@@ -215,10 +215,79 @@ export async function GET(request: NextRequest) {
215
  sessionStats[sessionId] = calculateStats(records)
216
  }
217
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
  return NextResponse.json({
219
  summary: overallStats,
220
  models: modelStats,
221
  sessions: sessionStats,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  timeframe,
223
  recordCount: filteredData.length,
224
  })
 
215
  sessionStats[sessionId] = calculateStats(records)
216
  }
217
 
218
+ // Agent aggregation: extract agent name from sessionId (format: "agentName:chatType")
219
+ const agentGroups = filteredData.reduce((acc, record) => {
220
+ const agent = record.sessionId.split(':')[0] || 'unknown'
221
+ if (!acc[agent]) acc[agent] = []
222
+ acc[agent].push(record)
223
+ return acc
224
+ }, {} as Record<string, TokenUsageRecord[]>)
225
+
226
+ const agentStats: Record<string, TokenStats> = {}
227
+ for (const [agent, records] of Object.entries(agentGroups)) {
228
+ agentStats[agent] = calculateStats(records)
229
+ }
230
+
231
  return NextResponse.json({
232
  summary: overallStats,
233
  models: modelStats,
234
  sessions: sessionStats,
235
+ agents: agentStats,
236
+ timeframe,
237
+ recordCount: filteredData.length,
238
+ })
239
+ }
240
+
241
+ if (action === 'agent-costs') {
242
+ const agentGroups = filteredData.reduce((acc, record) => {
243
+ const agent = record.sessionId.split(':')[0] || 'unknown'
244
+ if (!acc[agent]) acc[agent] = []
245
+ acc[agent].push(record)
246
+ return acc
247
+ }, {} as Record<string, TokenUsageRecord[]>)
248
+
249
+ const agents: Record<string, {
250
+ stats: TokenStats
251
+ models: Record<string, TokenStats>
252
+ sessions: string[]
253
+ timeline: Array<{ date: string; cost: number; tokens: number }>
254
+ }> = {}
255
+
256
+ for (const [agent, records] of Object.entries(agentGroups)) {
257
+ const stats = calculateStats(records)
258
+
259
+ // Per-agent model breakdown
260
+ const modelGroups = records.reduce((acc, r) => {
261
+ if (!acc[r.model]) acc[r.model] = []
262
+ acc[r.model].push(r)
263
+ return acc
264
+ }, {} as Record<string, TokenUsageRecord[]>)
265
+ const models: Record<string, TokenStats> = {}
266
+ for (const [model, mrs] of Object.entries(modelGroups)) {
267
+ models[model] = calculateStats(mrs)
268
+ }
269
+
270
+ // Unique sessions
271
+ const sessions = [...new Set(records.map(r => r.sessionId))]
272
+
273
+ // Daily timeline
274
+ const dailyMap = records.reduce((acc, r) => {
275
+ const date = new Date(r.timestamp).toISOString().split('T')[0]
276
+ if (!acc[date]) acc[date] = { cost: 0, tokens: 0 }
277
+ acc[date].cost += r.cost
278
+ acc[date].tokens += r.totalTokens
279
+ return acc
280
+ }, {} as Record<string, { cost: number; tokens: number }>)
281
+
282
+ const timeline = Object.entries(dailyMap)
283
+ .sort(([a], [b]) => a.localeCompare(b))
284
+ .map(([date, data]) => ({ date, ...data }))
285
+
286
+ agents[agent] = { stats, models, sessions, timeline }
287
+ }
288
+
289
+ return NextResponse.json({
290
+ agents,
291
  timeframe,
292
  recordCount: filteredData.length,
293
  })
src/app/page.tsx CHANGED
@@ -10,6 +10,7 @@ import { LogViewerPanel } from '@/components/panels/log-viewer-panel'
10
  import { CronManagementPanel } from '@/components/panels/cron-management-panel'
11
  import { MemoryBrowserPanel } from '@/components/panels/memory-browser-panel'
12
  import { TokenDashboardPanel } from '@/components/panels/token-dashboard-panel'
 
13
  import { SessionDetailsPanel } from '@/components/panels/session-details-panel'
14
  import { TaskBoardPanel } from '@/components/panels/task-board-panel'
15
  import { ActivityFeedPanel } from '@/components/panels/activity-feed-panel'
@@ -163,6 +164,8 @@ function ContentRouter({ tab }: { tab: string }) {
163
  return <MemoryBrowserPanel />
164
  case 'tokens':
165
  return <TokenDashboardPanel />
 
 
166
  case 'users':
167
  return <UserManagementPanel />
168
  case 'history':
 
10
  import { CronManagementPanel } from '@/components/panels/cron-management-panel'
11
  import { MemoryBrowserPanel } from '@/components/panels/memory-browser-panel'
12
  import { TokenDashboardPanel } from '@/components/panels/token-dashboard-panel'
13
+ import { AgentCostPanel } from '@/components/panels/agent-cost-panel'
14
  import { SessionDetailsPanel } from '@/components/panels/session-details-panel'
15
  import { TaskBoardPanel } from '@/components/panels/task-board-panel'
16
  import { ActivityFeedPanel } from '@/components/panels/activity-feed-panel'
 
164
  return <MemoryBrowserPanel />
165
  case 'tokens':
166
  return <TokenDashboardPanel />
167
+ case 'agent-costs':
168
+ return <AgentCostPanel />
169
  case 'users':
170
  return <UserManagementPanel />
171
  case 'history':
src/components/layout/nav-rail.tsx CHANGED
@@ -33,6 +33,7 @@ const navGroups: NavGroup[] = [
33
  { id: 'activity', label: 'Activity', icon: <ActivityIcon />, priority: true },
34
  { id: 'logs', label: 'Logs', icon: <LogsIcon />, priority: false },
35
  { id: 'tokens', label: 'Tokens', icon: <TokensIcon />, priority: false },
 
36
  { id: 'memory', label: 'Memory', icon: <MemoryIcon />, priority: false },
37
  ],
38
  },
@@ -578,6 +579,17 @@ function IntegrationsIcon() {
578
  )
579
  }
580
 
 
 
 
 
 
 
 
 
 
 
 
581
  function SettingsIcon() {
582
  return (
583
  <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
 
33
  { id: 'activity', label: 'Activity', icon: <ActivityIcon />, priority: true },
34
  { id: 'logs', label: 'Logs', icon: <LogsIcon />, priority: false },
35
  { id: 'tokens', label: 'Tokens', icon: <TokensIcon />, priority: false },
36
+ { id: 'agent-costs', label: 'Agent Costs', icon: <AgentCostsIcon />, priority: false },
37
  { id: 'memory', label: 'Memory', icon: <MemoryIcon />, priority: false },
38
  ],
39
  },
 
579
  )
580
  }
581
 
582
+ function AgentCostsIcon() {
583
+ return (
584
+ <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
585
+ <circle cx="6" cy="5" r="3" />
586
+ <path d="M1 14c0-2.8 2.2-5 5-5" />
587
+ <circle cx="12" cy="10" r="3.5" />
588
+ <path d="M12 8.5v3M10.8 10h2.4" />
589
+ </svg>
590
+ )
591
+ }
592
+
593
  function SettingsIcon() {
594
  return (
595
  <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
src/components/panels/agent-cost-panel.tsx ADDED
@@ -0,0 +1,302 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 'use client'
2
+
3
+ import { useState, useEffect, useCallback } from 'react'
4
+ import {
5
+ PieChart, Pie, Cell, LineChart, Line, XAxis, YAxis, CartesianGrid,
6
+ Tooltip, Legend, ResponsiveContainer,
7
+ } from 'recharts'
8
+
9
+ interface AgentCostData {
10
+ stats: { totalTokens: number; totalCost: number; requestCount: number; avgTokensPerRequest: number; avgCostPerRequest: number }
11
+ models: Record<string, { totalTokens: number; totalCost: number; requestCount: number }>
12
+ sessions: string[]
13
+ timeline: Array<{ date: string; cost: number; tokens: number }>
14
+ }
15
+
16
+ interface AgentCostsResponse {
17
+ agents: Record<string, AgentCostData>
18
+ timeframe: string
19
+ recordCount: number
20
+ }
21
+
22
+ const COLORS = ['#0088FE', '#00C49F', '#FFBB28', '#FF8042', '#8884d8', '#82ca9d', '#ffc658', '#ff6b6b']
23
+
24
+ export function AgentCostPanel() {
25
+ const [selectedTimeframe, setSelectedTimeframe] = useState<'hour' | 'day' | 'week' | 'month'>('day')
26
+ const [data, setData] = useState<AgentCostsResponse | null>(null)
27
+ const [isLoading, setIsLoading] = useState(false)
28
+ const [expandedAgent, setExpandedAgent] = useState<string | null>(null)
29
+
30
+ const loadData = useCallback(async () => {
31
+ setIsLoading(true)
32
+ try {
33
+ const res = await fetch(`/api/tokens?action=agent-costs&timeframe=${selectedTimeframe}`)
34
+ const json = await res.json()
35
+ setData(json)
36
+ } catch (err) {
37
+ console.error('Failed to load agent costs:', err)
38
+ } finally {
39
+ setIsLoading(false)
40
+ }
41
+ }, [selectedTimeframe])
42
+
43
+ useEffect(() => { loadData() }, [loadData])
44
+
45
+ const formatNumber = (num: number) => {
46
+ if (num >= 1_000_000) return (num / 1_000_000).toFixed(1) + 'M'
47
+ if (num >= 1_000) return (num / 1_000).toFixed(1) + 'K'
48
+ return num.toString()
49
+ }
50
+
51
+ const formatCost = (cost: number) => '$' + cost.toFixed(4)
52
+
53
+ const agents = data?.agents ? Object.entries(data.agents) : []
54
+ const sortedAgents = agents.sort(([, a], [, b]) => b.stats.totalCost - a.stats.totalCost)
55
+
56
+ const totalCost = agents.reduce((sum, [, a]) => sum + a.stats.totalCost, 0)
57
+ const totalAgents = agents.length
58
+
59
+ const mostExpensive = sortedAgents[0]
60
+ const mostEfficient = agents.length > 0
61
+ ? agents.reduce((best, curr) => {
62
+ const currCostPer1k = curr[1].stats.totalCost / Math.max(1, curr[1].stats.totalTokens) * 1000
63
+ const bestCostPer1k = best[1].stats.totalCost / Math.max(1, best[1].stats.totalTokens) * 1000
64
+ return currCostPer1k < bestCostPer1k ? curr : best
65
+ })
66
+ : null
67
+
68
+ // Pie chart data
69
+ const pieData = sortedAgents.slice(0, 8).map(([name, a]) => ({
70
+ name,
71
+ value: a.stats.totalCost,
72
+ }))
73
+
74
+ // Line chart: top 5 agents over time
75
+ const top5 = sortedAgents.slice(0, 5).map(([name]) => name)
76
+ const allDates = new Set<string>()
77
+ for (const [name, a] of agents) {
78
+ if (top5.includes(name)) {
79
+ for (const t of a.timeline) allDates.add(t.date)
80
+ }
81
+ }
82
+ const trendData = [...allDates].sort().map(date => {
83
+ const point: Record<string, string | number> = { date: date.slice(5) } // MM-DD
84
+ for (const name of top5) {
85
+ const entry = data?.agents[name]?.timeline.find(t => t.date === date)
86
+ point[name] = entry?.cost ?? 0
87
+ }
88
+ return point
89
+ })
90
+
91
+ // Efficiency bars
92
+ const efficiencyData = sortedAgents.map(([name, a]) => ({
93
+ name,
94
+ costPer1k: a.stats.totalCost / Math.max(1, a.stats.totalTokens) * 1000,
95
+ }))
96
+ const maxCostPer1k = Math.max(...efficiencyData.map(d => d.costPer1k), 0.0001)
97
+
98
+ return (
99
+ <div className="p-6 space-y-6">
100
+ {/* Header */}
101
+ <div className="border-b border-border pb-4">
102
+ <div className="flex items-center justify-between">
103
+ <div>
104
+ <h1 className="text-3xl font-bold text-foreground">Agent Cost Breakdown</h1>
105
+ <p className="text-muted-foreground mt-2">Per-agent token usage and spend analysis</p>
106
+ </div>
107
+ <div className="flex space-x-2">
108
+ {(['hour', 'day', 'week', 'month'] as const).map((tf) => (
109
+ <button
110
+ key={tf}
111
+ onClick={() => setSelectedTimeframe(tf)}
112
+ className={`px-4 py-2 text-sm rounded-md font-medium transition-colors ${
113
+ selectedTimeframe === tf
114
+ ? 'bg-primary text-primary-foreground'
115
+ : 'bg-secondary text-muted-foreground hover:text-foreground hover:bg-secondary/80'
116
+ }`}
117
+ >
118
+ {tf.charAt(0).toUpperCase() + tf.slice(1)}
119
+ </button>
120
+ ))}
121
+ </div>
122
+ </div>
123
+ </div>
124
+
125
+ {isLoading ? (
126
+ <div className="flex items-center justify-center h-32">
127
+ <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary" />
128
+ <span className="ml-3 text-muted-foreground">Loading agent costs...</span>
129
+ </div>
130
+ ) : !data || agents.length === 0 ? (
131
+ <div className="text-center text-muted-foreground py-12">
132
+ <div className="text-lg mb-2">No agent cost data available</div>
133
+ <div className="text-sm">Cost data will appear once agents start using tokens</div>
134
+ <button onClick={loadData} className="mt-4 px-4 py-2 bg-primary text-primary-foreground rounded-md hover:bg-primary/90 transition-colors">
135
+ Refresh
136
+ </button>
137
+ </div>
138
+ ) : (
139
+ <div className="space-y-6">
140
+ {/* Summary Cards */}
141
+ <div className="grid grid-cols-1 md:grid-cols-4 gap-6">
142
+ <div className="bg-card border border-border rounded-lg p-6">
143
+ <div className="text-3xl font-bold text-foreground">{totalAgents}</div>
144
+ <div className="text-sm text-muted-foreground">Total Agents</div>
145
+ </div>
146
+ <div className="bg-card border border-border rounded-lg p-6">
147
+ <div className="text-3xl font-bold text-foreground">{formatCost(totalCost)}</div>
148
+ <div className="text-sm text-muted-foreground">Total Cost ({selectedTimeframe})</div>
149
+ </div>
150
+ <div className="bg-card border border-border rounded-lg p-6">
151
+ <div className="text-3xl font-bold text-orange-500">{mostExpensive?.[0] || '-'}</div>
152
+ <div className="text-sm text-muted-foreground">Most Expensive Agent</div>
153
+ {mostExpensive && <div className="text-xs text-muted-foreground mt-1">{formatCost(mostExpensive[1].stats.totalCost)}</div>}
154
+ </div>
155
+ <div className="bg-card border border-border rounded-lg p-6">
156
+ <div className="text-3xl font-bold text-green-500">{mostEfficient?.[0] || '-'}</div>
157
+ <div className="text-sm text-muted-foreground">Most Efficient Agent</div>
158
+ {mostEfficient && (
159
+ <div className="text-xs text-muted-foreground mt-1">
160
+ ${(mostEfficient[1].stats.totalCost / Math.max(1, mostEfficient[1].stats.totalTokens) * 1000).toFixed(4)}/1K tokens
161
+ </div>
162
+ )}
163
+ </div>
164
+ </div>
165
+
166
+ {/* Charts */}
167
+ <div className="grid lg:grid-cols-2 gap-6">
168
+ {/* Cost Distribution Pie */}
169
+ <div className="bg-card border border-border rounded-lg p-6">
170
+ <h2 className="text-xl font-semibold mb-4">Cost Distribution by Agent</h2>
171
+ <div className="h-64">
172
+ {pieData.length === 0 ? (
173
+ <div className="h-full flex items-center justify-center text-muted-foreground text-sm">No cost data</div>
174
+ ) : (
175
+ <ResponsiveContainer width="100%" height="100%">
176
+ <PieChart>
177
+ <Pie data={pieData} cx="50%" cy="50%" innerRadius={40} outerRadius={80} paddingAngle={5} dataKey="value">
178
+ {pieData.map((_, i) => (
179
+ <Cell key={`cell-${i}`} fill={COLORS[i % COLORS.length]} />
180
+ ))}
181
+ </Pie>
182
+ <Tooltip formatter={(value) => formatCost(Number(value))} />
183
+ <Legend />
184
+ </PieChart>
185
+ </ResponsiveContainer>
186
+ )}
187
+ </div>
188
+ </div>
189
+
190
+ {/* Cost Trend Lines */}
191
+ <div className="bg-card border border-border rounded-lg p-6">
192
+ <h2 className="text-xl font-semibold mb-4">Cost Trends (Top 5 Agents)</h2>
193
+ <div className="h-64">
194
+ {trendData.length === 0 ? (
195
+ <div className="h-full flex items-center justify-center text-muted-foreground text-sm">No trend data</div>
196
+ ) : (
197
+ <ResponsiveContainer width="100%" height="100%">
198
+ <LineChart data={trendData}>
199
+ <CartesianGrid strokeDasharray="3 3" />
200
+ <XAxis dataKey="date" />
201
+ <YAxis />
202
+ <Tooltip formatter={(value) => formatCost(Number(value))} />
203
+ <Legend />
204
+ {top5.map((name, i) => (
205
+ <Line key={name} type="monotone" dataKey={name} stroke={COLORS[i % COLORS.length]} strokeWidth={2} dot={false} />
206
+ ))}
207
+ </LineChart>
208
+ </ResponsiveContainer>
209
+ )}
210
+ </div>
211
+ </div>
212
+ </div>
213
+
214
+ {/* Cost Efficiency Comparison */}
215
+ <div className="bg-card border border-border rounded-lg p-6">
216
+ <h2 className="text-xl font-semibold mb-4">Cost Efficiency ($/1K Tokens per Agent)</h2>
217
+ <div className="space-y-2">
218
+ {efficiencyData.map(({ name, costPer1k }) => (
219
+ <div key={name} className="flex items-center text-sm">
220
+ <div className="w-32 truncate text-muted-foreground font-medium">{name}</div>
221
+ <div className="flex-1 mx-3">
222
+ <div className="w-full bg-secondary rounded-full h-2">
223
+ <div
224
+ className="bg-blue-500 h-2 rounded-full"
225
+ style={{ width: `${(costPer1k / maxCostPer1k) * 100}%` }}
226
+ />
227
+ </div>
228
+ </div>
229
+ <div className="w-24 text-right text-xs text-muted-foreground">${costPer1k.toFixed(4)}/1K</div>
230
+ </div>
231
+ ))}
232
+ </div>
233
+ </div>
234
+
235
+ {/* Agent Cost Ranking Table */}
236
+ <div className="bg-card border border-border rounded-lg p-6">
237
+ <h2 className="text-xl font-semibold mb-4">Agent Cost Ranking</h2>
238
+ <div className="space-y-2 max-h-[500px] overflow-y-auto">
239
+ {sortedAgents.map(([name, a], index) => (
240
+ <div key={name} className="border border-border rounded-lg overflow-hidden">
241
+ <button
242
+ onClick={() => setExpandedAgent(expandedAgent === name ? null : name)}
243
+ className="w-full p-4 flex items-center justify-between hover:bg-secondary/50 transition-colors text-left"
244
+ >
245
+ <div className="flex items-center gap-3">
246
+ <span className="text-xs text-muted-foreground w-6">#{index + 1}</span>
247
+ <span className="font-medium text-foreground">{name}</span>
248
+ <span className="text-xs px-2 py-0.5 rounded-full bg-secondary text-muted-foreground">
249
+ {a.sessions.length} session{a.sessions.length !== 1 ? 's' : ''}
250
+ </span>
251
+ </div>
252
+ <div className="flex items-center gap-6 text-sm">
253
+ <div className="text-right">
254
+ <div className="font-medium text-foreground">{formatCost(a.stats.totalCost)}</div>
255
+ <div className="text-xs text-muted-foreground">{formatNumber(a.stats.totalTokens)} tokens</div>
256
+ </div>
257
+ <div className="text-right">
258
+ <div className="text-muted-foreground">{a.stats.requestCount} reqs</div>
259
+ <div className="text-xs text-muted-foreground">{formatCost(a.stats.avgCostPerRequest)} avg</div>
260
+ </div>
261
+ <svg
262
+ className={`w-4 h-4 text-muted-foreground transition-transform ${expandedAgent === name ? 'rotate-180' : ''}`}
263
+ viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"
264
+ >
265
+ <polyline points="4,6 8,10 12,6" />
266
+ </svg>
267
+ </div>
268
+ </button>
269
+
270
+ {expandedAgent === name && (
271
+ <div className="px-4 pb-4 border-t border-border bg-secondary/30">
272
+ <div className="pt-3 text-sm">
273
+ <h4 className="font-medium text-muted-foreground mb-2">Model Breakdown</h4>
274
+ <div className="space-y-1.5">
275
+ {Object.entries(a.models)
276
+ .sort(([, x], [, y]) => y.totalCost - x.totalCost)
277
+ .map(([model, stats]) => {
278
+ const displayName = model.split('/').pop() || model
279
+ return (
280
+ <div key={model} className="flex items-center justify-between text-xs">
281
+ <span className="text-muted-foreground">{displayName}</span>
282
+ <div className="flex gap-4">
283
+ <span>{formatNumber(stats.totalTokens)} tokens</span>
284
+ <span>{stats.requestCount} reqs</span>
285
+ <span className="font-medium text-foreground">{formatCost(stats.totalCost)}</span>
286
+ </div>
287
+ </div>
288
+ )
289
+ })}
290
+ </div>
291
+ </div>
292
+ </div>
293
+ )}
294
+ </div>
295
+ ))}
296
+ </div>
297
+ </div>
298
+ </div>
299
+ )}
300
+ </div>
301
+ )
302
+ }
tests/agent-costs.spec.ts ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { test, expect } from '@playwright/test'
2
+ import { API_KEY_HEADER } from './helpers'
3
+
4
+ test.describe('Agent Costs API', () => {
5
+ test('GET action=stats includes agents field', async ({ request }) => {
6
+ const res = await request.get('/api/tokens?action=stats&timeframe=all', {
7
+ headers: API_KEY_HEADER,
8
+ })
9
+ expect(res.status()).toBe(200)
10
+ const body = await res.json()
11
+ expect(body).toHaveProperty('summary')
12
+ expect(body).toHaveProperty('models')
13
+ expect(body).toHaveProperty('sessions')
14
+ expect(body).toHaveProperty('agents')
15
+ expect(body).toHaveProperty('timeframe')
16
+ expect(body).toHaveProperty('recordCount')
17
+ expect(typeof body.agents).toBe('object')
18
+ })
19
+
20
+ test('GET action=agent-costs returns per-agent breakdown', async ({ request }) => {
21
+ const res = await request.get('/api/tokens?action=agent-costs&timeframe=all', {
22
+ headers: API_KEY_HEADER,
23
+ })
24
+ expect(res.status()).toBe(200)
25
+ const body = await res.json()
26
+ expect(body).toHaveProperty('agents')
27
+ expect(body).toHaveProperty('timeframe')
28
+ expect(body).toHaveProperty('recordCount')
29
+ expect(typeof body.agents).toBe('object')
30
+
31
+ // If there are agents, verify structure
32
+ for (const [, agentData] of Object.entries(body.agents) as [string, any][]) {
33
+ expect(agentData).toHaveProperty('stats')
34
+ expect(agentData).toHaveProperty('models')
35
+ expect(agentData).toHaveProperty('sessions')
36
+ expect(agentData).toHaveProperty('timeline')
37
+ expect(agentData.stats).toHaveProperty('totalTokens')
38
+ expect(agentData.stats).toHaveProperty('totalCost')
39
+ expect(agentData.stats).toHaveProperty('requestCount')
40
+ expect(Array.isArray(agentData.sessions)).toBe(true)
41
+ expect(Array.isArray(agentData.timeline)).toBe(true)
42
+ }
43
+ })
44
+
45
+ test('GET action=agent-costs respects timeframe filtering', async ({ request }) => {
46
+ // Create a token record to ensure data exists
47
+ await request.post('/api/tokens', {
48
+ headers: API_KEY_HEADER,
49
+ data: {
50
+ model: 'claude-sonnet-4',
51
+ sessionId: 'e2e-cost-agent:chat',
52
+ inputTokens: 100,
53
+ outputTokens: 50,
54
+ },
55
+ })
56
+
57
+ const resHour = await request.get('/api/tokens?action=agent-costs&timeframe=hour', {
58
+ headers: API_KEY_HEADER,
59
+ })
60
+ expect(resHour.status()).toBe(200)
61
+ const hourData = await resHour.json()
62
+ expect(hourData.timeframe).toBe('hour')
63
+
64
+ const resMonth = await request.get('/api/tokens?action=agent-costs&timeframe=month', {
65
+ headers: API_KEY_HEADER,
66
+ })
67
+ expect(resMonth.status()).toBe(200)
68
+ const monthData = await resMonth.json()
69
+ expect(monthData.timeframe).toBe('month')
70
+ // Month should include at least as many records as hour
71
+ expect(monthData.recordCount).toBeGreaterThanOrEqual(hourData.recordCount)
72
+ })
73
+
74
+ test('POST /api/tokens records data that appears in agent-costs', async ({ request }) => {
75
+ const agentName = `e2e-costtest-${Date.now()}`
76
+ const postRes = await request.post('/api/tokens', {
77
+ headers: API_KEY_HEADER,
78
+ data: {
79
+ model: 'claude-sonnet-4',
80
+ sessionId: `${agentName}:chat`,
81
+ inputTokens: 500,
82
+ outputTokens: 200,
83
+ },
84
+ })
85
+ expect(postRes.status()).toBe(200)
86
+
87
+ const res = await request.get('/api/tokens?action=agent-costs&timeframe=hour', {
88
+ headers: API_KEY_HEADER,
89
+ })
90
+ const body = await res.json()
91
+ expect(body.agents).toHaveProperty(agentName)
92
+ expect(body.agents[agentName].stats.totalTokens).toBe(700)
93
+ expect(body.agents[agentName].stats.requestCount).toBe(1)
94
+ })
95
+
96
+ test('GET action=agent-costs requires auth', async ({ request }) => {
97
+ const res = await request.get('/api/tokens?action=agent-costs&timeframe=all')
98
+ expect(res.status()).toBe(401)
99
+ })
100
+ })