50–90% fewer tokens, almost the same answers — a compression API for AI agents and LLM-powered applications.
There is a number that has quietly become one of the most important line items in every serious AI engineering budget: the token count. Every call to an AI LLM is billed by the token.
A new approach in context engineering is now emerging: compress what you send before it reaches the model. Process raw, verbose tool outputs before calling the LLM, strip out redundancy, and ship only the semantically effective signal to the model.
Context semantic compression (Prompt Context) condenses lengthy conversation histories, documents and prompts into compact tokens or summarized text for AI model input. Its core strength lies in resource efficiency: it drastically cuts token consumption, lowers API costs, and bypasses rigid context window limits to process far more background data in one request.
The suitable choice of compression rate is critical. It measures how much original data is shrunk after compression — the space-saving efficiency of the algorithm.
Webtokenflow is not a model and not a provider. It is an API processing pipeline that sits between your application and the cloud LLM — a lossy semantic compression method that keeps critical semantics and strips out relatively unimportant content. Unlike the Headroom method, which is better suited to code compression.
Conversation histories, documents, prompts and tool outputs arrive as a data group of paragraphs.
A fast clustering algorithm in the LSH space of any embedding space, keeping only the effective semantic signal.
A short selected paragraphs is shipped to the model — fewer tokens, almost the same answer.
The example uses the static cc.en.300.vector.txt embedding vector for speed. You can plug in other embedding models — for example, EmbeddingGemma2 — for better performance.
The API roughly processes 10+ million paragraphs in a few seconds, not counting network delay.
A middleware processing pipeline: your application → Webtokenflow → cloud LLM.
No user data upload required, ensuring data security.
Supports arbitrary embedding models, subject to end-user hardware capabilities.
$1.0 per million paragraphs — one paragraph averages about 50 tokens.
10+ million paragraphs processed per second.
A minimal bandwidth footprint for every compressed request.
Roughly equivalent to 50 million tokens in English.
Below is a text example that works out of the box with the default segment tool. You can also develop more sophisticated segmentation for text, JS, TS, C, C++, CPP, Java, Go, or Python.
import OpenAI from "openai";
import fs from "fs";
import { languagemap, initPromise } from "./utils.js";
import { tokenizerMap } from "./tokenizer.js";
import { vectormap } from "./table.js";
await initPromise;
const client = new OpenAI({
baseURL: "http://www.webtokenflow.com/v1",
apiKey: "sk_xxx"
});
function normalizeAbsMax(arr) {
if (!Array.isArray(arr) || arr.length === 0) return [];
let absMax = Math.abs(arr[0]);
for (const cur of arr) {
const abs = Math.abs(cur);
if (abs > absMax) absMax = abs;
}
if (absMax === 0) {
return new Array(arr.length).fill(0);
}
return arr.map(v => v / absMax);
}
async function txtParser() {
try {
const language = "english";
// Get language dictionary, natural tokenizer, embedding hash vectors
const wordsmap = languagemap.get(language);
const tokenizer = tokenizerMap.get(language);
const langah = vectormap.get(language + "a");
const langbh = vectormap.get(language + "b");
if (!wordsmap || !tokenizer || !langah || !langbh) {
throw new Error("missing language resources");
}
// Testing txt file
const content = fs.readFileSync("./test.txt", "utf8");
const segments = content.replace(/\r\n?/g, "\n").split("\n");
const floats = [];
for (const paragraph of segments) {
const tokens = tokenizer.tokenize(paragraph.toLowerCase());
var x = 0;
var y = 0;
// Paragraph vector = sum of word embedding vectors dot hash vector
for (const token of tokens) {
if (wordsmap.has(token)) {
const idx = wordsmap.get(token);
x = x + langah[idx];
y = y + langbh[idx];
}
}
floats.push(x);
floats.push(y);
}
// Normalize all in [-1, +1]
normalizeAbsMax(floats);
const fa = new Float64Array(floats);
const inputBuf = Buffer.from(fa.buffer);
const res = await client.chat.completions.create({
model: "Webtokenflow2.0",
compressionrate: 0.3,
input: inputBuf.toString("base64")
});
const b64Result = res.choices[0].message.content;
const resultBuf = Buffer.from(b64Result, "base64");
if (resultBuf.length % 8 !== 0) {
throw new Error("Float64 % 8 !== 0");
}
const results = new Float64Array(
resultBuf.buffer, resultBuf.byteOffset, resultBuf.length / 8
);
// results are the indexes of the selected paragraphs in segments
console.log("final results length: ", results.length);
console.log(res.usage);
} catch (err) {
console.error("read fail", err.message);
console.error(err.stack);
}
}
txtParser().catch(console.error);
model
The compression model to call, e.g. Webtokenflow2.0.
compressionrate
CR in range 0 < CR < 1.0. CR = 0.5–0.1 means 50–90% fewer tokens.
input
A data group, such as [{ content: string }, { content: string }, …].