DEV Community

Michael Gantman
Michael Gantman

Posted on

Verify AI Token Cost Cuts with MgntUtils Stacktrace Filtering on your own data — Before You Touch Production

This article is for mid- and high-level managerial and technical decision makers. It is a follow-up to Cutting AI Token Costs with MgntUtils Stacktrace Filtering, a production case study of stacktrace filtering and AI token savings.

That case study presents measured AI token savings — and I am also the author of MgntUtils, which creates an obvious trust problem: why accept my numbers on someone else's system? You shouldn't have to. There is a way to get your own numbers from your own system. MgntUtils can filter stacktraces not only from a live Exception ("hot") but from a stacktrace already captured as text ("cold"). That cold path was built for log pipelines, remote/serialized errors, and post-processing — and it also lets you verify the cost-cutting claim on your own data before applying any changes to your system.

The idea is simple: put the MgntUtils jar on the classpath, write a tiny standalone Java program that uses MgntUtils cold filtering, filter stacktraces extracted from your own logs, and compare filtered vs original on lines, bytes, and tokens. You will see (1) whether filtering behaves as documented — the exact algorithm is described in Java Stacktrace filtering utility, so you can verify the kept and collapsed frames against that description — and (2) what your reduction range is — not mine.

This is the general idea.

Here are the implementation details of this idea. What follows is technical — but it is trivial enough that you can follow it even if hands-on coding is not your day-to-day work.

Implementation details

First, download the jar of the latest MgntUtils version available in the Release section of the GitHub page.

In your IDE, open a new workspace (or use an existing one) that is not connected to any of your system projects. In your project settings, add the MgntUtils jar to your classpath.

Now create a new class that may look like this:

package com.example.verify;

import com.mgnt.utils.TextUtils;

public class ColdStacktraceFilter {
    private static final String[] RELEVANT_PREFIXES = {"com.acme.", "org.acme."};

    public static void main(String[] args) {
        String stacktrace = getUnfilteredStacktrace();
        String filteredStacktrace = TextUtils.getStacktrace(stacktrace, RELEVANT_PREFIXES);
        writeFilteredStacktrace(filteredStacktrace);
    }

    private static String getUnfilteredStacktrace() {
        /*
         * Return the original unfiltered stacktrace here
         * It could be read from file or just paste it here and return it.
         */
        return "";
    }

    private static void writeFilteredStacktrace(String filteredStacktrace) {
        /*
         * Write your filtered stacktrace into a file or print it to console, or save it wherever
         * is convenient for you
         */
    }
}
Enter fullscreen mode Exit fullscreen mode

Change RELEVANT_PREFIXES to your company prefixes (or just one prefix, which is the case for most projects). Implement getUnfilteredStacktrace() and writeFilteredStacktrace() as appropriate — reading from and writing to local files is probably a good idea. Important: feed the filter the raw throwable stacktrace text (the block that starts with the exception type/message and includes the at ... / Caused by: frames) — not an arbitrary log line or a full JSON log event. If your logs are structured, extract that stacktrace body first. And that's it. You can run the class and get your filtered stacktraces. To estimate token savings, compare the original and filtered text by lines and bytes; for a cost decision, prefer pasting both into your model's tokenizer (or another exact counter). A rough proxy such as characters ÷ 4 can give a quick ballpark, but stacktraces often tokenize less efficiently than plain prose, so do not rely on that proxy alone.

Now you can analyze your own data and make the appropriate decision. Here are the links to the relevant technical articles:

In case you need any support, feel free to contact me at michael_gantman@yahoo.com or through a message on my LinkedIn page.

Top comments (0)