Report observed PromptKit invocation usage
This commit is contained in:
@@ -129,7 +129,7 @@ func buildCommand[I, S any](suite Suite[I, S], aliases *[]commandkit.Declaration
|
||||
for _, issue := range suite.issues {
|
||||
command = commandkit.WithError(command, issue)
|
||||
}
|
||||
return command
|
||||
return commandkit.WithInvocationFeature(command, usageFeature())
|
||||
}
|
||||
|
||||
func newCatalogListCommand[I, S any](suite Suite[I, S], name string) commandkit.Command {
|
||||
|
||||
83
integrations/ckprompt/usage_report.go
Normal file
83
integrations/ckprompt/usage_report.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package ckprompt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"text/template"
|
||||
|
||||
commandkit "go.maximumdirect.net/commandkit"
|
||||
promptkit "go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
type usageInput struct{ ShowTokens bool }
|
||||
|
||||
type usageSummary struct {
|
||||
Prompts int
|
||||
RepairCalls uint64
|
||||
CoverageComplete bool
|
||||
Usage promptkit.ObservedTokenUsage
|
||||
Cost promptkit.ObservedCost
|
||||
CacheHitRatio *float64
|
||||
Smoke bool
|
||||
}
|
||||
|
||||
func usageFeature() commandkit.InvocationFeature {
|
||||
return commandkit.NewInvocationFeature(commandkit.InvocationFeatureSpec[usageInput]{
|
||||
Flags: []commandkit.Flag[usageInput]{commandkit.BoolFlag(commandkit.FlagMeta{Name: "show-tokens", Usage: "Show token usage and available cost information after execution."}, false, func(i *usageInput, v commandkit.Supplied[bool]) { i.ShowTokens = v.Value })},
|
||||
Start: func(inv commandkit.Invocation, input usageInput) (context.Context, func() commandkit.Report) {
|
||||
if !input.ShowTokens {
|
||||
return inv.Context, nil
|
||||
}
|
||||
group := new(promptkit.ExecutionGroup)
|
||||
ctx := promptkit.WithExecutionObserver(inv.Context, group.Observe)
|
||||
smoke := inv.CanonicalPath() == "promptkit test"
|
||||
return ctx, func() commandkit.Report { return usageReport(group.Snapshot(), smoke) }
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func usageReport(summary promptkit.ExecutionGroupSummary, smoke bool) commandkit.Report {
|
||||
value := usageSummary{Prompts: len(summary.Members), RepairCalls: summary.RepairCalls, CoverageComplete: summary.TerminalCoverageComplete, Usage: summary.Usage, Cost: summary.CostUSD, Smoke: smoke}
|
||||
if ratio, ok := summary.Usage.CacheHitRatio(); ok {
|
||||
value.CacheHitRatio = &ratio
|
||||
}
|
||||
return commandkit.Report{Value: value, TextTemplate: usageTextTemplate}
|
||||
}
|
||||
|
||||
func usageCount(count promptkit.ObservedTokenCount) string {
|
||||
if !count.Known {
|
||||
return "unknown"
|
||||
}
|
||||
if !count.Complete {
|
||||
return fmt.Sprintf("%d (incomplete)", count.Value)
|
||||
}
|
||||
return fmt.Sprint(count.Value)
|
||||
}
|
||||
func usageCost(cost promptkit.ObservedCost) string {
|
||||
if !cost.Known {
|
||||
return "unknown"
|
||||
}
|
||||
result := fmt.Sprintf("USD %.6g", cost.Value)
|
||||
if !cost.Complete {
|
||||
result += " (incomplete subtotal)"
|
||||
}
|
||||
if cost.Source != "" {
|
||||
result += fmt.Sprintf(" [%s]", cost.Source)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
var usageTextTemplate = template.Must(commandkit.ParseTextTemplate("observed usage", `Observed PromptKit execution usage
|
||||
Prompts: {{.Prompts}}
|
||||
{{if .RepairCalls}} Repair calls: {{.RepairCalls}}
|
||||
{{end}}Prompt tokens: {{count .Usage.PromptTokens}}
|
||||
Completion tokens: {{count .Usage.CompletionTokens}}
|
||||
Reasoning tokens: {{count .Usage.ReasoningTokens}}
|
||||
Total tokens: {{count .Usage.TotalTokens}}
|
||||
Cached tokens: {{count .Usage.CachedTokens}}
|
||||
Cache-write tokens: {{count .Usage.CacheWriteTokens}}
|
||||
{{with .CacheHitRatio}}Cache hit ratio: {{printf "%.1f" (percent .)}}%
|
||||
{{end}}Cost: {{cost .Cost}}
|
||||
Terminal coverage: {{if .CoverageComplete}}complete{{else}}incomplete{{end}}
|
||||
{{if .Smoke}}Smoke-test usage is reported separately in the command report.
|
||||
{{end}}`, template.FuncMap{"count": usageCount, "cost": usageCost, "percent": func(value *float64) float64 { return *value * 100 }}))
|
||||
47
integrations/ckprompt/usage_report_test.go
Normal file
47
integrations/ckprompt/usage_report_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package ckprompt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
commandkit "go.maximumdirect.net/commandkit"
|
||||
promptkit "go.maximumdirect.net/promptkit"
|
||||
)
|
||||
|
||||
func TestUsageSummaryPreservesNativeEvidence(t *testing.T) {
|
||||
count := func(n int, known, complete bool) promptkit.ObservedTokenCount {
|
||||
return promptkit.ObservedTokenCount{Value: n, Known: known, Complete: complete}
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
summary promptkit.ExecutionGroupSummary
|
||||
smoke bool
|
||||
want []string
|
||||
absent []string
|
||||
}{
|
||||
{name: "empty", summary: new(promptkit.ExecutionGroup).Snapshot(), want: []string{"Prompts: 0", "Prompt tokens: 0", "Cost: USD 0", "Terminal coverage: complete"}, absent: []string{"Repair calls:", "Cache hit ratio:", "[provider]"}},
|
||||
{name: "unknown", summary: promptkit.ExecutionGroupSummary{Members: []promptkit.ExecutionGroupMember{{RunID: "a"}}}, smoke: true, want: []string{"Prompts: 1", "Prompt tokens: unknown", "Cost: unknown", "Terminal coverage: incomplete", "Smoke-test usage is reported separately"}},
|
||||
{name: "known", summary: promptkit.ExecutionGroupSummary{Members: []promptkit.ExecutionGroupMember{{}, {}}, TerminalCoverageComplete: true, RepairCalls: 2, Usage: promptkit.ObservedTokenUsage{PromptTokens: count(8, true, true), CompletionTokens: count(9, true, false), ReasoningTokens: count(0, true, true), CachedTokens: count(1, true, true)}, CostUSD: promptkit.ObservedCost{Value: 0.012345678, Known: true, Source: promptkit.CostSource("mixed")}}, want: []string{"Prompts: 2", "Repair calls: 2", "Completion tokens: 9 (incomplete)", "Reasoning tokens: 0", "Cache hit ratio: 12.5%", "Cost: USD 0.0123457 (incomplete subtotal) [mixed]"}},
|
||||
{name: "configured zero", summary: promptkit.ExecutionGroupSummary{CostUSD: promptkit.ObservedCost{Known: true, Complete: true, Source: promptkit.CostSource("configured_zero")}}, want: []string{"Cost: USD 0 [configured_zero]"}},
|
||||
{name: "provider zero", summary: promptkit.ExecutionGroupSummary{CostUSD: promptkit.ObservedCost{Known: true, Complete: true, Source: promptkit.CostSource("provider")}}, want: []string{"Cost: USD 0 [provider]"}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
report := usageReport(tc.summary, tc.smoke)
|
||||
var out bytes.Buffer
|
||||
if err := commandkit.RenderReport(&out, report, commandkit.FormatText); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range tc.want {
|
||||
if !strings.Contains(out.String(), want) {
|
||||
t.Errorf("missing %q in %q", want, out.String())
|
||||
}
|
||||
}
|
||||
for _, absent := range tc.absent {
|
||||
if strings.Contains(out.String(), absent) {
|
||||
t.Errorf("unexpected %q in %q", absent, out.String())
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user