tokman

module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 19, 2026 License: MIT

README ΒΆ

TokMan 🌸

World's Most Advanced Token Reduction System β€” 14-layer research-based compression pipeline achieving 95-99% token reduction.

TokMan intercepts CLI commands, applies 14 research-backed compression layers, and tracks token savings. Built on 50+ papers from top institutions (Mila, Microsoft, Stanford, Berkeley, NeurIPS 2023-2025).

Compression Performance

Input Size Original Final Reduction
Small (100 lines) 982 tokens 44 tokens 95.5%
Medium (1000 lines) 9,737 tokens 52 tokens 99.5%
Large (5000 lines) 49,437 tokens 63 tokens 99.9%
Up to 2M tokens Full context support Streaming processing Memory optimized

Features

  • 🧠 14-Layer Compression Pipeline β€” Research-based token reduction (95-99%)
  • πŸ”§ Git Command Wrappers β€” Filtered status, diff, log, add, commit, push, pull, and more
  • πŸ“ LS Handler β€” Hide noise directories (.git, node_modules, target, etc.)
  • 🐳 Infrastructure Wrappers β€” Docker, kubectl, AWS CLI with filtered output
  • πŸ“¦ Package Managers β€” npm, pnpm, pip, cargo with compact output
  • πŸ§ͺ Test Runners β€” Go, pytest, vitest, jest, npm test, playwright with aggregated results
  • πŸ”¨ Build Tools β€” Go, cargo, next.js with error-only output
  • πŸ“Š Token Tracking β€” SQLite-based metrics on tokens saved
  • πŸ”„ Shell Integration β€” Automatic command rewriting via shell hooks
  • πŸ” Integrity Verification β€” SHA-256 hook verification for security
  • πŸ’° Economics Analysis β€” Compare spending vs savings with quota estimates
  • πŸ’Ύ Tee on Failure β€” Auto-saves full output when commands fail for debugging

14-Layer Compression Pipeline

TokMan implements the world's most advanced token reduction system based on 50+ research papers:

Layer Name Research Compression
1 Entropy Filtering Selective Context (Mila 2023) 2-3x
2 Perplexity Pruning LLMLingua (Microsoft 2023) 20x
3 Goal-Driven Selection SWE-Pruner (Shanghai Jiao Tong 2025) 14.8x
4 AST Preservation LongCodeZip (NUS 2025) 4-8x
5 Contrastive Ranking LongLLMLingua (Microsoft 2024) 4-10x
6 N-gram Abbreviation CompactPrompt (2025) 2.5x
7 Evaluator Heads EHPC (Tsinghua/Huawei 2025) 5-7x
8 Gist Compression Stanford/Berkeley (2023) 20x+
9 Hierarchical Summary AutoCompressor (Princeton/MIT 2023) Extreme
10 Budget Enforcement Industry standard Guaranteed
11 Compaction MemGPT (UC Berkeley 2023) 98%+
12 Attribution Filter ProCut (LinkedIn 2025) 78%
13 H2O Filter Heavy-Hitter Oracle (NeurIPS 2023) 30x+
14 Attention Sink StreamingLLM (2023) Infinite stability

See docs/LAYERS.md for detailed documentation of each layer.

Installation

# Clone and build
git clone https://github.com/GrayCodeAI/tokman.git
cd tokman
go build -o tokman ./cmd/tokman

# Install to PATH (optional)
sudo mv tokman /usr/local/bin/
Docker
# Pull from GitHub Container Registry (once published)
docker pull ghcr.io/graycodeai/tokman:latest

# Or build locally
docker build -f docker/Dockerfile -t tokman:latest .

# Run with dashboard
docker run -d -p 8080:8080 -v tokman-data:/home/tokman/.local/share/tokman tokman:latest

# Run with docker-compose
cd docker && docker-compose up -d
Homebrew (macOS/Linux)
brew install GrayCodeAI/tap/tokman

Quick Start

# Initialize TokMan (install shell hook)
tokman init

# Check token savings
tokman status

# View comprehensive savings analysis
tokman gain

# Use wrapped commands
tokman git status
tokman ls
tokman test ./...

Commands

Core Commands
Command Description
tokman init Initialize TokMan and install shell hook
tokman status Quick token savings summary
tokman report Detailed usage analytics
tokman gain Comprehensive savings with graphs, history, quota
tokman config Show or create configuration file
tokman verify Verify hook integrity (SHA-256 check)
tokman economics Show spending vs savings analysis
Git Wrappers
Command Filter Applied
tokman git status Porcelain parsing, emoji formatting
tokman git diff Stats summary, compact hunks
tokman git log Oneline format, smart limits
tokman git add Compact "ok βœ“" output
tokman git commit Show hash on success
tokman git push Show branch on success
tokman git pull Show stats summary
tokman git branch Compact listing
tokman git stash Compact list/apply/drop
tokman git show Commit summary + compact diff
tokman git fetch Show new refs count
tokman git worktree Compact listing
Infrastructure Wrappers
Command Description
tokman docker Docker CLI with filtered output
tokman kubectl Kubernetes CLI with filtered output
tokman aws AWS CLI with filtered output
tokman gh GitHub CLI with token-optimized output
tokman gh run list GitHub Actions workflow runs (compact)
tokman gh release list GitHub releases (compact)
tokman gh api GitHub API with JSON structure output
tokman gt Graphite stacked PR commands
Test Runners
Command Description
tokman go test Go tests with aggregated results
tokman go build Go build with error-only output
tokman go vet Go vet with compact output
tokman cargo test Cargo tests with compact output
tokman cargo build Cargo build with error-only output
tokman cargo clippy Rust linter with compact output
tokman pytest Python tests with compact output
tokman ruff Python linter/formatter compact
tokman mypy Python type checker compact
tokman vitest Vitest with compact output
tokman jest Jest with compact output (90% reduction)
tokman npm test npm test with compact output (90% reduction)
tokman playwright Playwright E2E tests compact
Package Managers
Command Description
tokman npm npm run with filtered output
tokman pnpm pnpm with ultra-compact output
tokman npx npx with intelligent routing
tokman pip pip with compact output
tokman cargo Cargo commands with filtering
Utilities
Command Description
tokman ls Hide noise dirs, human sizes
tokman tree Compact tree output
tokman find Find files with compact output
tokman grep Compact grep, groups by file
tokman diff Ultra-condensed diff
tokman json Show JSON structure
tokman env Show env vars (sensitive masked)
tokman deps Summarize dependencies
tokman log Filter/deduplicate logs
tokman wc Word/line/byte count compact
tokman curl Auto-JSON detection
tokman wget Download with compact output
tokman summary Heuristic summary of long output
tokman count Count tokens using tiktoken (OpenAI tokenizer)
Analysis Commands
Command Description
tokman discover Find missed savings in Claude Code history
tokman learn Generate CLI correction rules from errors
tokman err <cmd> Run command, show only errors/warnings
tokman proxy <cmd> Run without filtering (still tracked)
tokman hook-audit Show hook rewrite metrics
Rewriting
Command Description
tokman rewrite <cmd> Rewrite a command to use TokMan
tokman rewrite list List all registered rewrites

Shell Integration

Token Counting (tiktoken)

TokMan includes direct token counting using OpenAI's tiktoken tokenizer:

# Count tokens in text
tokman count "Hello, world!"

# Count tokens from stdin
cat file.txt | tokman count

# Count tokens in a file
tokman count file.go

# Use specific model encoding
tokman count --model gpt-4o "Hello, world!"
tokman count --model claude-3-sonnet "Hello, world!"

# Compare heuristic vs actual count
tokman count --compare "Your text here"

# Count multiple files
tokman count --files *.go

Supported encodings:

  • cl100k_base β€” GPT-4, GPT-3.5-turbo, Claude, text-embedding-ada-002
  • o200k_base β€” GPT-4o, GPT-4o-mini
  • p50k_base β€” GPT-3 (davinci, curie, babbage, ada)

Add to your .bashrc or .zshrc:

source /path/to/tokman/hooks/tokman-rewrite.sh

This enables:

  • Automatic command rewriting for all supported commands
  • ts β€” alias for tokman status
  • tr β€” alias for tokman rewrite
  • tokman_install_hook β€” install hook to shell config
  • tokman_status β€” show integration status
Shell Completions

Enable autocompletions for your shell:

# Bash
source <(tokman completion bash)

# Zsh
source <(tokman completion zsh)

# Fish
tokman completion fish | source

Or save the completion files from completions/ directory.

Custom Filter Plugins

Create JSON-based filter plugins in ~/.config/tokman/plugins/:

{
  "name": "hide-npm-warnings",
  "description": "Hide npm deprecation warnings",
  "enabled": true,
  "patterns": ["npm WARN deprecated"],
  "mode": "hide"
}
# Plugin management
tokman plugin list           # List loaded plugins
tokman plugin create myfilter # Create new plugin template
tokman plugin enable myfilter # Enable a plugin
tokman plugin disable myfilter # Disable a plugin
tokman plugin examples       # Generate example plugins

Web Dashboard

Launch an interactive dashboard to visualize token savings:

# Start dashboard on default port (8080)
tokman dashboard

# Custom port
tokman dashboard --port 3000

# Open browser automatically
tokman dashboard --open

Features:

  • Real-time token savings charts
  • Daily/weekly/monthly breakdowns
  • Command-level analytics
  • Cost tracking with Claude API rates
  • RESTful API endpoints (/api/stats, /api/daily, /api/commands)

CI/CD Integration

Include TokMan in your CI pipelines for automated reporting:

GitHub Actions
# .github/workflows/tokman.yml
name: Token Savings Report
on: [workflow_run]

jobs:
  report:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-go@v5
        with: { go-version: '1.21' }
      - run: go install github.com/GrayCodeAI/tokman/cmd/tokman@latest
      - run: tokman summary --format markdown >> $GITHUB_STEP_SUMMARY

See templates/ for complete GitHub Actions and GitLab CI examples.

Configuration

Config file: ~/.config/tokman/config.toml

[tracking]
enabled = true
telemetry = false

[filter]
mode = "minimal"  # "minimal" or "aggressive"
noise_dirs = [
    ".git", "node_modules", "target",
    "__pycache__", ".venv", "vendor",
    ".idea", ".vscode", "dist", "build"
]

[hooks]
excluded_commands = []

Gain Command Features

The tokman gain command provides comprehensive savings analysis:

# Basic summary
tokman gain

# Show ASCII graph of daily savings
tokman gain --graph

# Show recent command history
tokman gain --history

# Show quota analysis (subscription tier estimate)
tokman gain --quota --tier pro

# Time breakdowns
tokman gain --daily
tokman gain --weekly
tokman gain --monthly
tokman gain --all

# Export data
tokman gain --format json
tokman gain --format csv

# Filter to current project
tokman gain --project

Security: Integrity Verification

TokMan protects against hook tampering:

# Verify hook integrity
tokman verify

# Runtime checks are automatic for operational commands
# Hooks are verified via SHA-256 hash stored during init

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                       TokMan CLI                            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Command Handlers (internal/commands/)                      β”‚
β”‚  β”œβ”€β”€ git.go       - Git wrappers                            β”‚
β”‚  β”œβ”€β”€ docker.go    - Docker filtering                        β”‚
β”‚  β”œβ”€β”€ go.go        - Go build/test                           β”‚
β”‚  β”œβ”€β”€ cargo.go     - Rust commands                           β”‚
β”‚  β”œβ”€β”€ npm.go       - npm/pnpm/npx                            β”‚
β”‚  β”œβ”€β”€ pytest.go    - Python tests                            β”‚
β”‚  β”œβ”€β”€ gain.go      - Savings analysis                        β”‚
β”‚  β”œβ”€β”€ economics.go - Cost analysis                           β”‚
β”‚  └── ...          - 40+ command handlers                    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  14-Layer Compression Pipeline (internal/filter/)           β”‚
β”‚  β”œβ”€β”€ Layer 1-9:   Research compression (95%+)               β”‚
β”‚  β”œβ”€β”€ Layer 10:    Budget enforcement                        β”‚
β”‚  β”œβ”€β”€ Layer 11:    Compaction (MemGPT-style)                 β”‚
β”‚  β”œβ”€β”€ Layer 12:    Attribution (ProCut-style)                β”‚
β”‚  β”œβ”€β”€ Layer 13:    H2O (Heavy-Hitter Oracle)                 β”‚
β”‚  └── Layer 14:    Attention Sink (StreamingLLM)             β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Core Engine (internal/)                                    β”‚
β”‚  β”œβ”€β”€ tracking/    - SQLite token tracking                   β”‚
β”‚  β”œβ”€β”€ config/      - TOML config loader                      β”‚
β”‚  β”œβ”€β”€ integrity/   - SHA-256 hook verification               β”‚
β”‚  └── economics/   - Cost analysis engine                    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Shell Integration (hooks/)                                 β”‚
β”‚  └── tokman-rewrite.sh - Bash/Zsh hook                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Development

# Run tests
go test ./...

# Build
go build -o tokman ./cmd/tokman

# Run with verbose output
./tokman -v git status

Token Estimation

TokMan uses a simple heuristic for token estimation:

tokens = ceil(text.length / 4.0)

This provides a reasonable approximation for tracking savings without requiring an actual tokenizer.

Data Storage

Following XDG Base Directory Specification:

Resource Path
Config ~/.config/tokman/config.toml
Database ~/.config/tokman/tracking.db
Logs ~/.config/tokman/tokman.log
Hook ~/.claude/hooks/tokman-rewrite.sh
Hook Hash ~/.claude/hooks/tokman-rewrite.sh.sha256

Override with environment variables:

  • XDG_CONFIG_HOME
  • TOKMAN_DATABASE_PATH

Roadmap

  • Git command wrappers
  • Token tracking database
  • Shell integration hooks
  • Integrity verification
  • Economics analysis
  • Python tool wrappers (pytest, ruff, mypy)
  • Rust/Cargo test aggregation
  • Windows support
  • Custom filter plugins
  • Web dashboard for analytics
  • Shell completions (bash/zsh/fish)
  • CI/CD integration templates
  • LLM API integration (direct token counting)
  • Homebrew formula
  • Docker image

License

MIT License - see LICENSE for details.


🌸 TokMan β€” Save tokens, save money, save the context window.

Directories ΒΆ

Path Synopsis
cmd
tokman command
internal
agents
Package agents provides integration with various AI coding agents.
Package agents provides integration with various AI coding agents.
alerts
Package alerts provides configurable alert management for TokMan.
Package alerts provides configurable alert management for TokMan.
ccusage
Package ccusage provides integration with the ccusage CLI for fetching Claude Code API usage metrics.
Package ccusage provides integration with the ccusage CLI for fetching Claude Code API usage metrics.
economics
Package economics provides Claude Code spending vs savings analysis.
Package economics provides Claude Code spending vs savings analysis.
integrity
Package integrity provides hook integrity verification via SHA-256.
Package integrity provides hook integrity verification via SHA-256.
llm
tee

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL