rtk-ai/rtk

feat: rtk context — Show Claude Code context token budget

Open

#486 aberto em 10 de mar. de 2026

Ver no GitHub
 (1 comment) (0 reactions) (0 assignees)Rust (2.914 forks)batch import
area:clieffort-mediumenhancementhelp wantedpriority:medium

Métricas do repositório

Stars
 (48.085 stars)
Métricas de merge de PR
 (Mesclagem média 11d 1h) (45 fundiu PRs em 30d)

Description

Use case

rtk context gives a clear picture of how many tokens my Claude Code setup costs per message, so I can decide if I need to slim down my CLAUDE.md or archive skills.

Scope MVP (v1)

Single command, zero flags. Scans and displays:

Component Source Method
CLAUDE.md (global) ~/.claude/CLAUDE.md Read file, estimate_tokens()
CLAUDE.md (project) ./CLAUDE.md Read file, estimate_tokens()
@imports detected Lines @file.md in CLAUDE.md List without resolving, show "not resolved (v1)"
MEMORY.md ~/.claude/projects/<encoded-cwd>/memory/MEMORY.md + global Read file, estimate_tokens()
Skills ~/.claude/skills/*/SKILL.md Read each SKILL.md, estimate_tokens() on real content
Commands ~/.claude/commands/**/*.md (recursive) Read each file, estimate_tokens()
Rules .claude/rules/*.md (project) Read each file, estimate_tokens()
MCP servers ~/.claude/settings.jsonmcpServers keys Count servers (not tools)

Target output

RTK Context Budget
--------------------------------------
Component                Tokens   Note
--------------------------------------
CLAUDE.md (global)        1,200
CLAUDE.md (project)         480
  @imports detected:
    @RTK.md                        not resolved (v1)
    @TONE.md                       not resolved (v1)
MEMORY.md (global)            0   not found
MEMORY.md (project)         187
Skills (47 loaded)        4,200
Commands (53 loaded)      2,150
Rules (1 file)              890
MCP servers (0)               0
--------------------------------------
TOTAL MEASURABLE          9,107
--------------------------------------
Note: System prompt + built-in tools add ~15K (not measurable by RTK)

Files to create/modify

File Action
src/context_cmd.rs Create (~300 lines)
src/main.rs Add mod context_cmd, Commands::Context, routing
scripts/test-all.sh Add smoke test assertion

Reuse existing

  • tracking::estimate_tokens() (pub, src/tracking.rs)
  • Table pattern from gain.rs (colored, TTY-aware, styled())
  • dirs::home_dir() to locate ~/.claude/
  • discover::provider::ClaudeProvider::encode_project_path() to find the project folder

Error handling

  • ~/.claude/ absent → "Claude Code not detected"
  • Missing file → skip, display 0 + "not found"
  • settings.json absent/malformed → display "unknown"

Tests

  • Inline #[cfg(test)] with tempdir
  • Test detect_imports() on @file.md patterns
  • Test missing component → 0 tokens
  • Snapshot test on output format

Verification

cargo fmt --all && cargo clippy --all-targets && cargo test
./target/debug/rtk context   # on real setup
rtk --help | grep context
hyperfine 'rtk context'      # target <100ms

Out of scope (v2, defer)

  • --format json
  • --project scope
  • Recursive @import resolution
  • --detailed per-file breakdown

Acceptance criteria

  • rtk context displays a table with all listed components
  • Each token count is based on real file content (no fixed estimates except MCP)
  • Missing file = graceful skip (no panic)
  • cargo test passes, clippy clean
  • CHANGELOG.md + README Module Responsibilities table updated

Guia do colaborador