MCPcopy
hub / github.com/coder/mux / recordUsage

Method recordUsage

src/node/services/sessionUsageService.ts:146–155  ·  view source on GitHub ↗

* Record usage from a completed stream. Accumulates with existing usage * AND updates lastRequest in a single atomic write. * Model should already be normalized via normalizeToCanonical().

(workspaceId: string, model: string, usage: ChatUsageDisplay)

Source from the content-addressed store, hash-verified

144 * Model should already be normalized via normalizeToCanonical().
145 */
146 async recordUsage(workspaceId: string, model: string, usage: ChatUsageDisplay): Promise<void> {
147 return this.fileLocks.withLock(workspaceId, async () => {
148 const current = await this.readFile(workspaceId);
149 const existing = current.byModel[model];
150 // CRITICAL: Accumulate, don't overwrite
151 current.byModel[model] = existing ? sumUsageHistory([existing, usage])! : usage;
152 current.lastRequest = { model, usage, timestamp: Date.now() };
153 await this.writeFile(workspaceId, current);
154 });
155 }
156
157 /**
158 * Persist derived token stats (consumer + file breakdown) as a cache.

Callers

nothing calls this directly

Calls 4

readFileMethod · 0.95
writeFileMethod · 0.95
sumUsageHistoryFunction · 0.90
withLockMethod · 0.80

Tested by

no test coverage detected