If you haven't seen it yet, I have been working on tokf, a command and hook combo that helps me reduce the amount of tokens injected into Claude Code when commands like cargo test are run. The initial post about it can be found here. The main benefit of doing this is increasing the amount of context available to Claude Code, which generally leads to better results.
Going deeper
I started by using the hooks to intercept the commands being run and modify them to be run by tokf instead, this works well when Claude Code simply tries to run a command like cargo test. A lot of the time, however, that simple command will be part of a Justfile or a Makefile. This means that being able to intercept just test doesn't let us filter the subcommands. We need to go deeper, we need to be able to intercept the commands that just tries to run, not only just itself.
Shell interception
Both make and just allow us to set up a shell which they can use to execute the commands that have been configured. This means we have a way to insert ourselves between the tool and the execution.
With a justfile as the example below:
# Run all checks
check: fmt-check lint test file-size
# Format code
fmt:
cargo fmt
# Check formatting
fmt-check:
cargo fmt -- --check
# Run unit tests (no database required)
test:
cargo test
A call to:
just test
Will end up calling cargo test internally. We already have a system for rewriting commands, so we do exactly that:
just test
# becomes
just --shell tokf --shell-arg -cu test
Now we are able to intercept the commands, but wait - tokf isn't a shell, is it?
The answer is no, it isn't and it doesn't try to be, it also doesn't need to be. We can just delegate work to the shell, but this does allow us to rewrite commands before having the shell execute them.
So when just test tries to invoke cargo test, we now have the ability to sub in tokf run cargo test, which will allow tokf to filter the content of the internal command.
There is a risk doing this, as we have implemented a workaround for a bug in Claude Code - if exit code != 0 then Claude Code duplicates any command output and doubles the context usage for no reason. Our solution was to make tokf always return a 0 exit code, but print out the actual exit code as text.
Commands that we run in processes like make then need to return the actual exit code, make relies on it. So we added the flag --no-mask-exit-code, which prevents the exit code from being changed.
Now we have a working interception that works for both make and just and would work with most of the tools that do a similar thing (like mise), just a rewrite away.
Getting hooked on hooks
Git hooks are a different beast, compared to make and just, we cannot just inject a shell to make it execute our wanted commands with. But it executes binaries at some point. How does the shell know where an executable is, anyway?
Enter the PATH variable. It defines the lookup for an executable in a hierarchical manner (PATH is delimited with :).
We can check the value by running:
echo $PATH
#/usr/bin:/bin:/usr/sbin:/sbin:/opt/podman/bin
This tells the shell to look for executables in order:
- /usr/bin
- /bin
- /usr/sbin
And so forth. This means that if we put our own path in front of the rest, we can define what the binaries actually are, essentially we use simple shims to redirect calls to tokf instead.
An example shim would be the following one for go:
#!/bin/sh
exec 'tokf' -c 'go' "$@"
This ends up just delegating the command to tokf as a shell (which is what we created before). Now we also have the ability to intercept git-hooks or any other command that runs subcommands by looking them up in the path.
Conclusion
We are now able to intercept commands in tooling that orchestrates work, no more massive content being created because of a hook failing, instead we now get clear and concise information, filtered to include the minimum required context.
I also learned a lot about how commands like make and just work under the hood during this work, I found solutions which I didn't think of before, like shimming the commands and intercepting them + delegating them to tokf instead.
If you are also looking to extend your Claude Code sessions and reduce the number of compactions, then try out tokf, all instructions available at tokf.net.
Top comments (0)