← Back to list

Cut Your LLM Bill Before It Cuts Your Budget: A Deep Dive into Token Optimization — Part 2, RTK…

As Large Language Models continue to evolve, organizations are increasingly facing challenges related to inference costs, latency, context…

Retheesh Ravi · 2026-06-08 02:59 · 0 claps · 31.8 min read paywalled
#rust-token-killer #llmlingua #cross-encoder-rerank #mem0 #tokenshrink
Open on Medium ↗
Wiki topics: LLM · Large Language Models OPS · LLMOps & Inference

Cut Your LLM Bill Before It Cuts Your Budget: A Deep Dive into Token Optimization — Part 2, RTK, Fine tuned cross encoders, TokenShrink, Mem0, LLMLingua

Image courtesy — Gemini

Image courtesy — Gemini

As Large Language Models continue to evolve, organizations are increasingly facing challenges related to inference costs, latency, context window limitations, and scalability. While model quality often receives the most attention, the efficiency of the data sent to and from the model can have an equally significant impact on both performance and cost. This article explores several complementary optimization techniques — including RTK, Cross Encoders, TokenShrink, Mem0, and LLMLingua — that address token consumption at different stages of the GenAI pipeline. Together, these approaches demonstrate that effective token optimization is not limited to prompt compression alone but spans retrieval, conversation management, tool interactions, and intelligent context reduction. Understanding where each technique fits enables architects and developers to build AI systems that are both cost-efficient and highly performant.

Q6) What is RUST token killer? How does RTK reduce token usage? What types of transformations does RTK perform? How does RTK integrate with Claude Code? Where does RTK sit in the Claude Code architecture?

Rust Token Killer (RTK) is a token optimization tool designed for AI coding assistants such as Claude Code. Instead of compressing user prompts, RTK reduces the size of command outputs and tool responses before they are sent to the LLM. This helps lower token consumption, reduce inference costs, improve response latency, and make better use of the model’s context window.

RTK sits between command execution and the LLM. When a command such as find, git diff, or pytest generates a large amount of output, RTK analyzes the results and removes redundant or low-value information while preserving the important details needed for reasoning. In testing, RTK reduced a find . command output from approximately 68.6K tokens to 174 tokens, achieving nearly 99.7% token savings.

RTK performs several output-compression techniques, including:

  • Directory summarization — Groups files and folders into concise summaries.
  • File extension aggregation — Replaces long file lists with extension counts and statistics.
  • Deduplication — Removes repetitive information and duplicate entries.
  • Noise reduction — Filters out low-value details that add tokens but little context.
  • Output truncation and sampling — Shows representative examples instead of every item.
  • Structure preservation — Retains the overall project, repository, or system structure so the LLM can still reason effectively.

The result is a much smaller representation of the original command output while maintaining the key information needed by the AI assistant.

Below simple experiment gives an idea of the savings it has done. As a pre-requesite , lets install the rtk. All of the setup instructions can be found here https://github.com/rtk-ai/rtk . Below instructions are when I ran on MacOS

brew install rtk

rtk --version
rtk 0.42.3

Now run a terminal command it on folder where there are many files. for e.g. like this below

find . 

find .
.
./docker-compose.yaml
./csv_data
./csv_data/wiki_movie.csv
./.DS_Store
./uploads
./data
./data/ibdata1
./data/aria_log_control
./data/mysqldb
./data/mysqldb/db.opt
./data/mysqldb/movie_staging.ibd
./data/mysqldb/movie_staging.frm
./data/ib_buffer_pool
./data/ib_logfile0
./data/multi-master.info
./data/mysql_upgrade_info
./data/aria_log.00000001
./data/sys
./data/sys/latest_file_io.frm
./data/sys/statements_with_temp_tables.frm
./data/sys/waits_by_user_by_latency.frm
./data/sys/user_summary_by_file_io.frm
./data/sys/x@0024memory_by_user_by_current_bytes.frm
./data/sys/schema_table_lock_waits.frm
./data/sys/x@0024memory_global_by_current_bytes.frm
./data/sys/user_summary_by_file_io_type.frm
./data/sys/io_global_by_file_by_bytes.frm
./data/sys/x@0024user_summary_by_file_io_type.frm
./data/sys/statement_analysis.frm
./data/sys/wait_classes_global_by_latency.frm
./data/sys/x@0024processlist.frm
./data/sys/x@0024io_global_by_file_by_bytes.frm
./data/sys/schema_auto_increment_columns.frm
./data/sys/x@0024memory_global_total.frm
./data/sys/x@0024schema_table_statistics_with_buffer.frm
./data/sys/waits_by_host_by_latency.frm
./data/sys/x@0024io_by_thread_by_latency.frm
./data/sys/processlist.frm

---- Close to 100 lines in between here

./data/sys/x@0024innodb_lock_waits.frm
./data/sys/x@0024memory_by_thread_by_current_bytes.frm
./data/sys/x@0024innodb_buffer_stats_by_schema.frm
./data/sys/ps_check_lost_instrumentation.frm
./data/sys/x@0024statements_with_sorting.frm
./data/sys/x@0024statements_with_temp_tables.frm
./data/sys/memory_by_thread_by_current_bytes.frm
./data/sys/x@0024statement_analysis.frm
./data/.my-healthcheck.cnf
./data/mysql
./data/mysql/index_stats.frm
./data/mysql/time_zone_transition.MAI
./data/mysql/index_stats.MAD
./data/mysql/time_zone_transition_type.MAD
./data/mysql/table_stats.frm
./data/mysql/columns_priv.MAD
./data/mysql/slow_log.frm
./data/mysql/columns_priv.frm
./data/mysql/time_zone_transition_type.frm
./data/mysql/table_stats.MAD
./data/mysql/tables_priv.frm
./data/mysql/help_category.MAI
./data/mysql/func.MAD
./data/mysql/column_stats.MAD
./data/mysql/plugin.MAI
./data/mysql/column_stats.frm
./data/mysql/func.frm
./data/mysql/innodb_index_stats.ibd
./data/mysql/innodb_index_stats.frm
./data/mysql/tables_priv.MAD
./data/mysql/help_relation.MAI
./data/mysql/slow_log.CSM

---- Close to 100 lines in between here 

./data/mysql/time_zone_name.frm
./data/mysql/general_log.frm
./data/mysql/time_zone_name.MAD
./data/mysql/proc.MAI
./data/mysql/servers.frm
./data/mysql/help_keyword.frm
./data/mysql/time_zone_leap_second.MAI
./data/mysql/event.MAD
./data/mysql/db.MAI
./data/mysql/help_topic.MAI
./data/mysql/procs_priv.MAI
./data/mysql/global_priv.MAI
./data/mysql/event.frm
./data/mysql/general_log.CSV
./data/mysql/general_log.CSM
./data/performance_schema
./data/performance_schema/db.opt
./data/Lab9Retheesh
./data/Lab9Retheesh/db.opt
./data/Lab9Retheesh/movie.frm
./data/Lab9Retheesh/movie.ibd

Now lets run it using rtk find .

 rtk find .
208F 8D:

./ docker-compose.yaml
csv_data/ wiki_movie.csv
data/ aria_log.00000001 aria_log_control ib_buffer_pool ib_logfile0 ibdata1 multi-master.info mysql_upgrade_info
data/Lab9Retheesh/ db.opt movie.frm movie.ibd
data/mysql/ column_stats.MAD column_stats.MAI column_stats.frm columns_priv.MAD columns_priv.MAI columns_priv.frm db.MAD db.MAI db.frm db.opt event.MAD event.MAI event.frm func.MAD func.MAI func.frm general_log.CSM general_log.CSV general_log.frm global_priv.MAD global_priv.MAI global_priv.frm gtid_slave_pos.frm gtid_slave_pos.ibd help_category.MAD help_category.MAI help_category.frm help_keyword.MAD help_keyword.MAI help_keyword.frm help_relation.MAD help_relation.MAI help_relation.frm help_topic.MAD help_topic.MAI help_topic.frm index_stats.MAD index_stats.MAI
+158 more

Lets run the rtk gain command

rtk gain
RTK Token Savings (Global Scope)
════════════════════════════════════════════════════════════

Total commands:    5
Input tokens:      70.4K
Output tokens:     189
Tokens saved:      70.2K (99.7%)
Total exec time:   133ms (avg 26ms)
Efficiency meter: ████████████████████████ 99.7%

[warn] No hook installed — run `rtk init -g` for automatic token savings

By Command
───────────────────────────────────────────────────────────────────────
  #  Command                   Count  Saved    Avg%    Time  Impact    
───────────────────────────────────────────────────────────────────────
 1.  rtk find                      3  70.1K   99.4%    18ms  ██████████
 2.  rtk git status                2    104   42.3%    39ms  ░░░░░░░░░░
───────────────────────────────────────────────────────────────────────

RTK reduces excessive token consumption caused by verbose command outputs such as:

  • find
  • grep
  • git diff
  • pytest
  • docker
  • kubectl
  • build logs
  • CI/CD output

Now, how can it integrate with a coding tools like claude code ? Below diagram explains it.

RTK integrates with Claude using PreToolUse Hooks

RTK integrates with Claude using PreToolUse Hooks

The hook can be added with the below steps

rtk init -g

RTK hook registered (global).

  Command:   rtk hook claude
  RTK.md:    /Users/retheesh/.claude/RTK.md (10 lines)
  CLAUDE.md: @RTK.md reference added

Patch existing /Users/retheesh/.claude/settings.json? [y/N] 
y

  settings.json: hook added
  Backup: /Users/retheesh/.claude/settings.json.bak
  Restart Claude Code. Test with: git status
  filters:   /Users/retheesh/Library/Application Support/rtk/filters.toml (template, edit to add user-global filters)
rtk hook claude --help
Process Claude Code PreToolUse hook (reads JSON from stdin)

Usage: rtk hook claude [OPTIONS]

Options:
  -v, --verbose...     Verbosity level (-v, -vv, -vvv)
      --ultra-compact  Ultra-compact mode: ASCII icons, inline format (Level 2 optimizations)
      --skip-env       Set SKIP_ENV_VALIDATION=1 for child processes (Next.js, tsc, lint, prisma)
  -h, --help           Print help

So thats it pretty much for Rust token killer. You can set this up and run rtk gain to get satisfied about the token savings!

Q7) How can Cross Encoders improve chunk selection accuracy in a RAG system? How can I fine-tune a Cross Encoder for command selection or retrieval reranking?

While tools such as RTK, TokenShrink, LLMLingua, and Mem0 focus on reducing the number of tokens sent to the LLM by compressing prompts, conversation history, or tool outputs, Cross Encoders tackle the problem from a completely different angle. A Cross Encoder does not compress text at all. Instead, it improves the quality of information retrieval by ensuring that only the most relevant documents, commands, chunks, or knowledge snippets are selected before the prompt is constructed. In other words, Cross Encoders reduce token usage indirectly by preventing irrelevant context from ever entering the prompt. This is particularly valuable in Retrieval-Augmented Generation (RAG), enterprise search, command execution systems, and agentic workflows where large numbers of candidate documents are retrieved.

from sentence_transformers import CrossEncoder
from sentence_transformers.cross_encoder import (
    CrossEncoderTrainer,
    CrossEncoderTrainingArguments
)

from datasets import Dataset
import pandas as pd
import random

COMMANDS = [
    {
        "command": "swifter a-run nginx",
        "description": "Start a container"
    },
    {
        "command": "swifter a-run -d nginx",
        "description": "Start a container in detached mode"
    },
    {
        "command": "swifter ps",
        "description": "List running containers"
    },
    {
        "command": "swifter ps -a",
        "description": "List all containers"
    },
    {
        "command": "swifter stop mycontainer",
        "description": "Stop a container"
    },
    {
        "command": "swifter rm mycontainer",
        "description": "Remove a container"
    },
    {
        "command": "swifter images",
        "description": "List images"
    },
    {
        "command": "swifter pull nginx",
        "description": "Download an image"
    },
    {
        "command": "swifter compose a-up -d",
        "description": "Start compose services"
    },
    {
        "command": "swifter compose a-down",
        "description": "Stop compose services"
    }
]

QUERY_TEMPLATES = {
    "Start a container": [
        "start a container",
        "run a container",
        "launch a container",
        "create and run container",
        "execute a container"
    ],

    "Start a container in detached mode": [
        "start container in background",
        "run detached container",
        "start detached mode container",
        "launch container in background",
        "run container without terminal"
    ],

    "List running containers": [
        "show running containers",
        "display active containers",
        "what containers are running",
        "list active containers",
        "show currently running containers"
    ],

    "List all containers": [
        "show all containers",
        "list every container",
        "display stopped containers too",
        "show complete container list",
        "list active and stopped containers"
    ],

    "Stop a container": [
        "stop a container",
        "terminate container",
        "shutdown container",
        "halt running container",
        "stop container execution"
    ],

    "Remove a container": [
        "delete container",
        "remove container",
        "cleanup container",
        "destroy container",
        "erase container"
    ],

    "List images": [
        "show images",
        "list images",
        "display local images",
        "what images exist",
        "show downloaded images"
    ],

    "Download an image": [
        "download nginx image",
        "pull image",
        "fetch docker image",
        "retrieve image",
        "download container image"
    ],

    "Start compose services": [
        "start compose stack",
        "bring up compose services",
        "launch compose application",
        "run compose deployment",
        "start all compose containers"
    ],

    "Stop compose services": [
        "shutdown compose stack",
        "stop compose services",
        "bring down compose application",
        "terminate compose deployment",
        "stop all compose containers"
    ]
}

training_data = []

def generate_fine_tuning_data(training_data):

    print(f"Generating training data for cross-encoder ...")
    for command in COMMANDS:
        positives = QUERY_TEMPLATES[command["description"]]
        # Positive pairs
        for q in positives:
            for _ in range(4):
                training_data.append({
                    "query": q,
                    "command": command["command"],
                    "label": 1
                })
        # Negative pairs
        other_commands = [
            c for c in COMMANDS
            if c["command"] != command["command"]
        ]
        for q in positives:
            for _ in range(2):
                neg = random.choice(other_commands)
                training_data.append({
                    "query": q,
                    "command": neg["command"],
                    "label": 0
                })
        print(len(training_data))

def finetune_cross_encoder(training_data):

    print("\n================================================")
    print("CROSS-ENCODER FINE-TUNING")
    print("================================================")

    #
    # Build dataframe
    #
    df = pd.DataFrame(training_data)

    #
    # Rename columns to what trainer expects
    #
    df = df.rename(
        columns={
            "query": "sentence1",
            "command": "sentence2"
        }
    )

    #
    # Convert labels to float
    #
    df["label"] = df["label"].astype(float)

    print("\nSample Training Records")
    print(df.head())

    #
    # Create HuggingFace Dataset
    #
    dataset = Dataset.from_pandas(df)

    print("\nDataset Sample")
    print(dataset[0])

    #
    # Load base cross encoder
    #
    model = CrossEncoder(
        "cross-encoder/ms-marco-MiniLM-L-6-v2",
        num_labels=1
    )

    #
    # Training arguments
    #
    training_args = CrossEncoderTrainingArguments(
        output_dir="./swifter-crossencoder",
        num_train_epochs=5,
        per_device_train_batch_size=16,
        learning_rate=2e-5,
        warmup_ratio=0.1,
        logging_steps=10,
        save_strategy="epoch",
        eval_strategy="no",
        fp16=False,
        report_to="none"
    )

    #
    # Trainer
    #
    trainer = CrossEncoderTrainer(
        model=model,
        args=training_args,
        train_dataset=dataset
    )

    #
    # Train
    #
    trainer.train()

    #
    # Save model
    #
    trainer.save_model(
        "./swifter-crossencoder"
    )

    print(
        "\nFine-tuned model saved to ./swifter-crossencoder"
    )

def compare_cross_encoder():

    print("\n================================================")
    print("CROSS-ENCODER INFERENCE")
    print("================================================")

    from sentence_transformers import CrossEncoder

    generic_model = CrossEncoder(
        "cross-encoder/ms-marco-MiniLM-L-6-v2"
    )

    custom_model = CrossEncoder(
        "./swifter-crossencoder"
    )

    query = "bring up all services in compose"
    candidates = [
        "swifter compose a-up -d",
        "swifter a-run nginx",
        "swifter ps",
        "swifter stop mycontainer",
        "swifter images"
    ]
    pairs = [
        (query, cmd)
        for cmd in candidates
    ]

    scores = generic_model.predict(pairs)
    print('===== General Model Scores =====')
    for cmd, score in sorted(
            zip(candidates, scores),
            key=lambda x: x[1],
            reverse=True):

        print(round(score, 2), cmd)

    scores = custom_model.predict(pairs)
    print('===== Custom Model Scores =====')
    for cmd, score in sorted(
            zip(candidates, scores),
            key=lambda x: x[1],
            reverse=True):

        print(round(score, 2), cmd)

generate_fine_tuning_data(training_data)
finetune_cross_encoder(training_data)
compare_cross_encoder()

Here is the execution result

Generating training data for cross-encoder ...
30
60
90
120
150
180
210
240
270
300

================================================
CROSS-ENCODER FINE-TUNING
================================================

Sample Training Records
           sentence1            sentence2  label
0  start a container  swifter a-run nginx    1.0
1  start a container  swifter a-run nginx    1.0
2  start a container  swifter a-run nginx    1.0
3  start a container  swifter a-run nginx    1.0
4    run a container  swifter a-run nginx    1.0

Dataset Sample
{'sentence1': 'start a container', 'sentence2': 'swifter a-run nginx', 'label': 1.0}
Loading weights: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 105/105 [00:00<00:00, 6595.41it/s]
BertForSequenceClassification LOAD REPORT from: cross-encoder/ms-marco-MiniLM-L-6-v2
Key                          | Status     |  | 
-----------------------------+------------+--+-
bert.embeddings.position_ids | UNEXPECTED |  | 

Notes:
- UNEXPECTED :can be ignored when loading from different task/architecture; not ok if you expect identical arch.
The `warmup_ratio` argument is deprecated in Transformers v5+, and will also be removed from Sentence Transformers once support for Transformers v4 is dropped. Since you're using Transformers v5+, please use `warmup_steps` (as a float) to specify the warmup ratio instead.
  0%|                                                                                                                                                                       | 0/95 [00:00<?, ?it/s]/Users/retheesh/Documents/RR/projects/Blogs/workspaces/blog/llm_caching/llmcachevenv/lib/python3.13/site-packages/torch/utils/data/dataloader.py:775: UserWarning: 'pin_memory' argument is set as true but not supported on MPS now, device pinned memory won't be used.
  super().__init__(loader)
{'loss': '5.544', 'grad_norm': '30.73', 'learning_rate': '1.8e-05', 'epoch': '0.5263'}                                                                                                             
Writing model shards: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00,  6.20it/s]
/Users/retheesh/Documents/RR/projects/Blogs/workspaces/blog/llm_caching/llmcachevenv/lib/python3.13/site-packages/torch/utils/data/dataloader.py:775: UserWarning: 'pin_memory' argument is set as true but not supported on MPS now, device pinned memory won't be used.
  super().__init__(loader)
{'loss': '1.686', 'grad_norm': '11.9', 'learning_rate': '1.788e-05', 'epoch': '1.053'}                                                                                                             
{'loss': '0.792', 'grad_norm': '13.39', 'learning_rate': '1.553e-05', 'epoch': '1.579'}                                                                                                            
Writing model shards: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00,  5.48it/s]
/Users/retheesh/Documents/RR/projects/Blogs/workspaces/blog/llm_caching/llmcachevenv/lib/python3.13/site-packages/torch/utils/data/dataloader.py:775: UserWarning: 'pin_memory' argument is set as true but not supported on MPS now, device pinned memory won't be used.
  super().__init__(loader)
{'loss': '0.4859', 'grad_norm': '8.583', 'learning_rate': '1.318e-05', 'epoch': '2.105'}                                                                                                           
{'loss': '0.4758', 'grad_norm': '13.14', 'learning_rate': '1.082e-05', 'epoch': '2.632'}                                                                                                           
Writing model shards: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00,  6.60it/s]
/Users/retheesh/Documents/RR/projects/Blogs/workspaces/blog/llm_caching/llmcachevenv/lib/python3.13/site-packages/torch/utils/data/dataloader.py:775: UserWarning: 'pin_memory' argument is set as true but not supported on MPS now, device pinned memory won't be used.
  super().__init__(loader)
{'loss': '0.3782', 'grad_norm': '6.503', 'learning_rate': '8.471e-06', 'epoch': '3.158'}                                                                                                           
{'loss': '0.3818', 'grad_norm': '6.455', 'learning_rate': '6.118e-06', 'epoch': '3.684'}                                                                                                           
Writing model shards: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00,  3.87it/s]
/Users/retheesh/Documents/RR/projects/Blogs/workspaces/blog/llm_caching/llmcachevenv/lib/python3.13/site-packages/torch/utils/data/dataloader.py:775: UserWarning: 'pin_memory' argument is set as true but not supported on MPS now, device pinned memory won't be used.
  super().__init__(loader)
{'loss': '0.4822', 'grad_norm': '4.579', 'learning_rate': '3.765e-06', 'epoch': '4.211'}                                                                                                           
{'loss': '0.344', 'grad_norm': '8.616', 'learning_rate': '1.412e-06', 'epoch': '4.737'}                                                                                                            
Writing model shards: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00,  6.29it/s]
{'train_runtime': '14.61', 'train_samples_per_second': '102.7', 'train_steps_per_second': '6.504', 'train_loss': '1.13', 'epoch': '5'}                                                             
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 95/95 [00:14<00:00,  6.50it/s]
Writing model shards: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:00<00:00,  7.90it/s]

Fine-tuned model saved to ./swifter-crossencoder

================================================
CROSS-ENCODER INFERENCE
================================================
Loading weights: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 105/105 [00:00<00:00, 5348.71it/s]
BertForSequenceClassification LOAD REPORT from: cross-encoder/ms-marco-MiniLM-L-6-v2
Key                          | Status     |  | 
-----------------------------+------------+--+-
bert.embeddings.position_ids | UNEXPECTED |  | 

Notes:
- UNEXPECTED :can be ignored when loading from different task/architecture; not ok if you expect identical arch.
Loading weights: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 105/105 [00:00<00:00, 15839.52it/s]
===== General Model Scores =====
-7.69 swifter compose a-up -d
-11.19 swifter ps
-11.34 swifter images
-11.4 swifter stop mycontainer
-11.41 swifter a-run nginx
===== Custom Model Scores =====
2.5 swifter compose a-up -d
-2.78 swifter ps
-4.31 swifter a-run nginx
-6.54 swifter images
-6.92 swifter stop mycontainer

Yes, this is actually a very good sign, and it’s exactly the kind of result you would want.

In the example above, the generic Cross Encoder model is fine-tuned on domain-specific command descriptions and user queries. Rather than sending all available commands or documents to the LLM, the fine-tuned model ranks candidates and selects only the most relevant ones. As a result, the final prompt becomes smaller, more focused, and more accurate. Unlike TokenShrink or LLMLingua, which operate after the prompt has been constructed, a Cross Encoder operates before prompt creation, acting as a precision filter. This often leads to significant token savings because irrelevant retrieval results are discarded entirely rather than compressed.

To summarize, Cross Encoders Reduce Token Usage by

  • Re-rank retrieved documents based on semantic relevance.
  • Remove irrelevant chunks before prompt construction.
  • Improve retrieval precision in RAG pipelines.
  • Reduce the number of documents sent to the LLM.
  • Improve answer quality by providing more focused context.
  • Reduce hallucinations caused by noisy retrieval results.
  • Enable smaller context windows even for large knowledge bases.
  • Improve command selection accuracy in command-execution systems.
  • Learn domain-specific terminology through fine-tuning.
  • Reduce both token costs and inference latency indirectly.

Q8) What is TokenShrink, How does TokenShrink help reduce LLM inference costs? Is TokenShrink a prompt optimization tool or a summarization tool? What types of prompts benefit the most from TokenShrink?

TokenShrink is a prompt compression tool designed to reduce unnecessary tokens before a prompt is sent to a Large Language Model (LLM). Rather than aggressively summarizing content or removing information, TokenShrink applies lightweight compression techniques such as phrase substitution, abbreviation encoding, redundancy elimination, and dictionary-based replacements. The primary goal is to preserve the original intent and semantic meaning of the prompt while reducing the number of tokens that need to be processed by the foundation model.

Lets try this out with the following code

import { compress } from 'tokenshrink';

const longPrompt = `
I would like your assistance with helping me better understand and evaluate a cloud architecture that my organization is currently considering for a future modernization initiative.
Before providing your answer, I would like you to carefully review all of the information that I am providing below, think deeply about the various architectural considerations involved, and then provide a detailed, comprehensive, thoughtful, and well-structured response.
It is extremely important that your response be detailed. I would prefer a detailed answer rather than a short answer. Please do not provide a brief answer. Please provide a comprehensive answer. Please make sure that all important considerations are included in your answer. If there are tradeoffs, please explain those tradeoffs. If there are risks, please explain those risks. If there are benefits, please explain those benefits.
Our organization is currently evaluating whether we should migrate a large number of enterprise applications from our on-premises datacenters to a public cloud environment. We have been discussing this topic internally for many months. Different stakeholders have different opinions. Some stakeholders believe that migration should happen as quickly as possible. Other stakeholders believe that migration should happen more gradually. Still others believe that some applications should remain on-premises.
When evaluating this migration, I would like you to consider scalability. I would also like you to consider reliability. In addition, please consider security. Furthermore, please consider operational efficiency. Also consider observability. In addition, consider governance. Please also consider compliance. Please also consider cost optimization. Please also consider performance optimization.
For scalability considerations, please discuss scalability in detail. Explain how applications can scale. Explain how databases can scale. Explain how APIs can scale. Explain how workloads can scale. Explain how cloud services can scale. Explain how scaling strategies differ across various workload types.
For security considerations, please discuss security in detail. Explain identity management. Explain access control. Explain encryption. Explain key management. Explain monitoring. Explain auditing. Explain governance. Explain security best practices. Explain common mistakes. Explain common risks.
For observability considerations, please discuss metrics. Please discuss logs. Please discuss traces. Please discuss dashboards. Please discuss alerting. Please discuss monitoring. Please discuss operational excellence. Please discuss troubleshooting. Please discuss incident response.
For cost optimization considerations, please discuss cost optimization in detail. Explain reserved capacity. Explain autoscaling. Explain rightsizing. Explain monitoring. Explain token optimization. Explain caching. Explain storage optimization. Explain network optimization. Explain operational efficiency improvements.
In addition to discussing cloud migration, I would also like you to discuss artificial intelligence adoption.
Our organization is interested in generative AI. We are interested in large language models. We are interested in foundation models. We are interested in retrieval augmented generation. We are interested in AI agents. We are interested in prompt engineering. We are interested in token optimization. We are interested in model observability. We are interested in model evaluation.
Please discuss these topics in detail.
Please explain prompt engineering in detail. Please explain prompt engineering best practices. Please explain common prompt engineering mistakes. Please explain prompt engineering techniques. Please explain prompt optimization strategies.
Please explain token optimization in detail. Please explain how token optimization reduces costs. Please explain how token optimization improves latency. Please explain how token optimization impacts scalability. Please explain how token optimization affects context windows.
Please explain semantic caching. Please explain response caching. Please explain prompt compression. Please explain context compression. Please explain retrieval optimization. Please explain model routing. Please explain model selection. Please explain inference optimization.
Please provide examples wherever possible.
Please provide recommendations wherever possible.
Please provide best practices wherever possible.
Please provide implementation guidance wherever possible.
Please provide tradeoff analysis wherever possible.
Please provide risk analysis wherever possible.
Please provide mitigation strategies wherever possible.
Please provide operational guidance wherever possible.
Please provide governance guidance wherever possible.
Please provide security guidance wherever possible.
Please provide architecture guidance wherever possible.
Please provide implementation guidance wherever possible.
Please provide migration guidance wherever possible.
Please provide detailed explanations rather than short explanations.
Please provide comprehensive explanations rather than brief explanations.
Please provide thorough explanations rather than summary explanations.
Please provide complete explanations rather than partial explanations.
Please provide detailed explanations rather than short explanations.
Please provide comprehensive explanations rather than brief explanations.
Please provide thorough explanations rather than summary explanations.
Please provide complete explanations rather than partial explanations.
Please provide detailed explanations rather than short explanations.
Please provide comprehensive explanations rather than brief explanations.
Please provide thorough explanations rather than summary explanations.
Please provide complete explanations rather than partial explanations.
Please provide detailed explanations rather than short explanations.
Please provide comprehensive explanations rather than brief explanations.
Please provide thorough explanations rather than summary explanations.
Please provide complete explanations rather than partial explanations.
`;

// Compress a prompt — runs locally, no API call needed
const result = compress(longPrompt);
console.log("Compressed Prompt:", result.compressed);
console.log("Tokens Saved:", result.stats.tokensSaved);       // Real token savings
console.log("Original Tokens:", result.stats.originalTokens);     // Original token count
console.log("Compressed Tokens:", result.stats.totalCompressedTokens); // Compressed token count

Now the output is like below

Compressed Prompt: [DECODE]
vs=rather than
full=comprehensive
P1="we are interested in"
[/DECODE]

I would like your assistance with helping me better understand and evaluate a cloud architecture that my organization is currently considering for a future modernization initiative.
Before providing your answer, I would like you to carefully review all of the information that I am providing below, think deeply about the various architectural considerations involved, and then provide a detailed, full, thoughtful, and well-structured response.
It is extremely important that your response be detailed. I would prefer a detailed answer vs a short answer. Please do not provide a brief answer. Please provide a full answer. Please ensure all important considerations are included in your answer. If there are tradeoffs, please explain those tradeoffs. If there are risks, please explain those risks. If there are benefits, please explain those benefits.
Our organization is currently evaluating whether we should migrate many enterprise applications from our on-premises datacenters to a public cloud environment. We have been discussing this topic internally for many months. Different stakeholders have different opinions. Some stakeholders believe that migration should happen as quickly as possible. Other stakeholders believe that migration should happen more gradually. Still others believe that some applications should remain on-premises.
When evaluating this migration, I would like you to consider scalability. I would also like you to consider reliability. also, please consider security. Furthermore, please consider operational efficiency. Also consider observability. also, consider governance. Please also consider compliance. Please also consider cost optimization. Please also consider performance optimization.
For scalability considerations, please discuss scalability in detail. Explain how applications can scale. Explain how databases can scale. Explain how APIs can scale. Explain how workloads can scale. Explain how cloud services can scale. Explain how scaling strategies differ across various workload types.
For security considerations, please discuss security in detail. Explain identity management. Explain access control. Explain encryption. Explain key management. Explain monitoring. Explain auditing. Explain governance. Explain security best practices. Explain common mistakes. Explain common risks.
For observability considerations, please discuss metrics. Please discuss logs. Please discuss traces. Please discuss dashboards. Please discuss alerting. Please discuss monitoring. Please discuss operational excellence. Please discuss troubleshooting. Please discuss incident response.
For cost optimization considerations, please discuss cost optimization in detail. Explain reserved capacity. Explain autoscaling. Explain rightsizing. Explain monitoring. Explain token optimization. Explain caching. Explain storage optimization. Explain network optimization. Explain operational efficiency improvements.
also discussing cloud migration, I would also like you to discuss artificial intelligence adoption.
Our organization is interested in generative AI. P1 large language models. P1 foundation models. P1 retrieval augmented generation. P1 AI agents. P1 prompt engineering. P1 token optimization. P1 model observability. P1 model evaluation.
Please discuss these topics in detail.
Please explain prompt engineering in detail. Please explain prompt engineering best practices. Please explain common prompt engineering mistakes. Please explain prompt engineering techniques. Please explain prompt optimization strategies.
Please explain token optimization in detail. Please explain how token optimization reduces costs. Please explain how token optimization improves latency. Please explain how token optimization impacts scalability. Please explain how token optimization affects context windows.
Please explain semantic caching. Please explain response caching. Please explain prompt compression. Please explain context compression. Please explain retrieval optimization. Please explain model routing. Please explain model selection. Please explain inference optimization.
Please provide examples wherever possible.
Please provide recommendations wherever possible.
Please provide best practices wherever possible.
Please provide implementation guidance wherever possible.
Please provide tradeoff analysis wherever possible.
Please provide risk analysis wherever possible.
Please provide mitigation strategies wherever possible.
Please provide operational guidance wherever possible.
Please provide governance guidance wherever possible.
Please provide security guidance wherever possible.
Please provide architecture guidance wherever possible.
Please provide implementation guidance wherever possible.
Please provide migration guidance wherever possible.
Please provide detailed explanations vs short explanations.
Please provide full explanations vs brief explanations.
Please provide thorough explanations vs summary explanations.
Please provide complete explanations vs partial explanations.
Please provide detailed explanations vs short explanations.
Please provide full explanations vs brief explanations.
Please provide thorough explanations vs summary explanations.
Please provide complete explanations vs partial explanations.
Please provide detailed explanations vs short explanations.
Please provide full explanations vs brief explanations.
Please provide thorough explanations vs summary explanations.
Please provide complete explanations vs partial explanations.
Please provide detailed explanations vs short explanations.
Please provide full explanations vs brief explanations.
Please provide thorough explanations vs summary explanations.
Please provide complete explanations vs partial explanations.
Tokens Saved: 74
Original Tokens: 1389
Compressed Tokens: 1315

Here is an analysis summary: TokenShrink reduced a 1,389-token prompt to 1,315 tokens, achieving a 5.3% reduction. Inspection of the compressed prompt reveals that the tool primarily uses dictionary-based compression techniques, including phrase substitution, abbreviation replacement, and repetition encoding. Rather than aggressively summarizing content, TokenShrink appears to preserve semantic meaning while eliminating repeated linguistic patterns. This makes it better suited for large conversational histories, agent traces, and verbose user instructions than for already concise technical prompts.

TokenShrink appears to be most effective on verbose natural-language prompts that contain repeated phrases, redundant instructions, conversational wording, and recurring patterns. It showed little or no benefit when applied to concise prompts, structured technical documentation, architecture descriptions, bullet-point lists, or information-dense content. In the successful test, TokenShrink identified recurring phrases such as “we are interested in” and replaced them with compact references, while also substituting verbose expressions with shorter equivalents. This suggests that TokenShrink is best suited for use cases such as long user instructions, chat histories, support transcripts, agent traces, and repetitive RAG contexts, where linguistic redundancy exists. For highly optimized prompts that already contain mostly unique information, the achievable token savings may be minimal.

At the time of writing, TokenShrink is primarily available as a Node.js SDK and can be integrated directly into JavaScript and TypeScript applications. It performs prompt compression locally without requiring an external API call, making it easy to incorporate into existing LLM pipelines as a lightweight token optimization layer.

Q9) What problem does Mem0 solve in long-running AI conversations? Why can Mem0 initially increase token usage before eventually providing significant token savings? How can token savings from Mem0 be measured and quantified during a conversation?

Lets take a look at the sample

import os
from typing import List, Dict, Optional
from langchain_openai import ChatOpenAI
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from mem0 import MemoryClient
from dotenv import load_dotenv
import tiktoken
from datetime import datetime

load_dotenv()

enc = tiktoken.encoding_for_model("gpt-4o")
def count_tokens(text):
    return len(enc.encode(text))

# Configuration
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY")
os.environ["MEM0_API_KEY"] = os.environ.get("MEM0_API_KEY")

# Initialize LangChain and Mem0
llm = ChatOpenAI(model="gpt-4o-mini")
mem0 = MemoryClient(api_key=os.environ["MEM0_API_KEY"])

# Store complete conversation history
complete_conversation_history = []

prompt = ChatPromptTemplate.from_messages([
    SystemMessage(content="""You are a helpful travel agent AI. Use the provided context to personalize your responses and remember user preferences and past interactions. 
    Provide travel recommendations, itinerary suggestions, and answer questions about destinations. 
    If you don't have specific information, you can make general suggestions based on common travel knowledge."""),
    MessagesPlaceholder(variable_name="context"),
    HumanMessage(content="{input}")
])

def retrieve_context(query: str, user_id: str) -> tuple[List[Dict], int, List[str]]:
    """Retrieve relevant context from Mem0"""
    try:
        memories = mem0.search(query, filters={"user_id": user_id})

        if isinstance(memories, dict) and 'results' in memories:
            memory_list = memories['results']
        else:
            memory_list = []

        memory_texts = []
        if memory_list and len(memory_list) > 0:
            for mem in memory_list:
                if isinstance(mem, dict):
                    memory_text = mem.get('memory') or mem.get('text') or mem.get('content')
                    if memory_text:
                        memory_texts.append(memory_text)

        if memory_texts:
            # Limit memory context to most relevant ones to avoid token explosion
            # Take top 3 most relevant memories for better efficiency
            relevant_memories = memory_texts[:3]
            serialized_memories = ' '.join(relevant_memories)
            memory_tokens = count_tokens(serialized_memories)

            context = [
                {
                    "role": "system", 
                    "content": f"Relevant information from past conversations: {serialized_memories}"
                },
                {
                    "role": "user",
                    "content": query
                }
            ]
        else:
            memory_tokens = 0
            memory_texts = []
            context = [{"role": "user", "content": query}]

        return context, memory_tokens, memory_texts
    except Exception as e:
        print(f"Error retrieving memories: {e}")
        return [{"role": "user", "content": query}], 0, []

def generate_response(input_text: str, context: List[Dict]) -> str:
    """Generate a response using the language model"""
    try:
        chain = prompt | llm
        response = chain.invoke({
            "context": context,
            "input": input_text
        })
        return response.content
    except Exception as e:
        print(f"Error generating response: {e}")
        return "I'm sorry, I encountered an error. Please try again."

def save_interaction(user_id: str, user_input: str, assistant_response: str):
    """Save the interaction to Mem0"""
    try:
        messages = [
            {"role": "user", "content": user_input},
            {"role": "assistant", "content": assistant_response}
        ]
        result = mem0.add(messages, user_id=user_id)
        print(f"✓ Memory saved successfully")
    except Exception as e:
        print(f"✗ Error saving interaction: {e}")

def calculate_token_efficiency(user_input: str, memory_tokens: int, memory_count: int):
    """Calculate and display accurate token efficiency metrics"""
    global complete_conversation_history

    # Calculate tokens for current conversation (short-term memory)
    current_conversation_tokens = 0
    if complete_conversation_history:
        # Get recent conversation (last 5 exchanges for context)
        recent_history = complete_conversation_history[-10:] if len(complete_conversation_history) > 10 else complete_conversation_history
        current_conversation_tokens = count_tokens("\n".join(recent_history))

    # Traditional approach: Send recent conversation history + current query
    traditional_tokens = current_conversation_tokens + count_tokens(user_input)

    # Mem0 approach: Send relevant past memories + current query
    mem0_tokens = memory_tokens + count_tokens(user_input)

    # Calculate efficiency
    if traditional_tokens > 0:
        if mem0_tokens < traditional_tokens:
            saved_tokens = traditional_tokens - mem0_tokens
            savings_percentage = (saved_tokens / traditional_tokens) * 100
            efficiency_status = "✅ BENEFICIAL"
        else:
            saved_tokens = mem0_tokens - traditional_tokens
            savings_percentage = ((mem0_tokens - traditional_tokens) / traditional_tokens) * 100
            efficiency_status = "⚠️  OVERHEAD"

        print("\n" + "="*70)
        print("TOKEN EFFICIENCY ANALYSIS")
        print("="*70)
        print(f"Conversation history length: {len(complete_conversation_history)} messages")
        print(f"Recent conversation tokens (last ~5 exchanges): {current_conversation_tokens:,}")
        print(f"Retrieved memories: {memory_count} items ({memory_tokens:,} tokens)")
        print(f"Current query tokens: {count_tokens(user_input):,}")
        print("-"*70)
        print(f"Traditional approach (recent history + query): {traditional_tokens:,} tokens")
        print(f"Mem0 approach (memories + query): {mem0_tokens:,} tokens")
        print(f"Efficiency: {efficiency_status}")

        if mem0_tokens < traditional_tokens:
            print(f"Tokens saved: {saved_tokens:,} ({savings_percentage:.1f}% reduction)")
        else:
            print(f"Extra tokens used: {saved_tokens:,} ({savings_percentage:.1f}% increase)")

            # Provide recommendation
            if len(complete_conversation_history) < 10:
                print("\nRECOMMENDATION: Mem0 is most efficient with longer conversations (10+ exchanges).")
                print("For short conversations, the overhead of retrieving memories may exceed benefits.")
            elif memory_count > 5:
                print("\nRECOMMENDATION: Consider limiting retrieved memories to top 3-5 most relevant ones.")

        print("="*70 + "\n")

        return {
            "traditional_tokens": traditional_tokens,
            "mem0_tokens": mem0_tokens,
            "saved_tokens": saved_tokens if mem0_tokens < traditional_tokens else -saved_tokens,
            "savings_percentage": savings_percentage if mem0_tokens < traditional_tokens else -savings_percentage,
            "efficient": mem0_tokens < traditional_tokens
        }
    else:
        print("\n" + "="*70)
        print("FIRST INTERACTION - BASELINE ESTABLISHED")
        print("="*70)
        print(f"❓ Current query tokens: {count_tokens(user_input):,}")
        if memory_tokens > 0:
            print(f"Retrieved memories: {memory_count} items ({memory_tokens:,} tokens)")
            print(f"Total context tokens: {memory_tokens + count_tokens(user_input):,}")
        print("="*70 + "\n")
        return None

def chat_turn(user_input: str, user_id: str) -> str:
    global complete_conversation_history

    # Retrieve context
    context, memory_tokens, memory_texts = retrieve_context(user_input, user_id)

    # Calculate and display token efficiency
    token_stats = calculate_token_efficiency(user_input, memory_tokens, len(memory_texts))

    # Generate response
    response = generate_response(user_input, context)

    # Update conversation history
    complete_conversation_history.append(f"User: {user_input}")
    complete_conversation_history.append(f"Assistant: {response[:200]}")  # Store preview

    # Limit history to last 50 messages for calculation
    if len(complete_conversation_history) > 50:
        complete_conversation_history = complete_conversation_history[-50:]

    # Save to Mem0
    save_interaction(user_id, user_input, response)

    return response

if __name__ == "__main__":
    print("="*70)
    print("SMART TRAVEL AGENT WITH MEM0 MEMORY")
    print("="*70)
    print("This AI remembers past conversations to provide personalized recommendations!")
    print("(Type 'quit', 'exit', or 'bye' to end)")
    print("-"*70)

    user_id = "alice"
    interaction_count = 0

    while True:
        user_input = input("\nYou: ").strip()
        if user_input.lower() in ['quit', 'exit', 'bye']:
            print("\nTravel Agent: Thank you for using our service. Safe travels! ✨\n")
            break

        if not user_input:
            continue

        interaction_count += 1
        print(f"\nConversation #{interaction_count}")

        response = chat_turn(user_input, user_id)
        print(f"\nTravel Agent: {response}")
        print("-"*70)

Now lets see the output from this

 llm_caching % python mem0_demo.py
======================================================================
SMART TRAVEL AGENT WITH MEM0 MEMORY
======================================================================
This AI remembers past conversations to provide personalized recommendations!
(Type 'quit', 'exit', or 'bye' to end)
----------------------------------------------------------------------

You: Hello Bike trip planning to be done 

Conversation #1

======================================================================
TOKEN EFFICIENCY ANALYSIS
======================================================================
Conversation history length: 0 messages
Recent conversation tokens (last ~5 exchanges): 0
Retrieved memories: 10 items (49 tokens)
Current query tokens: 7
----------------------------------------------------------------------
Traditional approach (recent history + query): 7 tokens
Mem0 approach (memories + query): 56 tokens
Efficiency: ⚠️  OVERHEAD
Extra tokens used: 49 (700.0% increase)

RECOMMENDATION: Mem0 is most efficient with longer conversations (10+ exchanges).
   For short conversations, the overhead of retrieving memories may exceed benefits.
======================================================================

Memory saved successfully

Travel Agent: Great! It sounds like you're ready to dive into planning your motorbike trip from Bangalore to Leh. Let's create a personalized itinerary for your Himalayas adventure on your Honda NX 200.

### Suggested Itinerary for Bangalore to Leh

**Day 1: Bangalore to Coimbatore (approx. 350 km)**
- Start early from Bangalore.
- Breakfast stop at a roadside diner.
- Reach Coimbatore by afternoon; explore the local cuisine for dinner.

**Day 2: Coimbatore to Madurai (approx. 200 km)**
- Enjoy the scenic ride; stop at scenic viewpoints.
- Visit the famous Meenakshi Temple in Madurai.
- Overnight in Madurai.

**Day 3: Madurai to Kanyakumari (approx. 240 km)**
- Ride towards the southern tip of India.
- Visit Cape Comorin and the Vivekananda Rock Memorial.
- Overnight stay in Kanyakumari.

**Day 4: Kanyakumari to Trivandrum (approx. 100 km)**
- A short ride to Trivandrum.
- Explore the Padmanabhaswamy Temple and local markets.
- Overnight in Trivandrum.

**Day 5: Trivandrum to Alleppey (approx. 160 km)**
- Enjoy a ride to the beautiful backwaters of Alleppey.
- Consider an overnight houseboat stay for a unique experience.

**Day 6: Alleppey to Kochi (approx. 50 km)**
- A relaxed ride to Kochi.
- Explore Fort Kochi, the Chinese fishing nets, and local art galleries.
- Overnight in Kochi.

**Day 7: Kochi to Munnar (approx. 130 km)**
- Ride to the picturesque hill station of Munnar.
- Visit tea plantations and enjoy the cooler climate.
- Overnight in Munnar.

**Day 8-9: Munnar to Leh (take a flight or arrange for a bike transport)**
- Given the distance and road conditions, consider flying to Leh and then starting your biking adventure from there.

**Days 10-15: Explore Leh and Surrounding Areas**
- Explore the beauty of Ladakh: Nubra Valley, Pangong Lake, and Khardung La Pass.
- Take some time for acclimatization and enjoy the local culture.

**Return Journey: Leh to Bangalore**
- Plan your return with stops similar to your outbound journey.

### Important Tips:
- Ensure your bike is thoroughly serviced before departure.
- Carry spare parts, tools, and a first aid kit.
- Stay hydrated and acclimatize when reaching high altitudes.
- Check weather and road conditions regularly.

How does this suggested itinerary sound? Would you like to customize any part of it or have questions about specific locations?
----------------------------------------------------------------------

You: Am moving from Bangalore to Leh Ladakh

Conversation #2

======================================================================
TOKEN EFFICIENCY ANALYSIS
======================================================================
Conversation history length: 2 messages
Recent conversation tokens (last ~5 exchanges): 52
Retrieved memories: 10 items (87 tokens)
Current query tokens: 8
----------------------------------------------------------------------
Traditional approach (recent history + query): 60 tokens
Mem0 approach (memories + query): 95 tokens
Efficiency: ⚠️  OVERHEAD
Extra tokens used: 35 (58.3% increase)

RECOMMENDATION: Mem0 is most efficient with longer conversations (10+ exchanges).
   For short conversations, the overhead of retrieving memories may exceed benefits.
======================================================================

Memory saved successfully

Travel Agent: That sounds like an exciting journey! Traveling from Bangalore to Leh Ladakh by motorbike will be an adventure of a lifetime. Given your plans, you might want to start with a well-structured itinerary to ensure a smooth ride. 

Here’s a rough suggestion for your trip:

### Suggested Itinerary for Bangalore to Leh Ladakh Motorbike Trip

**Day 1-2: Bangalore to Hyderabad**
- Distance: ~570 km
- Travel through scenic routes and enjoy a night in Hyderabad.

**Day 3: Hyderabad to Nagpur**
- Distance: ~500 km
- Enjoy local cuisines and perhaps visit some attractions.

**Day 4: Nagpur to Bhopal**
- Distance: ~450 km
- Explore the historical and cultural sites.

**Day 5: Bhopal to Delhi**
- Distance: ~700 km
- Consider breaking this leg if needed, possibly overnight in Agra.

**Day 6: Delhi to Manali**
- Distance: ~540 km
- Prepare yourself for the mountain journey ahead. You could spend a day exploring Manali.

**Day 7-10: Manali to Leh**
- **Day 7:** Manali to Jispa (About 140 km)
- **Day 8:** Jispa to Zingzing Bar and onward (About 100 km)
- **Day 9:** Zingzing Bar to Leh (About 200 km)
- Allocate time for acclimatization and exploring Leh.

**Day 11-14: Exploring Leh and Surroundings**
- Visit places like Pangong Lake, Nubra Valley, and Khardung La.
- Allow for some rest days as well.

**Final Days: Return Journey**
- You can choose to return via the same route or a different one, depending on how much time you have.

### Important Tips:
- Ensure your bike is in top condition before starting the trip.
- Carry enough fuel, especially in remote areas like Ladakh.
- Keep warm clothing, as temperatures can drop, especially at high altitudes.
- Stay hydrated and adjust to altitude changes gradually.

Would you like more information on specific routes or places to visit along the journey?
----------------------------------------------------------------------

You: Am planning for 5 days trip 

Conversation #3

======================================================================
TOKEN EFFICIENCY ANALYSIS
======================================================================
Conversation history length: 4 messages
Recent conversation tokens (last ~5 exchanges): 106
Retrieved memories: 10 items (82 tokens)
Current query tokens: 7
----------------------------------------------------------------------
Traditional approach (recent history + query): 113 tokens
Mem0 approach (memories + query): 89 tokens
Efficiency: ✅ BENEFICIAL
Tokens saved: 24 (21.2% reduction)
======================================================================

Memory saved successfully

Travel Agent: For a 5-day trip from Bangalore to Leh on your Honda NX 200, you'll need to plan your route carefully to cover a significant distance. Here’s a suggested itinerary that balances riding time with some sightseeing:

### Day 1: Bangalore to Belagavi (Bangalore to Kolhapur)
- **Distance**: ~500 km
- **Route**: Via NH48
- **Highlights**: Explore the ancient temples and enjoy local cuisine in Belagavi.

### Day 2: Belagavi to Pune
- **Distance**: ~230 km
- **Route**: Continue on NH48
- **Highlights**: Visit the famous Aga Khan Palace and enjoy some time at the Osho Ashram in Pune.

### Day 3: Pune to Udaipur
- **Distance**: ~730 km (This will be a long ride)
- **Route**: NH48 and NH62
- **Highlights**: Udaipur's beautiful lakes and palaces. Relax and explore the local markets.

### Day 4: Udaipur to Jaisalmer
- **Distance**: ~780 km
- **Route**: Via NH62
- **Highlights**: This will be another long ride. Once you arrive, explore the Jaisalmer Fort and enjoy a desert sunset.

### Day 5: Jaisalmer to Leh 
- **Distance**: Over 1,000 km (consider splitting this into two parts)
- **Possible Stops**: Depending on your pace, you might need to stop for a night at places like Jodpur or Bikaner before heading to Leh.

### Notes:
- **Accommodation**: It's best to book accommodation in advance, especially in popular tourist destinations.
- **Weather and Road Conditions**: Keep an eye on weather conditions and road updates, as the routes can change.
- **Safety Gear**: Ensure you have proper riding gear for comfort and safety.

If you have any specific places you'd like to see or if you need more details on any segment, feel free to ask!
----------------------------------------------------------------------

Q10) What is LLMLingua and how does it differ from traditional prompt compression techniques? When should LLMLingua be preferred over other token optimization techniques? What is LLMLingua and how does it differ from traditional prompt compression techniques?

LLMLingua is a prompt compression framework developed by researchers at Microsoft Research to reduce LLM inference costs and latency by intelligently removing low-value tokens from prompts while preserving their semantic meaning. Unlike traditional prompt optimization tools such as TokenShrink, which rely on rule-based phrase substitutions, abbreviations, or dictionary encoding, LLMLingua uses a smaller language model to estimate token importance and selectively prune less important content. This semantic compression approach enables significantly higher compression ratios — often 30% to 70% token reduction — while maintaining the quality of responses generated by the target LLM.

Lets take a look at a sample code optimizing the same prompt that we gave to the TokenShrink

from llmlingua import PromptCompressor
import tiktoken

# ----------------------------------
# Token Counter
# ----------------------------------

enc = tiktoken.encoding_for_model("gpt-4o")

def count_tokens(text):
    return len(enc.encode(text))

# ----------------------------------
# Initialize LLMLingua
# ----------------------------------

compressor = PromptCompressor(
    model_name="microsoft/llmlingua-2-xlm-roberta-large-meetingbank",
    use_llmlingua2=True,
    device_map="cpu"
)

# ----------------------------------
# Long Prompt
# ----------------------------------

prompt = """
I would like your assistance with helping me better understand and evaluate a cloud architecture that my organization is currently considering for a future modernization initiative.
Before providing your answer, I would like you to carefully review all of the information that I am providing below, think deeply about the various architectural considerations involved, and then provide a detailed, comprehensive, thoughtful, and well-structured response.
It is extremely important that your response be detailed. I would prefer a detailed answer rather than a short answer. Please do not provide a brief answer. Please provide a comprehensive answer. Please make sure that all important considerations are included in your answer. If there are tradeoffs, please explain those tradeoffs. If there are risks, please explain those risks. If there are benefits, please explain those benefits.
Our organization is currently evaluating whether we should migrate a large number of enterprise applications from our on-premises datacenters to a public cloud environment. We have been discussing this topic internally for many months. Different stakeholders have different opinions. Some stakeholders believe that migration should happen as quickly as possible. Other stakeholders believe that migration should happen more gradually. Still others believe that some applications should remain on-premises.
When evaluating this migration, I would like you to consider scalability. I would also like you to consider reliability. In addition, please consider security. Furthermore, please consider operational efficiency. Also consider observability. In addition, consider governance. Please also consider compliance. Please also consider cost optimization. Please also consider performance optimization.
For scalability considerations, please discuss scalability in detail. Explain how applications can scale. Explain how databases can scale. Explain how APIs can scale. Explain how workloads can scale. Explain how cloud services can scale. Explain how scaling strategies differ across various workload types.
For security considerations, please discuss security in detail. Explain identity management. Explain access control. Explain encryption. Explain key management. Explain monitoring. Explain auditing. Explain governance. Explain security best practices. Explain common mistakes. Explain common risks.
For observability considerations, please discuss metrics. Please discuss logs. Please discuss traces. Please discuss dashboards. Please discuss alerting. Please discuss monitoring. Please discuss operational excellence. Please discuss troubleshooting. Please discuss incident response.
For cost optimization considerations, please discuss cost optimization in detail. Explain reserved capacity. Explain autoscaling. Explain rightsizing. Explain monitoring. Explain token optimization. Explain caching. Explain storage optimization. Explain network optimization. Explain operational efficiency improvements.
In addition to discussing cloud migration, I would also like you to discuss artificial intelligence adoption.
Our organization is interested in generative AI. We are interested in large language models. We are interested in foundation models. We are interested in retrieval augmented generation. We are interested in AI agents. We are interested in prompt engineering. We are interested in token optimization. We are interested in model observability. We are interested in model evaluation.
Please discuss these topics in detail.
Please explain prompt engineering in detail. Please explain prompt engineering best practices. Please explain common prompt engineering mistakes. Please explain prompt engineering techniques. Please explain prompt optimization strategies.
Please explain token optimization in detail. Please explain how token optimization reduces costs. Please explain how token optimization improves latency. Please explain how token optimization impacts scalability. Please explain how token optimization affects context windows.
Please explain semantic caching. Please explain response caching. Please explain prompt compression. Please explain context compression. Please explain retrieval optimization. Please explain model routing. Please explain model selection. Please explain inference optimization.
Please provide examples wherever possible.
Please provide recommendations wherever possible.
Please provide best practices wherever possible.
Please provide implementation guidance wherever possible.
Please provide tradeoff analysis wherever possible.
Please provide risk analysis wherever possible.
Please provide mitigation strategies wherever possible.
Please provide operational guidance wherever possible.
Please provide governance guidance wherever possible.
Please provide security guidance wherever possible.
Please provide architecture guidance wherever possible.
Please provide implementation guidance wherever possible.
Please provide migration guidance wherever possible.
Please provide detailed explanations rather than short explanations.
Please provide comprehensive explanations rather than brief explanations.
Please provide thorough explanations rather than summary explanations.
Please provide complete explanations rather than partial explanations.
Please provide detailed explanations rather than short explanations.
Please provide comprehensive explanations rather than brief explanations.
Please provide thorough explanations rather than summary explanations.
Please provide complete explanations rather than partial explanations.
Please provide detailed explanations rather than short explanations.
Please provide comprehensive explanations rather than brief explanations.
Please provide thorough explanations rather than summary explanations.
Please provide complete explanations rather than partial explanations.
Please provide detailed explanations rather than short explanations.
Please provide comprehensive explanations rather than brief explanations.
Please provide thorough explanations rather than summary explanations.
Please provide complete explanations rather than partial explanations.
""" 

# ----------------------------------
# Original Tokens
# ----------------------------------

original_tokens = count_tokens(prompt)

# ----------------------------------
# Compress
# ----------------------------------

compressed = compressor.compress_prompt(
    prompt,
    rate=0.5
)

compressed_prompt = compressed["compressed_prompt"]

compressed_tokens = count_tokens(
    compressed_prompt
)

saved = (
    original_tokens -
    compressed_tokens
)

pct = (
    saved /
    original_tokens
) * 100

# ----------------------------------
# Results
# ----------------------------------

print("\n" + "="*60)
print("LLMLINGUA COMPRESSION RESULTS")
print("="*60)

print(
    f"Original Tokens: "
    f"{original_tokens:,}"
)

print(
    f"Compressed Tokens: "
    f"{compressed_tokens:,}"
)

print(
    f"Tokens Saved: "
    f"{saved:,}"
)

print(
    f"Savings: "
    f"{pct:.2f}%"
)

print("\nCompressed Prompt:\n")
print(compressed_prompt)

the output is like this below

(llmcachevenv) (base) retheesh@Retheeshs-MacBook-Pro llm_caching % python llmlingua_demo.py
Loading weights: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 391/391 [00:00<00:00, 4813.31it/s]
Token indices sequence length is longer than the specified maximum sequence length for this model (1124 > 512). Running this sequence through the model will result in indexing errors

============================================================
LLMLINGUA COMPRESSION RESULTS
============================================================
Original Tokens: 929
Compressed Tokens: 401
Tokens Saved: 528
Savings: 56.84%

Compressed Prompt:

your assistance understand evaluate cloud architecture my organization considering for future modernization initiative. Before answer review information below think deeply about architectural considerations provide detailed comprehensive thoughtful well-structured response. important response be detailed prefer detailed answer short answer. not provide brief answer. provide comprehensive answer all important considerations included in answer. If tradeoffs explain tradeoffs risks explain risks benefits explain benefits. Our organization evaluating migrate enterprise applications from on-premises datacenters to public cloud environment discussing topic internally for months Different stakeholders have different opinions Some believe migration quickly Other gradually others believe some applications should remain on-premises. evaluating migration consider scalability reliability consider security operational efficiency observability governance compliance cost optimization performance optimization. For scalability considerations discuss scalability in detail. Explain applications databases APIs workloads cloud services scale scaling strategies differ across workload types For security considerations discuss security in detail Explain identity management access control encryption key management monitoring auditing governance security best practices common mistakes common risks. For observability considerations discuss metrics logs traces dashboards alerting monitoring operational excellence.discuss troubleshooting incident response. For cost optimization considerations discuss cost optimization in detail Explain reserved capacity autoscaling rightsizing monitoring token optimization caching storage optimization network optimization operational efficiency improvements discussing cloud migration discuss artificial intelligence adoption. organization interested in generative AI large language models foundation models retrieval augmented generation AI agents prompt engineering token optimization model observability model evaluation. discuss these topics in detail explain prompt engineering prompt engineering best practices common prompt engineering mistakes prompt engineering techniques prompt optimization strategies explain token optimization reduces costs improves latency impacts scalability affects context windows explain semantic caching response caching prompt compression context compression retrieval optimization model routing model selection inference optimization provide examples provide recommendations provide best practices implementation guidance tradeoff analysis risk analysis mitigation strategies operational guidance governance guidance security guidance architecture guidance implementation guidance migration guidance provide detailed explanations short explanations comprehensive explanations brief explanations thorough explanations summary explanations complete explanations partial explanations detailed explanations short explanations comprehensive explanations brief explanationsprovide thorough explanations summary complete explanations partial detailed explanations short comprehensive explanations brief explanations thorough explanations summary explanations complete explanations partial explanations detailed short comprehensive explanations brief explanations thorough explanations summary explanations complete explanations partial explanations

Here is a short analysis on this

LLMLingua results
================
Original Tokens:     929
Compressed Tokens:   401
Tokens Saved:        528
Savings:             56.84%

ToeknShrini Results
===================
Original Tokens:     1389
Compressed Tokens:   1315
Tokens Saved:        74
Savings:             5.3%

Compression actions performed by LLMLingua:

  • Removed repetitive instructional phrases such as “Please explain” and “Please provide”.
  • Eliminated redundant adjectives and modifiers such as detailed, comprehensive, thorough, and complete when they conveyed similar intent.
  • Compressed verbose sentences into concise keyword-oriented statements.
  • Preserved important entities, topics, and requirements while removing filler words.
  • Retained semantic meaning while reducing grammatical verbosity.
  • Consolidated repeated requests and duplicated instructions.
  • Prioritized high-information tokens and discarded low-information tokens.
  • Optimized prompts for token efficiency rather than human readability.

Compression actions performed by TokenShrink:

  • Replaced frequently occurring phrases with short aliases (e.g., “we are interested in”P1).
  • Substituted longer expressions with shorter equivalents (e.g., “rather than”vs).
  • Created a compact decode dictionary at the beginning of the prompt.
  • Reused dictionary entries whenever the same phrase appeared multiple times.
  • Reduced repeated natural-language expressions through symbolic references.
  • Preserved the original sentence structure and prompt organization.
  • Avoided removing information or changing semantic meaning.
  • Focused on lossless or near-lossless compression rather than semantic pruning.

A useful way to think about the difference is that TokenShrink asks “Can I represent the same words more efficiently?”, whereas LLMLingua asks “Do I need all of these words at all?”. This is why TokenShrink typically achieves modest savings with very low risk, while LLMLingua can achieve much larger reductions by removing semantically redundant content.

The techniques covered in this article highlight that token optimization is not a single technology but a collection of strategies applied across the entire GenAI workflow. RTK reduces verbose tool outputs, Cross Encoders improve retrieval precision, TokenShrink performs lightweight prompt compression, Mem0 manages long-term conversational memory, and LLMLingua applies semantic compression to large contexts. Each technique targets a different source of token growth and delivers benefits in cost reduction, latency improvement, and context efficiency. In practice, the most effective GenAI architectures often combine multiple optimization layers, creating a system that retrieves smarter, remembers better, compresses intelligently, and scales economically as usage grows.

Thats it for this article, If you would like to read the first part of it, it is here!


메타데이터
post_id
9ecfbf4f3af8
slug
cut-your-llm-bill-before-it-cuts-your-budget-a-deep-dive-into-token-optimization-part-2-rtk-9ecfbf4f3af8
url
https://medium.com/@ravi.retheesh/cut-your-llm-bill-before-it-cuts-your-budget-a-deep-dive-into-token-optimization-part-2-rtk-9ecfbf4f3af8
canonical_url
https://medium.com/@ravi.retheesh/cut-your-llm-bill-before-it-cuts-your-budget-a-deep-dive-into-token-optimization-part-2-rtk-9ecfbf4f3af8
author_url
https://medium.com/@ravi.retheesh
status
ok
fetched_at
2026-06-24 04:09:36