usage/ctxsz: separate billed tokens from context size

- Rename Core.Usage() to Core.CtxSz() (estimated context size)
- New Core.Usage() returns billed token counts (in, out, requests)
- 9P: add ctxsz file, usage file now returns billed tokens
- /context command now shows context size summary
- Emacs mode-line reads ctxsz instead of usage
This commit is contained in:
Levi Neely 2026-04-13 10:45:45 +02:00
parent f8e0750996
commit 0429c590af
2 changed files with 21 additions and 3 deletions

View File

@ -429,7 +429,7 @@ func (s *agentCore) IsRunning() bool {
return s.currentAction.Load() != nil
}
func (s *agentCore) Usage() string {
func (s *agentCore) CtxSz() string {
if s.session == nil {
return "no active session"
}
@ -442,6 +442,14 @@ func (s *agentCore) Usage() string {
return fmt.Sprintf("%d / %d (%d%%)", estimated, ctxLen, pct)
}
func (s *agentCore) Usage() string {
if s.session == nil {
return "no active session"
}
return fmt.Sprintf("%d in, %d out, %d requests",
s.session.TotalInputTokens, s.session.TotalOutputTokens, s.session.TotalRequests)
}
func (s *agentCore) ListModels() string {
models := s.loopcfg.Backend.Models(context.Background())
return strings.Join(models, "\n")
@ -804,6 +812,13 @@ func (s *agentCore) handleCommand(ctx context.Context, input string, handler Eve
handler(infoEvent("no active session"))
return true
}
ctxLen := s.loopcfg.Backend.ContextLength(ctx)
if ctxLen <= 0 {
ctxLen = defaultContextLength
}
estimated := s.session.estimateTokens()
pct := estimated * 100 / ctxLen
handler(infoEvent(fmt.Sprintf("~%d / %d tokens (%d%%)", estimated, ctxLen, pct)))
handler(infoEvent(strings.TrimRight(s.session.contextDebug(), "\n")))
return true
@ -943,7 +958,7 @@ func (s *agentCore) handleCommand(ctx context.Context, input string, handler Eve
" /cwd [path] - show or change working directory",
" /queued [pop|clear] - manage queued prompts",
" /compact - summarize conversation and compact context",
" /context - show context window debug info",
" /context - show context size and message breakdown",
" /usage - show token usage and context percentage",
" /history - dump bounded message history",
" /clear - clear session",

View File

@ -67,7 +67,10 @@ type Core interface {
// ModelName returns the name of the active model.
ModelName() string
// Usage returns a one-line usage summary string.
// CtxSz returns the estimated context size as a one-line summary.
CtxSz() string
// Usage returns billed token counts as a one-line summary.
Usage() string
// ListModels returns available model names, one per line.