mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-21 21:03:56 +00:00
mom: add context usage to thread summary, update docs
- Usage summary now shows context tokens vs model context window - Updated CHANGELOG.md with all recent changes - Updated README.md with new file structure (log.jsonl/context.jsonl)
This commit is contained in:
parent
99fe4802ef
commit
71b776e290
4 changed files with 87 additions and 30 deletions
|
|
@ -617,9 +617,24 @@ function createRunner(sandboxConfig: SandboxConfig, channelId: string, channelDi
|
|||
}
|
||||
}
|
||||
|
||||
// Log usage summary
|
||||
// Log usage summary with context info
|
||||
if (runState.totalUsage.cost.total > 0) {
|
||||
const summary = log.logUsageSummary(runState.logCtx!, runState.totalUsage);
|
||||
// Get last non-aborted assistant message for context calculation
|
||||
const messages = session.messages;
|
||||
const lastAssistantMessage = messages
|
||||
.slice()
|
||||
.reverse()
|
||||
.find((m) => m.role === "assistant" && (m as any).stopReason !== "aborted") as any;
|
||||
|
||||
const contextTokens = lastAssistantMessage
|
||||
? lastAssistantMessage.usage.input +
|
||||
lastAssistantMessage.usage.output +
|
||||
lastAssistantMessage.usage.cacheRead +
|
||||
lastAssistantMessage.usage.cacheWrite
|
||||
: 0;
|
||||
const contextWindow = model.contextWindow || 200000;
|
||||
|
||||
const summary = log.logUsageSummary(runState.logCtx!, runState.totalUsage, contextTokens, contextWindow);
|
||||
runState.queue.enqueue(() => ctx.respondInThread(summary), "usage summary");
|
||||
await queueChain;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -195,13 +195,26 @@ export function logUsageSummary(
|
|||
cacheWrite: number;
|
||||
cost: { input: number; output: number; cacheRead: number; cacheWrite: number; total: number };
|
||||
},
|
||||
contextTokens?: number,
|
||||
contextWindow?: number,
|
||||
): string {
|
||||
const formatTokens = (count: number): string => {
|
||||
if (count < 1000) return count.toString();
|
||||
if (count < 10000) return (count / 1000).toFixed(1) + "k";
|
||||
if (count < 1000000) return Math.round(count / 1000) + "k";
|
||||
return (count / 1000000).toFixed(1) + "M";
|
||||
};
|
||||
|
||||
const lines: string[] = [];
|
||||
lines.push("*Usage Summary*");
|
||||
lines.push(`Tokens: ${usage.input.toLocaleString()} in, ${usage.output.toLocaleString()} out`);
|
||||
if (usage.cacheRead > 0 || usage.cacheWrite > 0) {
|
||||
lines.push(`Cache: ${usage.cacheRead.toLocaleString()} read, ${usage.cacheWrite.toLocaleString()} write`);
|
||||
}
|
||||
if (contextTokens && contextWindow) {
|
||||
const contextPercent = ((contextTokens / contextWindow) * 100).toFixed(1);
|
||||
lines.push(`Context: ${formatTokens(contextTokens)} / ${formatTokens(contextWindow)} (${contextPercent}%)`);
|
||||
}
|
||||
lines.push(
|
||||
`Cost: $${usage.cost.input.toFixed(4)} in, $${usage.cost.output.toFixed(4)} out` +
|
||||
(usage.cacheRead > 0 || usage.cacheWrite > 0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue