Back to all guides

Token Optimization for AI Coding Agents: A Practical Guide to RTK

How RTK reduces token consumption by 60-90% through shell output compression, and how to integrate it into your agentic workflow alongside caching and dependency management.

Published
May 29, 2026
Reading time
8 min read

If you have shipped apps, used Docker, or integrated with an AI API, you already know the cost model. Tokens are the metered resource. Every file read, every test run, every git diff feeds the meter. The surprise is not that agentic coding is expensive. The surprise is how much of that cost comes from output you will never read.

One engineer instrumented a two-hour Claude Code session on a Next.js 15 monorepo. The raw shell half consumed 1.42 million tokens. The RTK-filtered half consumed 287 thousand. Same model. Same engineer. Same task. A five-fold difference traced to a single variable: whether shell output was compressed before it hit the context window.

This post explains how RTK works, why it is the cleanest solution in the current landscape, and how to combine it with other strategies for a comprehensive token optimization stack.

The Problem: Verbose Output in an LLM World

Command-line tools were designed for human readability. A test runner prints every test name so you can scan for failures. A git status shows timestamps and permission bits because a human might need them. An AWS CLI response includes every field in the API schema because a human can ignore the irrelevant ones.

LLMs do not ignore. They tokenize everything. A two-hundred-line test output with three failures becomes two hundred separate items in the context window. The model must process all of them to find the signal.

This is not a prompt engineering problem. You cannot write a better system prompt to make ls -la output smaller. The waste happens after the prompt, in the tool call response. The fix belongs at the shell layer.

What RTK Does

RTK is a CLI proxy written in Rust. It intercepts shell command output, applies command-specific filters, and forwards a compact representation to the agent. The command runs identically on your machine. Only what the model sees changes.

The tool supports over one hundred commands across git, test runners, linters, build tools, Docker, Kubernetes, AWS, and package managers. Each filter is tuned for the command's output structure.

Four strategies drive the compression:

Smart filtering strips noise. Comments, whitespace, boilerplate headers, ANSI escape codes, and progress bars disappear. A docker ps table loses its ASCII borders and column headers. The data remains.

Grouping aggregates similar items. Instead of listing forty-seven component files individually, RTK emits components/ (47 .tsx files). The model knows the directory contents without reading every filename.

Truncation preserves the head and tail of long outputs while removing the redundant middle. A test run with two hundred passing tests and three failures shows only the failures. The passing tests become a count. The model gets exactly what it needs to act.

Deduplication collapses repeated log lines. Five identical warning lines become one with a count. A thousand-line log with five hundred repetitions shrinks to a handful of unique entries.

The result is not lossy compression in the traditional sense. RTK removes information the model does not need. When a command fails, the full raw output is preserved locally. The model can access it if the compact version is insufficient.

Architecture and Integration

RTK integrates with thirteen AI coding tools across three tiers.

Full hook integrations intercept commands transparently. Claude Code, Cursor, Gemini CLI, and VS Code Copilot use shell hooks that rewrite git status to rtk git status before execution. The agent never sees the prefix. It receives compact output as if the raw command had naturally produced it.

Plugin integrations use the agent's native extension API. OpenCode uses a TypeScript plugin that hooks tool.execute.before. Hermes uses a Python adapter that mutates terminal commands. Both delegate the rewrite decision to RTK's Rust core.

Rules file integrations inject instructions into the agent's prompt context. Cline, Windsurf, Codex, Kilo Code, and Google Antigravity receive guidance to prefer rtk <cmd> over raw commands. This tier relies on the model following instructions rather than automatic interception.

The graceful degradation is worth noting. If RTK is missing, the hook exits cleanly and the raw command runs. If a filter fails, the raw output passes through. The tool never blocks your workflow.

Benchmarks and Real-World Validation

The project publishes detailed benchmarks for a typical thirty-minute Claude Code session.

RTK token savings
RTK token savings · open-agency.io

 

Independent validation corroborates these figures. Esteban Estrada measured a 70% reduction across his Claude Code sessions. Pooya Golchian's instrumented session showed a 5x cost difference. A DEV Community post documented 130 million tokens saved across 15,720 commands, with 88.9% efficiency.

The savings are tool-agnostic. Web developers see them. So do embedded engineers running make, docker, and kubectl. The common factor is verbose CLI output, not the technology stack.

Installation and Configuration

Install via Homebrew:

bashShiki server render
brew install rtk

Or use the install script:

bashShiki server render
curl -fsSL https://raw.githubusercontent.com/rtk-ai/rtk/master/install.sh | sh

Initialize for your agent:

bashShiki server render
rtk init --global           # Claude Code, Copilot, Cursor
rtk init --global --gemini  # Gemini CLI
rtk init --global --opencode # OpenCode

Configuration lives in ~/.config/rtk/config.toml:

typescriptShiki server render
[hooks]
exclude_commands = ["curl", "playwright"]

[tee]
enabled = true
mode = "failures"

The tee system saves raw output on failure. You can disable it, set it to always save, or restrict it to failures only.

Beyond RTK: Building a Token Optimization Stack

RTK is the most impactful single change, but it is not the only one. A comprehensive strategy layers complementary techniques.

Prompt caching from Anthropic reduces cost for repeated prefix content such as system prompts and project conventions. It does not compress novel verbose output. RTK handles that. Together, small inputs hit cached prefixes while RTK keeps the per-turn output lean. That is the cheapest possible token economics.

Scoped sub-agents prevent context bloat at the architectural level. Spawning a sub-agent with a tight system prompt for a focused task, then discarding its context, avoids accumulating two million tokens in a main session. RTK optimizes what flows back from each tool call. Scoped agents optimize how many tool calls accumulate in the first place.

Dependency management with Devbox reduces failed command attempts. When your agent knows exactly which CLI tools are available, it stops trying to run utilities that are not installed. Fewer failed commands means fewer error outputs tokenized into the context window. The ticket author's observation that RTK pairs well with Devbox is spot on. Reproducible environments eliminate a whole category of token waste.

Monitoring and Continuous Improvement

RTK includes analytics to help you find missed opportunities.

bashShiki server render
rtk gain              # Summary stats
rtk gain --history    # Recent command history
rtk discover          # Find commands you're running raw

The discover command is particularly useful. It scans your session history and flags commands that bypassed RTK. One user found they were wasting twelve thousand tokens per session on docker logs alone. Fixing that was a single config change.

New tools enter workflows constantly. A team that adopts a new ORM or infrastructure CLI in the next quarter will see token usage spike if those commands are not wrapped. Checking rtk discover weekly keeps the optimization current.

Trade-offs and Limitations

RTK is not magic. It compresses output. It does not reduce the number of tool calls or the size of your prompts.

The hook only intercepts Bash tool calls. Claude Code's built-in Read, Grep, and Glob tools bypass it. For those workflows, use shell equivalents or explicit rtk read, rtk grep, and rtk find commands.

Compression percentages vary by project. A Python data science project with different command patterns may see different savings than a TypeScript monorepo. The benchmarks are directional, not guarantees.

When a command fails, RTK saves the full output locally. If you work with sensitive output, be aware that these tee files exist on disk. You can disable tee entirely if needed.

Decision Framework

Should you adopt RTK? Ask three questions.

Do you use an AI coding agent for more than occasional tasks? If yes, the savings compound quickly.

Are you hitting API rate limits or context window ceilings? If yes, RTK directly extends your capacity.

Do you run shell commands frequently in your workflow? If yes, the filter applies often. If your workflow is dominated by built-in agent tools with no shell equivalent, the impact is smaller.

If you answered yes to the first two, install RTK today. Run rtk gain after a week. If the savings number is large, you have found your biggest leak. If it is small, you have ruled out a major hypothesis and can move to prompt and architecture audits with confidence.

Final Word

Token optimization is not about shaving pennies. It is about making agentic coding economically sustainable at scale. A team running daily AI-assisted sessions can burn through subscription caps fast. The first audit should not be the model or the prompts. It should be what flows back from your tool calls.

RTK is the fastest way to find out how much of your spend is noise. It is also the fastest way to fix it.