Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

feat: use monthly estimated API cost for usage chart bars

Adam Eivybcb0017241de
diff --git a/client/src/components/usage/MonthlyUsageCard.jsx b/client/src/components/usage/MonthlyUsageCard.jsx
index 70094ab414..c958098bf2 100644
--- a/client/src/components/usage/MonthlyUsageCard.jsx
+++ b/client/src/components/usage/MonthlyUsageCard.jsx
@@ -1,41 +1,33 @@
import { ComposedChart, Bar, Line, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from 'recharts';
-import { Link } from 'react-router';
import { formatCount, formatCompactCount, formatMonthYear, formatUsd } from '../../utils/formatters';
-const spendColor = 'rgb(var(--port-chart-2))';
+const costColor = 'rgb(var(--port-chart-2))';
const tokenColor = 'rgb(var(--port-chart-3))';
const monthLabel = (month) => formatMonthYear(`${month}-01`);
const compactUsd = (value) => formatUsd(value, { compact: true });
-export default function MonthlyUsageCard({ history = [], savings }) {
- const configured = savings?.configured === true;
- const points = history.map((point) => ({
- ...point,
- spend: configured ? savings.totals.monthlyCost : null,
- }));
-
+export default function MonthlyUsageCard({ history: points = [] }) {
return (
- <section className="min-w-0 bg-port-card border border-port-border rounded-xl p-3 sm:p-4 space-y-3" aria-label="Monthly spend and tokens">
- <h3 className="text-sm font-medium text-gray-400">Monthly subscription spend &amp; tokens</h3>
+ <section className="min-w-0 bg-port-card border border-port-border rounded-xl p-3 sm:p-4 space-y-3" aria-label="Monthly estimated API cost and tokens">
+ <h3 className="text-sm font-medium text-gray-400">Monthly estimated API cost &amp; tokens</h3>
<p className="text-xs text-gray-500">
- Last 12 calendar months, independent of the report filter. Bars show the full monthly subscription budget at your current prices, not historical invoices or API charges. Only plans with configured prices are included.
- {' '}The line shows this machine&rsquo;s recorded input, output and cache tokens; counts may include estimates. The current and first recorded months may have partial token history.
+ Last 12 calendar months, independent of the report filter. Bars show this machine’s estimated API cost at the same rates as the cost report.
+ {' '}The line shows recorded input, output and cache tokens; counts may include estimates. The current and first recorded months may have partial token history.
</p>
- {!configured && <p className="text-sm text-gray-400"><Link to="/models/subscriptions" className="text-port-accent underline">Set subscription prices</Link> to show dollar spend. Missing prices are not treated as zero.</p>}
<div className="flex flex-wrap gap-x-5 gap-y-1 text-xs text-gray-400">
- <span style={{ color: spendColor }}>■ Subscription budget (USD, left axis)</span>
+ <span style={{ color: costColor }}>■ Est. API cost (USD, left axis)</span>
<span style={{ color: tokenColor }}>━ Tokens (right axis)</span>
</div>
{points.length === 0 ? <p className="text-sm text-gray-500">No monthly usage recorded yet.</p> : <>
- <div className="h-64 min-w-0" role="img" aria-label="Monthly subscription budget bars with token trend; exact values in the table below">
+ <div className="h-64 min-w-0" role="img" aria-label="Monthly estimated API cost bars with token trend; exact values in the table below">
<ResponsiveContainer width="100%" height="100%">
<ComposedChart data={points} margin={{ top: 8, right: 0, bottom: 0, left: 0 }}>
<CartesianGrid stroke="rgb(var(--port-border))" strokeDasharray="3 3" />
<XAxis dataKey="month" tickFormatter={monthLabel} minTickGap={30} tick={{ fontSize: 10 }} />
- <YAxis yAxisId="spend" tickFormatter={compactUsd} width={55} tick={{ fontSize: 10 }} domain={[0, 'auto']} />
+ <YAxis yAxisId="cost" tickFormatter={compactUsd} width={55} tick={{ fontSize: 10 }} domain={[0, 'auto']} />
<YAxis yAxisId="tokens" orientation="right" tickFormatter={formatCompactCount} width={50} tick={{ fontSize: 10 }} domain={[0, 'auto']} />
- <Tooltip labelFormatter={monthLabel} formatter={(value, name) => [name === 'Subscription budget' ? formatUsd(value) : formatCount(value), name]} contentStyle={{ background: 'rgb(var(--port-card))', borderColor: 'rgb(var(--port-border))' }} />
- <Bar yAxisId="spend" dataKey="spend" name="Subscription budget" fill={spendColor} maxBarSize={36} isAnimationActive={false} />
+ <Tooltip labelFormatter={monthLabel} formatter={(value, name) => [name === 'Est. API cost' ? formatUsd(value) : formatCount(value), name]} contentStyle={{ background: 'rgb(var(--port-card))', borderColor: 'rgb(var(--port-border))' }} />
+ <Bar yAxisId="cost" dataKey="estimatedCost" name="Est. API cost" fill={costColor} maxBarSize={36} isAnimationActive={false} />
<Line yAxisId="tokens" dataKey="tokens" name="Tokens" stroke={tokenColor} strokeWidth={2} dot={{ r: 3 }} isAnimationActive={false} />
</ComposedChart>
</ResponsiveContainer>
@@ -43,10 +35,10 @@ export default function MonthlyUsageCard({ history = [], savings }) {
<details className="text-xs text-gray-400">
<summary className="cursor-pointer py-2">Monthly values</summary>
<table className="w-full text-left tabular-nums">
- <thead><tr><th className="py-2">Month</th><th>Budget (USD)</th><th>Tokens</th></tr></thead>
+ <thead><tr><th className="py-2">Month</th><th>Est. cost (USD)</th><th>Tokens</th></tr></thead>
<tbody>{points.map((point) => <tr key={point.month}>
<th className="py-2 font-normal">{monthLabel(point.month)}{point.partial ? ' (partial)' : ''}</th>
- <td>{configured ? formatUsd(point.spend) : 'Not configured'}</td>
+ <td>{formatUsd(point.estimatedCost)}</td>
<td>{formatCount(point.tokens)}</td>
</tr>)}</tbody>
</table>
diff --git a/client/src/components/usage/MonthlyUsageCard.test.jsx b/client/src/components/usage/MonthlyUsageCard.test.jsx
index a9b5ad86cb..26cd2b3a41 100644
--- a/client/src/components/usage/MonthlyUsageCard.test.jsx
+++ b/client/src/components/usage/MonthlyUsageCard.test.jsx
@@ -1,28 +1,25 @@
import { describe, expect, it } from 'vitest';
import { render, screen, within } from '@testing-library/react';
-import { MemoryRouter } from 'react-router';
import MonthlyUsageCard from './MonthlyUsageCard';
const history = [
- { month: '2026-09', tokens: 1250000, partial: false },
- { month: '2026-10', tokens: 0, partial: true },
+ { month: '2026-09', estimatedCost: 123.45, tokens: 1250000, partial: false },
+ { month: '2026-10', estimatedCost: 0, tokens: 0, partial: true },
];
describe('monthly usage', () => {
- it('labels configured dollar budgets honestly and exposes exact token totals without hover', () => {
- render(<MemoryRouter><MonthlyUsageCard history={history} savings={{ configured: true, totals: { monthlyCost: 220 } }} /></MemoryRouter>);
- expect(screen.getByText(/not historical invoices/)).toBeInTheDocument();
+ it('shows each month’s API cost and exact token totals without hover, including zero and partial months', () => {
+ render(<MonthlyUsageCard history={history} />);
+ expect(screen.getByRole('heading')).toHaveTextContent('Monthly estimated API cost & tokens');
const rows = within(screen.getByRole('table')).getAllByRole('row');
- expect(rows[1]).toHaveTextContent('September 2026$220.001,250,000');
- expect(rows[2]).toHaveTextContent('October 2026 (partial)$220.000');
+ expect(rows[1]).toHaveTextContent('September 2026$123.451,250,000');
+ expect(rows[2]).toHaveTextContent('October 2026 (partial)$0.000');
expect(screen.getByRole('img')).toHaveAccessibleName(/token trend/);
});
- it('keeps missing prices distinct from zero while retaining the token series', () => {
- render(<MemoryRouter><MonthlyUsageCard history={history} savings={{ configured: false }} /></MemoryRouter>);
- expect(screen.getByRole('link', { name: 'Set subscription prices' })).toHaveAttribute('href', '/models/subscriptions');
- expect(screen.getAllByText('Not configured')).toHaveLength(2);
- expect(screen.getByText('1,250,000')).toBeInTheDocument();
- expect(screen.queryByText('$0.00')).not.toBeInTheDocument();
+ it('shows an empty state when no monthly history is available', () => {
+ render(<MonthlyUsageCard />);
+ expect(screen.getByText('No monthly usage recorded yet.')).toBeInTheDocument();
+ expect(screen.queryByRole('img')).not.toBeInTheDocument();
});
});
diff --git a/client/src/pages/UsagePage.jsx b/client/src/pages/UsagePage.jsx
index c077db12fc..b614180c46 100644
--- a/client/src/pages/UsagePage.jsx
+++ b/client/src/pages/UsagePage.jsx
@@ -659,7 +659,7 @@ function InternalUsageMetrics() {
<h2 className="text-lg font-semibold text-white">PortOS AI Usage</h2>
{/* Trailing 30 days across every instance — independent of the range below. */}
<RollingCostCard />
- <MonthlyUsageCard history={usage.monthlyHistory} savings={usage.subscriptionSavings} />
+ <MonthlyUsageCard history={usage.monthlyHistory} />
{reportControls}
<div className="bg-port-card border border-port-border rounded-xl p-3 sm:p-4 flex flex-col sm:flex-row sm:items-center sm:justify-between gap-3">
diff --git a/server/services/usage.js b/server/services/usage.js
index d6d79d9dfc..36edc4e2c6 100644
--- a/server/services/usage.js
+++ b/server/services/usage.js
@@ -1149,6 +1149,7 @@ function monthlyUsageHistory(providers) {
});
return {
month,
+ estimatedCost: totals.estimatedCost,
tokens: totals.tokensIn + totals.tokensOut + totals.cacheReadTokens + totals.cacheWriteTokens,
source: totals.source,
partial: today < end || firstDay > `${month}-01`,
diff --git a/server/services/usage.test.js b/server/services/usage.test.js
index 11aad13a31..5bdbd32926 100644
--- a/server/services/usage.test.js
+++ b/server/services/usage.test.js
@@ -79,13 +79,13 @@ describe('usage.js — summary', () => {
readJSONFile.mockResolvedValueOnce(makeUsage({
'2025-06-01': { byProvider: { example: { tokensIn: 10, tokensOut: 20, cacheReadTokens: 30, cacheWriteTokens: 40, source: 'measured' } } },
'2025-06-10': { tokens: 5 },
- }, { monthlyActivity: { '2025-04': { tokensIn: 100, tokens: 200 } } }));
+ }, { monthlyActivity: { '2025-04': { tokensIn: 1000000, tokens: 2000000 } } }));
await loadUsage();
const history = getUsageSummary({ from: '2025-06-10', to: '2025-06-11' }).monthlyHistory;
expect(history).toEqual([
- { month: '2025-04', tokens: 300, source: 'estimate', partial: false },
- { month: '2025-05', tokens: 0, source: 'estimate', partial: false },
- { month: '2025-06', tokens: 105, source: 'mixed', partial: true },
+ { month: '2025-04', estimatedCost: 33, tokens: 3000000, source: 'estimate', partial: false },
+ { month: '2025-05', estimatedCost: 0, tokens: 0, source: 'estimate', partial: false },
+ { month: '2025-06', estimatedCost: 0, tokens: 105, source: 'mixed', partial: true },
]);
});