# Add Token Profile Diagnostics

`a732e9b` · peyton-alt · 3w ago · 3 files · +645 added/-0 removed

## Sessions

cdebea3708fd View transcript

## Changes

3

- cmd/entire/cli

- Mroot.go +1

- Atokens_profile.go +413

- Atokens_profile_test.go +231

```
83 unmodified lines

84
85
86
87
88
89
90

83 unmodified lines

// Noun groups (canonical homes for subcommands).
cmd.AddCommand(newSessionsCmd())        // 'session' (with 'sessions' as Cobra alias)
cmd.AddCommand(newCheckpointGroupCmd()) // 'checkpoint' / 'cp' / 'checkpoints'
cmd.AddCommand(newTokensGroupCmd())     // 'tokens'
cmd.AddCommand(newAgentGroupCmd())      // 'agent'
cmd.AddCommand(newAuthCmd())            // 'auth'
cmd.AddCommand(newDoctorCmd())          // 'doctor' (group: trace/logs/bundle)
```

Mcmd/entire/cli/root.go +1

```
package cli

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"io"

"github.com/entireio/cli/cmd/entire/cli/agent"
	"github.com/entireio/cli/cmd/entire/cli/checkpoint"
	"github.com/entireio/cli/cmd/entire/cli/checkpoint/id"
	"github.com/spf13/cobra"
)

type tokensProfileReport struct {
	Source                   string                        `json:"source"`
	CheckpointsAvailable     int                           `json:"checkpoints_available"`
	CheckpointsAnalyzed      int                           `json:"checkpoints_analyzed"`
	CheckpointsWithTokenData int                           `json:"checkpoints_with_token_data"`
	MissingTokenData         int                           `json:"missing_token_data"`
	MetadataReadWarnings     int                           `json:"metadata_read_warnings,omitempty"`
	Tokens                   *sessionTokensUsage           `json:"tokens,omitempty"`
	Signals                  []tokensProfileSignal         `json:"signals,omitempty"`
	Recommendations          []sessionTokensRecommendation `json:"recommendations,omitempty"`
	Limitations              []string                      `json:"limitations,omitempty"`
}

type tokensProfileSignal struct {
	ID            string   `json:"id"`
	Label         string   `json:"label"`
	Count         int      `json:"count"`
	Percent       int      `json:"percent"`
	CheckpointIDs []string `json:"checkpoint_ids,omitempty"`
}

// Function to create a new tokens group command
func newTokensGroupCmd() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "tokens",
		Short: "Analyze token usage across sessions and checkpoints",
		Long:  `Analyze token usage across sessions and checkpoints. \nCommands: profile  Aggregate token usage across committed checkpoints \nExamples: entire tokens profile entire tokens profile --json`,
		RunE: func(cmd *cobra.Command, _ []string) error {
			return cmd.Help()
		},
	}

cmd.AddCommand(newTokensProfileCmd())
	return cmd
}

// Implementation of newTokensProfileCmd function
func newTokensProfileCmd() *cobra.Command {
	var jsonFlag bool
	var limitFlag int
	var allFlag bool

cmd := &cobra.Command{
		Use:   "profile",
		Short: "Aggregate token usage and recommendations across checkpoint history",
		Long:  `...`,
		Args: cobra.NoArgs,
		RunE: func(cmd *cobra.Command, _ []string) error {
			limit := limitFlag
			if allFlag {
				limit = 0
			} else if limit <= 0 {
				return errors.New("--limit must be positive unless --all is used")
			}
			return runTokensProfile(cmd.Context(), cmd, jsonFlag, limit)
		},
	}

cmd.Flags().BoolVar(&jsonFlag, "json", false, "Output as JSON")
	cmd.Flags().IntVar(&limitFlag, "limit", 50, "Maximum committed checkpoints to analyze")
	cmd.Flags().BoolVar(&allFlag, "all", false, "Analyze all committed checkpoints")
	return cmd
}

// Function that implements token profiling
func runTokensProfile(ctx context.Context, cmd *cobra.Command, jsonOutput bool, limit int) error {
	repo, err := openRepository(ctx)
	if err != nil {
		cmd.SilenceUsage = true
		fmt.Fprintln(cmd.ErrOrStderr(), "Not a git repository.")
		return NewSilentError(err)
	}
	defer repo.Close()

store := checkpoint.NewCommittedReadStore(ctx, repo)
	...
}

// Additional methods and functions for reading and writing token profile data...

// Further implementation details to follow...
```

Acmd/entire/cli/tokens_profile.go +413

```
