Skip to content

Reducing token usage with rtk

rtk is a CLI that rewrites common shell commands into token-optimized equivalents (for example git status becomes rtk git status, which emits a far more compact status). Because the config's tool_call hook can rewrite a tool's arguments before it runs, you can route the bash tool's commands through rtk transparently, so the model's own commands get the savings without it having to know about rtk.

Put this in a module alongside your config, e.g. ~/.config/wallah/rtk.lua:

-- rtk.lua -- rewrite bash tool commands through the `rtk` CLI to cut token use.
--
-- `rtk rewrite <cmd>` maps a raw command to its token-optimized rtk equivalent
-- on stdout, or prints nothing when there is no equivalent.

-- POSIX single-quote a string so the shell passes it as one argument.
local function shquote(s)
  return "'" .. s:gsub("'", "'\\''") .. "'"
end

-- Run a shell command and return its trimmed stdout ('' on any failure).
local function capture(cmd)
  local pipe = io.popen(cmd, 'r')
  if not pipe then return '' end
  local out = pipe:read('*a') or ''
  pipe:close()
  return out:trim()
end

agent.on('tool_call', function(invocation)
  if invocation.name ~= 'bash' then return end

  local input = invocation.input
  local command = input.command
  if not command or command == '' then return end

  -- Empty output, or output equal to the input, means leave the command
  -- untouched -- which also covers an already-rtk command and rtk being absent
  -- from PATH (so this degrades gracefully when rtk isn't installed).
  local rewritten = capture('rtk rewrite ' .. shquote(command) .. ' 2>/dev/null')
  if rewritten ~= '' and rewritten ~= command then
    input.command = rewritten
    invocation.input = input
  end
end)

io.popen above is the standard Lua io module.

Then enable it from config.lua by requiring the module (shingetsu resolves require against your config directory, so a sibling rtk.lua is found automatically):

require('rtk')

The rewrite is a no-op when rtk is not on PATH, so this is safe to leave in your config whether or not rtk is installed.