Skip to main content

Python command planning, execution, and output filtering for PYTK-AI

Project description

PYTK-AI

A lightweight, dependency-free Python library and CLI that executes shell commands and filters their output into compact, token-efficient results for LLM consumption.

PYTK-AI reduces token usage by 25-50% across common development commands through smart filtering, deduplication, and noise removal -- helping AI coding agents work faster and cheaper.

Features

  • Smart output filtering -- 55+ filters for git, build tools, package managers, infrastructure commands, and more
  • Zero dependencies -- standard library only, installs anywhere Python 3.10+ runs
  • Fail-safe design -- if a filter fails, raw output passes through unchanged
  • Exit code preservation -- underlying command exit codes propagate correctly
  • Compound command support -- handles &&, ||, ;, &, and simple pipes
  • Embeddable -- small enough to drop into agent toolchains, hooks, and scripts

Installation

From PyPI

pip install pytk-ai

Or with uv:

uv tool install pytk-ai

From source

git clone https://github.com/nasirus/pytk-ai.git
cd pytk-ai
pip install -e .

CLI Usage

pytk-ai run git status
pytk-ai run "cargo test && git push"
pytk-ai run "docker ps"

Library Usage

from pytk_ai.runner import run_command

result = run_command("git status")
print(result.filtered_output)
print(result.exit_code)

Supported Filters

Category Examples Avg Token Reduction
Git git status, git log, git diff, git pull 40%
Build cargo build, tsc, next build 19%
Files find, wc, rg 18%
Go golangci-lint, go test 26%
Infrastructure docker ps, kubectl, aws, docker logs 46%
Packages npm list, pip list, pnpm list, bundle install 47%
Python pytest, mypy, ruff 24%
Ruby rubocop, rspec 30%
Tests cargo test, pytest 36%
Generic ANSI stripping, progress bars, line collapse 52%

How It Works

Command input
    |
    v
Plan/Normalize --> Execute subprocess --> Apply filter --> Structured result
                                              |
                                    (smart filtering, grouping,
                                     deduplication, truncation)
  1. Planning -- determines if the command benefits from PYTK-AI filtering
  2. Execution -- runs the command via subprocess with stdout/stderr capture
  3. Filtering -- applies domain-specific filters to compress the output
  4. Result -- returns a CommandResult with filtered output, exit code, and metadata

Filter Benchmark Results

By Category

Category Scenarios Avg Raw Tokens Avg Filtered Avg Reduction
build 8 155 138 18.6%
files 6 272 192 18.4%
generic 2 8 4 51.8%
git 8 44 25 40.5%
go 2 438 244 25.6%
infra 11 68 35 46.4%
packages 8 57 26 46.9%
python 3 147 108 24.2%
ruby 2 158 130 30.4%
system 3 646 646 0.0%
tests 2 40 26 35.8%
Total 55 24.5%

Top Filters by Token Reduction

Filter Command Raw Filtered Saved Reduction
git.pull git pull 28 4 24 85.7%
kubectl.pods kubectl get pods -A -o json 96 21 75 78.1%
git.log git log -2 65 18 47 72.3%
aws.read aws ec2 describe-instances --output json 74 21 53 71.6%
python.pytest pytest -q 165 55 110 66.7%
git.diff git diff 41 14 27 65.9%
bundle.install bundle install 55 19 36 65.5%
git.status git status 11 4 7 63.6%
kubectl.services kubectl get services 56 22 34 60.7%
docker.ps docker ps 76 30 46 60.5%
docker.images docker images 45 18 27 60.0%
uv.sync uv sync 15 6 9 60.0%
docker.compose.ps docker compose ps 67 27 40 59.7%
npm.list npm list 215 87 128 59.5%
pnpm.list pnpm list --json 35 15 20 57.1%

License

MIT License

Acknowledgements

PYTK-AI is a Python port of RTK (Rust Token Killer), a high-performance Rust CLI proxy that reduces LLM token consumption by 60-90%. The filtering strategies, command coverage, and architectural patterns in this project are derived from RTK's design.

Citation

If you use PYTK-AI or RTK in your research or tooling, please cite the original project:

@software{rtk2024,
  title     = {RTK: Rust Token Killer},
  author    = {RTK AI},
  url       = {https://github.com/rtk-ai/rtk},
  year      = {2024},
  note      = {High-performance CLI proxy that reduces LLM token consumption by 60-90\%}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytk_ai-0.1.3.tar.gz (60.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytk_ai-0.1.3-py3-none-any.whl (53.3 kB view details)

Uploaded Python 3

File details

Details for the file pytk_ai-0.1.3.tar.gz.

File metadata

  • Download URL: pytk_ai-0.1.3.tar.gz
  • Upload date:
  • Size: 60.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for pytk_ai-0.1.3.tar.gz
Algorithm Hash digest
SHA256 ba3f6814624ab2b9ef1e376200aeb86af766c044267cfe8e5e1a63368005130b
MD5 2706dce24f8680841f98321523b7fde4
BLAKE2b-256 7bacbe3abcbaf157d66043c5cfa3c1fdabcfd32babf341b0b001a4bf8260db79

See more details on using hashes here.

File details

Details for the file pytk_ai-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pytk_ai-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 53.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for pytk_ai-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 b9af9b480cfb7b4e6523b771a29443832efeb5a0694e6aedb6f4da8c97864050
MD5 500862feae23eb4dad9e2d6bfac1d4a9
BLAKE2b-256 a576b392a750014dfd0c6d444f1d4edacef34aa35432cfe9b41a4827a2dbea22

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page