pipr.tools
Pipes
Clean Email Strip formatting from pasted email for clean plain text Clean AI Output Clean plain text from ChatGPT or AI output Decode & Format JWT Decode a JWT and pretty-print header and payload Word Frequency Count word frequency in text

token-truncate Truncate text to fit a token budget (real BPE)

# Stats
Input0 chars
Output0 chars

Flags

--max number default: 4096
--encoding select: cl100k_base | o200k_base default: cl100k_base

Examples

Truncate a system prompt to fit a 20-token budget

Usage
"You are a helpful assistant that answers questions about pro..." | token-truncate

Trim text to exactly 10 tokens for an API call

Usage
"The quick brown fox jumps over the lazy dog. Pack my box wit..." | token-truncate

Truncate a stack trace to fit an LLM error-analysis prompt

Usage
"Error: ENOENT: no such file or directory, open '/usr/local/e..." | token-truncate
View source
async (input, opts = {})=>{
                if (!input.trim()) return "(empty input)";
                const { getEncoder } = await import('./tokenizer_qMtbZfTQ.mjs').then(async (m)=>{
                    await m.__tla;
                    return m;
                });
                const encoding = opts.encoding || "cl100k_base";
                const max = Math.max(1, parseInt(opts.max, 10) || 4096);
                const enc = await getEncoder(encoding);
                const ids = enc.encode(input);
                if (ids.length <= max) return input;
                const truncated = enc.decode(ids.slice(0, max));
                return new TextDecoder("utf-8", {
                    fatal: false
                }).decode(truncated);
            }

Also useful for