llm-context-code-stripper
v0.1.0
Published
Strip large base64 data: URIs and raw base64 blobs out of source code before sending it to an LLM — saves context-window tokens and keeps prompts within request size limits. Works with OpenAI, Claude, and any LLM. Zero dependencies.
Maintainers
Readme
llm-context-code-stripper
Strip large base64
data:URIs and raw base64 blobs out of source code before you send it to an LLM.
Why
Source files often inline sprites, audio, fonts, or images as base64 data: URIs or raw base64 string literals. A single asset can balloon a file to hundreds of kilobytes even though the actual logic is tiny.
When you feed that code to an LLM, those blobs are pure waste:
- The model can't read base64 anyway — it's opaque noise to the tokenizer.
- It burns context-window tokens — a 770 KB sprite can eat your entire budget and crowd out the real code.
- It can blow past request size limits — pushing you over provider payload caps for no benefit.
llm-context-code-stripper replaces those blobs with a short placeholder while leaving the real code untouched. It's a pure, synchronous function with zero runtime dependencies, safe for both browser and server bundles.
- const sprite = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...770KB...";
+ const sprite = "data:[STRIPPED]";Install
npm install llm-context-code-stripperpnpm add llm-context-code-stripperyarn add llm-context-code-stripperUsage
Quick clean
import { stripCode } from "llm-context-code-stripper";
const cleaned = stripCode(sourceCode);
// send `cleaned` to your LLM instead of the raw fileWith stats
import { stripBase64Assets } from "llm-context-code-stripper";
const result = stripBase64Assets(sourceCode);
console.log(result);
// {
// code: "const sprite = \"data:[STRIPPED]\"; ...",
// beforeBytes: 812345,
// afterBytes: 33012,
// dataUrisStripped: 1,
// rawBase64Stripped: 0,
// }
const saved = result.beforeBytes - result.afterBytes;
console.log(`Trimmed ${(saved / 1024).toFixed(0)} KB before sending to the LLM`);Custom placeholder and thresholds
import { stripBase64Assets } from "llm-context-code-stripper";
const result = stripBase64Assets(sourceCode, {
placeholder: "<asset removed>",
minDataUriLength: 100, // strip smaller data: URIs than the default 200
minRawBase64Length: 512, // strip smaller raw base64 blobs than the default 1024
});Measuring payload size
import { byteLength } from "llm-context-code-stripper";
byteLength("hello"); // 5
byteLength("😀"); // 4 — correct utf-8 byte count, not string lengthAPI
stripBase64Assets(code, options?): StripResult
Strips large data: URIs and raw base64 string literals from code, returning the cleaned code plus statistics.
| Option | Type | Default | Description |
| -------------------- | -------- | -------------- | ------------------------------------------------------------------- |
| placeholder | string | "[STRIPPED]" | Replacement token substituted for each stripped blob. |
| minDataUriLength | number | 200 | Minimum length (chars) of a quoted data: URI before it's stripped. |
| minRawBase64Length | number | 1024 | Minimum length (chars) of a raw base64 literal before it's stripped. |
Returns StripResult:
| Field | Type | Description |
| ------------------- | -------- | -------------------------------------------------- |
| code | string | The cleaned source code. |
| beforeBytes | number | Byte length (utf-8) of the input, before stripping. |
| afterBytes | number | Byte length (utf-8) of the output, after stripping. |
| dataUrisStripped | number | Number of data: URIs that were replaced. |
| rawBase64Stripped | number | Number of raw base64 literals that were replaced. |
stripCode(code): string
Convenience wrapper that returns only the cleaned code. Use stripBase64Assets directly when you need the stats.
byteLength(code): number
Returns the utf-8 byte length of a string via TextEncoder, falling back to String.length when TextEncoder is unavailable. Useful for measuring real payload size before and after stripping.
How it works
Two conservative regular expressions target only asset-shaped strings:
data:URIs — any quoted (",', or`)data:URI at or aboveminDataUriLengthcharacters. The floor prevents removing tiny inline icons an LLM might legitimately read.- Raw base64 literals — any quoted string of at least
minRawBase64Lengthcontinuous base64 characters (A–Z a–z 0–9 + / =) that is not adata:URI. The high default keeps normal long constants (URLs, ids, prompts) safe.
Everything else — your actual code — is left byte-for-byte intact.
License
MIT © 2026 oratis
