cmd: add usage cmd to chat to see token consumption

Adding a `/usage` command to interactive cli chat sessions that displays the tokens used in the current sessions. This can be used alongside the models context window to understand when a context shift is going to happen.
This commit is contained in:
Bruce MacDonald
2026-01-27 17:14:25 -08:00
parent 7b62c41060
commit e6f5a982d3
2 changed files with 19 additions and 8 deletions

View File

@@ -1419,10 +1419,10 @@ func thinkingOutputClosingText(plainText bool) string {
return readline.ColorGrey + readline.ColorBold + text + readline.ColorDefault
}
func chat(cmd *cobra.Command, opts runOptions) (*api.Message, error) {
func chat(cmd *cobra.Command, opts runOptions) (*api.Message, *api.Metrics, error) {
client, err := api.ClientFromEnvironment()
if err != nil {
return nil, err
return nil, nil, err
}
p := progress.NewProgress(os.Stderr)
@@ -1515,7 +1515,7 @@ func chat(cmd *cobra.Command, opts runOptions) (*api.Message, error) {
if err := client.Chat(cancelCtx, req, fn); err != nil {
if errors.Is(err, context.Canceled) {
return nil, nil
return nil, nil, nil
}
// this error should ideally be wrapped properly by the client
@@ -1523,9 +1523,9 @@ func chat(cmd *cobra.Command, opts runOptions) (*api.Message, error) {
p.StopAndClear()
fmt.Println("An error occurred while processing your message. Please try again.")
fmt.Println()
return nil, nil
return nil, nil, nil
}
return nil, err
return nil, nil, err
}
if len(opts.Messages) > 0 {
@@ -1535,14 +1535,14 @@ func chat(cmd *cobra.Command, opts runOptions) (*api.Message, error) {
verbose, err := cmd.Flags().GetBool("verbose")
if err != nil {
return nil, err
return nil, nil, err
}
if verbose {
latest.Summary()
}
return &api.Message{Role: role, Thinking: thinkingContent.String(), Content: fullResponse.String()}, nil
return &api.Message{Role: role, Thinking: thinkingContent.String(), Content: fullResponse.String()}, &latest.Metrics, nil
}
func generate(cmd *cobra.Command, opts runOptions) error {