feat: add provider usage tracking

This commit is contained in:
Peter Steinberger
2026-01-07 11:42:41 +01:00
parent 4e14123edd
commit 9bf6684366
18 changed files with 1333 additions and 51 deletions

View File

@@ -38,6 +38,10 @@ import {
formatContextUsageShort,
formatTokenCount,
} from "../status.js";
import {
formatUsageSummaryLine,
loadProviderUsageSummary,
} from "../../infra/provider-usage.js";
import type { MsgContext } from "../templating.js";
import type {
ElevatedLevel,
@@ -383,6 +387,15 @@ export async function handleCommands(params: {
);
return { shouldContinue: false };
}
let usageLine: string | null = null;
try {
const usageSummary = await loadProviderUsageSummary({
timeoutMs: 3500,
});
usageLine = formatUsageSummaryLine(usageSummary, { now: Date.now() });
} catch {
usageLine = null;
}
const queueSettings = resolveQueueSettings({
cfg,
provider: command.provider,
@@ -421,6 +434,7 @@ export async function handleCommands(params: {
resolvedReasoning: resolvedReasoningLevel,
resolvedElevated: resolvedElevatedLevel,
modelAuth: resolveModelAuthLabel(provider, cfg),
usageLine: usageLine ?? undefined,
queue: {
mode: queueSettings.mode,
depth: queueDepth,

View File

@@ -95,6 +95,22 @@ describe("buildStatusMessage", () => {
);
});
it("inserts usage summary beneath context line", () => {
const text = buildStatusMessage({
agent: { model: "anthropic/claude-opus-4-5", contextTokens: 32_000 },
sessionEntry: { sessionId: "u1", updatedAt: 0, totalTokens: 1000 },
sessionKey: "agent:main:main",
sessionScope: "per-sender",
queue: { mode: "collect", depth: 0 },
usageLine: "📊 Usage: Claude 80% left (5h)",
});
const lines = text.split("\n");
const contextIndex = lines.findIndex((line) => line.startsWith("📚 "));
expect(contextIndex).toBeGreaterThan(-1);
expect(lines[contextIndex + 1]).toBe("📊 Usage: Claude 80% left (5h)");
});
it("prefers cached prompt tokens from the session log", async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "clawdbot-status-"));
const previousHome = process.env.HOME;

View File

@@ -50,6 +50,7 @@ type StatusArgs = {
resolvedReasoning?: ReasoningLevel;
resolvedElevated?: ElevatedLevel;
modelAuth?: string;
usageLine?: string;
queue?: QueueStatus;
includeTranscriptUsage?: boolean;
now?: number;
@@ -356,6 +357,7 @@ export function buildStatusMessage(args: StatusArgs): string {
versionLine,
modelLine,
`📚 ${contextLine}`,
args.usageLine,
`🧵 ${sessionLine}`,
`⚙️ ${optionsLine}`,
activationLine,