Skip to content

Commit 3a3796c

Browse files
committed
display usage stats
1 parent 445a34b commit 3a3796c

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

node/chat/chat.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
type ProviderMessageContent,
2121
type ProviderName,
2222
type StopReason,
23+
type Usage,
2324
} from "../providers/provider.ts";
2425
import { assertUnreachable } from "../utils/assertUnreachable.ts";
2526
import { DEFAULT_OPTIONS, type MagentaOptions } from "../options.ts";
@@ -34,6 +35,7 @@ export type ConversationState =
3435
| {
3536
state: "stopped";
3637
stopReason: StopReason;
38+
usage: Usage;
3739
};
3840

3941
export type Model = {
@@ -115,7 +117,11 @@ export function init({ nvim, lsp }: { nvim: Nvim; lsp: Lsp }) {
115117
lastUserMessageId: counter.last() as Message.MessageId,
116118
options: DEFAULT_OPTIONS,
117119
activeProvider: "anthropic",
118-
conversation: { state: "stopped", stopReason: "end_turn" },
120+
conversation: {
121+
state: "stopped",
122+
stopReason: "end_turn",
123+
usage: { inputTokens: 0, outputTokens: 0 },
124+
},
119125
messages: [],
120126
toolManager: toolManagerModel.initModel(),
121127
contextManager: contextManagerModel.initModel(),
@@ -509,6 +515,7 @@ ${msg.error.stack}`,
509515
conversation: {
510516
state: "stopped",
511517
stopReason: res?.stopReason || "end_turn",
518+
usage: res?.usage || { inputTokens: 0, outputTokens: 0 },
512519
},
513520
});
514521
}
@@ -549,7 +556,12 @@ ${msg.error.stack}`,
549556
) % MESSAGE_ANIMATION.length
550557
]
551558
}`
552-
: d`Stopped (${model.conversation.stopReason || ""})`
559+
: d`Stopped (${model.conversation.stopReason}) [input: ${model.conversation.usage.inputTokens.toString()}, output: ${model.conversation.usage.outputTokens.toString()}${
560+
model.conversation.usage.cacheHits !== undefined &&
561+
model.conversation.usage.cacheMisses !== undefined
562+
? d`, cache hits: ${model.conversation.usage.cacheHits.toString()}, cache misses: ${model.conversation.usage.cacheMisses.toString()}`
563+
: ""
564+
}]`
553565
}${
554566
model.conversation.state == "stopped" &&
555567
!contextManagerModel.isContextEmpty(model.contextManager)

0 commit comments

Comments
 (0)