RTK Compression Engine in OmniRoute: Architecture and Token Reduction Strategy

The RTK compression engine reduces LLM token consumption by applying configurable rule-based filters—such as stripping comments, removing duplicate lines, and truncating verbose logs—to tool outputs before they reach the upstream provider.

The RTK (Rule-Based Token-Killer) compression engine is a core component of the OmniRoute repository that sits within the prompt-compression pipeline. It applies deterministic text transformations to message payloads according to a JSON catalog of filter definitions, directly addressing token limit constraints and API cost optimization.

What Is the RTK Compression Engine?

The RTK compression engine is implemented in open-sse/services/compression/rtkEngine.ts and provides a pluggable compression mode that transforms tool-result text through a series of filter functions.

Engine Registration and Priority

RTK is registered in the compression registry at open-sse/services/compression/registry.ts with a stackPriority of 10, ensuring it executes before lower-priority engines. According to the OmniRoute source code, the engine is declared as:

{ id: "rtk", name: "RTK", stackPriority: 10, stable: true }

View in registry.ts

Core Implementation

The primary entry point is applyRtkCompression, an async function that loads a JSON filter catalog, builds a pipeline of active filters, and processes the payload sequentially. In open-sse/services/compression/rtkEngine.ts, the function orchestrates the compression workflow:

export async function applyRtkCompression(
  payload: string,
  config: CompressionConfig,
  rtkConfig: RtKConfig,
): Promise<CompressionResult> {
  const catalog = await loadRtkFilterCatalog();
  const pipeline = buildRtkPipeline(catalog, rtkConfig);
  let compressed = payload;
  for (const filter of pipeline) {
    compressed = await filter(compressed);
  }
  const originalTokens = countTokens(payload);
  const compressedTokens = countTokens(compressed);
  return { originalTokens, compressedTokens, compressed };
}

View implementation details

How the RTK Compression Engine Reduces Token Usage

RTK reduces token consumption through deterministic text transformation rather than semantic compression, making it fast and predictable for high-throughput scenarios.

Filter Pipeline Overview

The engine constructs a dynamic pipeline based on the enabledFilters array in RtKConfig. Each filter in open-sse/services/compression/rtkEngine.ts receives the current string and returns a shortened version, with the output feeding into the next filter stage until the final token count is calculated.

Key Filter Mechanisms

  • Repetitive Line Removal: Collapses blocks of identical log lines (e.g., build output) into single placeholders, eliminating thousands of redundant tokens from tool results.

  • Code Comment Stripping: When stripCodeComments is enabled, regex patterns remove // and /* */ comments from fenced code blocks, preserving functionality while removing non-essential text that would otherwise be tokenized.

  • Truncation Limits: Enforces maxLinesPerResult and maxCharsPerResult boundaries from the CompressionConfig interface, dropping middle sections of oversized outputs to maintain hard token ceilings.

  • Line Grouping: With enableGrouping activated and a configured groupingThreshold, the engine identifies similar lines and replaces them with a summary token (e.g., "5 identical warning lines"), converting multiple tokens into one.

  • Intensity Scaling: The intensity parameter (1-10) tightens truncation thresholds and activates aggressive filters, allowing operators to tune the compression-to-information ratio dynamically.

After processing, the shared countTokens utility calculates the delta between originalTokens and compressedTokens, providing measurable savings metrics.

Configuration and Runtime Integration

Configuration Schema

The RTK behavior is controlled through the RtKConfig interface defined in open-sse/services/compression/types.ts:

export interface RtKConfig {
  enabled: boolean;
  enabledFilters: string[];
  intensity: number;
  stripCodeComments: boolean;
  enableGrouping: boolean;
  groupingThreshold: number;
}

View type definitions

Runtime Selection

The selectCompressionEngine function in open-sse/services/compression/strategySelector.ts determines whether to invoke RTK based on the rtkConfig.enabled flag:

export async function selectCompressionEngine(
  payload: string,
  compressionConfig: CompressionConfig,
  rtkConfig: RtKConfig,
): Promise<string> {
  if (rtkConfig.enabled) {
    const result = await applyRtkCompression(payload, compressionConfig, rtkConfig);
    return result.compressed;
  }
  // other engines omitted
  return payload;
}

View selector logic

Implementation Examples

Configuring RTK via the Settings API

Update compression settings through the REST endpoint at src/app/api/settings/compression/route.ts:

PATCH /api/settings/compression HTTP/1.1
Content-Type: application/json

{
  "rtkConfig": {
    "enabled": true,
    "enabledFilters": ["stripComments", "deduplicate", "truncateLongLogs"],
    "intensity": 7,
    "stripCodeComments": true,
    "enableGrouping": true,
    "groupingThreshold": 5
  },
  "compressionConfig": {
    "maxLinesPerResult": 120,
    "maxCharsPerResult": 10000
  }
}

The Zod validation schema in the route ensures type safety for these parameters. View API route

Direct Engine Invocation

For custom tooling, import and call applyRtkCompression directly:

import { applyRtkCompression } from "@/open-sse/services/compression/rtkEngine";
import { type CompressionConfig, type RtKConfig } from "@/open-sse/services/compression/types";

const rawOutput = await fetchBuildLogs();

const compressionCfg: CompressionConfig = {
  maxLinesPerResult: 120,
  maxCharsPerResult: 10000,
};

const rtkCfg: RtKConfig = {
  enabled: true,
  enabledFilters: ["stripComments", "deduplicate"],
  intensity: 5,
  stripCodeComments: true,
  enableGrouping: false,
  groupingThreshold: 0,
};

const { originalTokens, compressedTokens, compressed } =
  await applyRtkCompression(rawOutput, compressionCfg, rtkCfg);

console.log(`RTK saved ${originalTokens - compressedTokens} tokens`);

Stacking with Other Engines

Because RTK has stackPriority: 10, it executes before lower-priority engines like "lite" or "caveman":

import { selectCompressionEngine } from "@/open-sse/services/compression/strategySelector";

const result = await selectCompressionEngine(
  payload,
  { maxLinesPerResult: 120, maxCharsPerResult: 10000 },
  {
    enabled: true,
    enabledFilters: ["stripComments", "truncateLongLogs"],
    intensity: 8,
    stripCodeComments: true,
    enableGrouping: true,
    groupingThreshold: 4,
  }
);
// Result contains RTK-processed text ready for secondary compression

Summary

  • The RTK compression engine is a rule-based pipeline in OmniRoute that transforms tool outputs before they reach the LLM.
  • It reduces token usage through comment stripping, duplicate line removal, truncation, and line grouping mechanisms.
  • Configuration happens via the RtKConfig interface in open-sse/services/compression/types.ts, controlling filters, intensity levels, and thresholds.
  • The engine integrates into the compression stack through selectCompressionEngine, running at priority 10 before other engines.
  • Token savings are calculated using the shared countTokens utility and returned in the CompressionResult object.

Frequently Asked Questions

What is the RTK compression engine?

The RTK compression engine is a specialized compression mode within OmniRoute that applies rule-based filters to message payloads. Implemented in open-sse/services/compression/rtkEngine.ts, it transforms text through a configurable pipeline of filters that remove redundant content, strip comments, and truncate verbose outputs before upstream transmission to LLM providers.

How does RTK compression reduce token usage?

RTK reduces token usage by applying deterministic text transformations: removing duplicate log lines, stripping code comments, grouping similar lines into summary tokens, and enforcing maximum line/character limits. Each filter in the pipeline shortens the payload, and the final token count is compared against the original using countTokens to measure concrete savings.

Where is the RTK compression engine configured?

Configuration is defined in open-sse/services/compression/types.ts through the RtKConfig interface and exposed via the API endpoint at src/app/api/settings/compression/route.ts. Administrators can enable specific filters, set intensity levels (1-10), and toggle features like comment stripping and line grouping through JSON payloads validated by Zod schemas.

Can RTK be used alongside other compression engines?

Yes. RTK is registered with stackPriority: 10 in open-sse/services/compression/registry.ts, ensuring it executes before lower-priority engines such as "lite" or "caveman". The output from RTK becomes the input for subsequent engines, allowing for layered compression strategies where RTK handles rule-based cleanup first.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →