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.
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
-- shlex.join shell-quotes each argument, so the command is passed to rtk as
-- one literal word however it is punctuated. Running it as a shell string
-- means an absent rtk makes the shell exit non-zero rather than raising, so
-- a non-zero exit -- including rtk not being on PATH -- leaves the command
-- untouched and this degrades gracefully when rtk isn't installed.
local result = process.run{ cmd = shlex.join{ 'rtk', 'rewrite', command } }
if not result.ok then return end
-- Empty output, or output equal to the input, means leave the command
-- untouched -- which also covers an already-rtk command.
local rewritten = result.stdout:trim()
if rewritten ~= '' and rewritten ~= command then
input.command = rewritten
invocation.input = input
end
end)
process.run above is from the standard process
module, and shlex.join from the
shlex 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):
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.