ai newsDi., 3. Nov.
EilmeldungClaude Message Threads Are Fast Until Your Agent Waits 5 Minutes

We Measured 1,500 Lines of Terminal Bloat Burning $0.20 Per Prompt in Cursor. Here's How We Stripped 97% of It in 2.5ms

If you are using Cursor, Claude Code, Aider, Windsurf, or Codex , you are likely hemorrhaging tokens—and money—every time your agent touches the command line. We recently profiled an autonomous agent workflow during a routine feature iteration. Over 45 minutes, the agent invoked test suites and bui

dev.toDienstag 20:00 · 03.11.6 Min. Lesezeit
Bild: dev.to

Posted on Nov 3 • Originally published at github.com

If you are using Cursor, Claude Code, Aider, Windsurf, or Codex, you are likely hemorrhaging tokens—and money—every time your agent touches the command line.

We recently profiled an autonomous agent workflow during a routine feature iteration. Over 45 minutes, the agent invoked test suites and build scripts 28 times.

Here is what shocked us: over 78% of the agent's prompt context was not user code, architecture diagrams, or instructions. It was terminal garbage.

Progress spinners, ANSI escape codes, passing test dot-markers, Webpack asset lists, and 40-frame deep node_modules stack traces were saturating the LLM context window.

We built termlens to solve this: an open-source, zero-token terminal compactor and semantic error lens that intercepts CLI streams, strips 97% of terminal noise in under 2.5ms, and feeds pristine, actionable signals straight into your AI coding agents.

Here is the data, the architecture, and how we engineered it.

Consider a standard Jest suite of 85 unit tests where 84 pass and 1 fails.

When your agent runs npm test, standard terminal output produces 1,482 lines of text (~3,840 tokens):

PASS src/math.test.ts (1.2s) PASS src/string.test.ts (0.8s) PASS src/auth/hash.test.ts (2.1s) PASS src/database/client.test.ts (3.4s)... [84 passing suites with detailed timing info]... FAIL src/auth/jwt.test.ts (4.1s) ● should reject expired token signature Expected: 401, Received: 200 at Object.<anonymous> (/workspace/src/auth/jwt.test.ts:42:29) at Promise.then.completed (/workspace/node_modules/jest-circus/build/utils.js:391:28) at new Promise (<anonymous>) at callAsyncCircusFn (/workspace/node_modules/jest-circus/build/utils.js:316:16) at _callCircusTest (/workspace/node_modules/jest-circus/build/run.js:218:40)... [38 lines of internal runner stack frames]... Enter fullscreen mode Exit fullscreen mode What did the AI actually need to fix the bug? Exactly 4 lines:

FAIL src/auth/jwt.test.ts:42:29 ● should reject expired token signature Expected: 401 Received: 200 Enter fullscreen mode Exit fullscreen mode The Silent Cost Let's calculate the real-world overhead:

Financial cost is only half the problem. When an LLM's context is flooded with 3,000 tokens of noisy terminal logs, it triggers Context Distraction.

Research on transformer attention mechanisms repeatedly demonstrates that needle-in-a-haystack recall degrades significantly when prompt windows are polluted with repetitive semi-structured logs. Agents overlook critical architectural constraints in the system prompt because their attention weights are drowned out by ANSI escape codes.

The immediate developer reaction is: "Why not just run npm test | grep FAIL?"

We tested this. In production, naive piping breaks in three distinct ways:

We engineered termlens in TypeScript with strict performance constraints: zero heavy native dependencies, constant memory allocation, and sub-3ms latency.

┌────────────────────────────────────────────────────────┐ │ termlens Pipeline │ ├────────────────────────────────────────────────────────┤ │ │ │ [ Raw CLI Command ] (npm test, tsc, pytest, docker) │ │ │ │ │ ▼ │ │ ┌─────────────────────────────────┐ │ │ │ 1. PTY Stream Interceptor │ (Preserves exit │ │ │ & Linear ANSI Stripper │ codes & TTY) │ │ └─────────────────┬───────────────┘ │ │ ▼ │ │ ┌─────────────────────────────────┐ │ │ │ 2. Bounded Ring Buffer │ (O(1) memory, │ │ │ (Fixed 1,000 FIFO slots) │ prevents OOM) │ │ └─────────────────┬───────────────┘ │ │ ▼ │ │ ┌─────────────────────────────────┐ │ │ │ 3.

Framework Semantic Lenses │ (Jest, Pytest, │ │ │ (Heuristic Pattern Decoders)│ TSC, Docker...) │ │ └─────────────────┬───────────────┘ │ │ ▼ │ │ ┌─────────────────────────────────┐ │ │ │ 4. Adaptive Density Formatters │ │ │ │ [ Human ] [ Agent ] [ CI ] │ │ │ └─────────────────┬───────────────┘ │ │ ▼ │ │ Clean, Actionable Signal │ │ (<100 tokens, 100% precision) │ └────────────────────────────────────────────────────────┘ Enter fullscreen mode Exit fullscreen mode A. Linear-Time ANSI Sanitization Rather than running complex regular expressions that trigger catastrophic backtracking on 10MB outputs, we implemented an optimized single-pass ANSI scanner:

export function stripAnsi ( str: string ): string { // Fast-path: skip clean strings if (! str. includes ( ' \ x1b ' )) return str; return str. replace ( / \x 1b \[[ 0-9; ] * [ a-zA-Z ] /g, '' ); } Enter fullscreen mode Exit fullscreen mode B. Bounded Rolling Ring Buffer Build logs can produce 500,000 lines. Storing unbounded strings in memory causes V8 heap thrashing and garbage collection spikes.

termlens uses an in-memory Circular Ring Buffer capped at 1,000 lines:

export class RingBuffer < T > { private buffer: T []; private head = 0; private size = 0; constructor ( private readonly capacity: number = 1000 ) { this. buffer = new Array ( capacity ); } push ( item: T ): void { this. buffer [ this. head ] = item; this. head = ( this. head + 1 ) % this. capacity; if ( this. size < this. capacity ) this. size ++; } toArray (): T [] { const result: T [] = []; const start = this. size < this. capacity? 0: this. head; for ( let i = 0; i < this. size; i ++ ) { result. push ( this. buffer [( start + i ) % this. capacity ]); } return result; } } Enter fullscreen mode Exit fullscreen mode No matter how large the build log is, memory consumption remains locked at under 4MB.

termlens introduces the concept of Semantic Lenses. A lens is a specialized parser that understands the structural syntax of a specific tool:

We ran rigorous head-to-head benchmarks measuring line counts, token consumption, and execution overhead across common development workflows.

Summary: termlens consistently achieves a 90% to 97% reduction in token consumption with an average latency overhead of under 2.5 milliseconds.

Coding agents shouldn't need custom shell wrappers. That's why termlens includes a built-in Model Context Protocol (MCP) server.

You can plug termlens directly into Claude Desktop, Claude Code, Cursor, or any MCP client.

Add to your claude_desktop_config.json or MCP settings:

{ "mcpServers": { "termlens": { "command": "npx", "args": [ "-y", "termlens", "--mcp" ] } } } Enter fullscreen mode Exit fullscreen mode Now, when your agent calls the tool:

{ "command": "npm test", "lens": "jest", "format": "agent" } Enter fullscreen mode Exit fullscreen mode The agent receives a hyper-focused, structured payload ready for immediate reasoning:

status: failed exitCode: 1 failures: - file: " src/auth/jwt.test.ts" line: 42 column: 29 test: " should reject expired token signature" expected: " 401" received: " 200" codeSnippet: " expect(response.status).toBe(401)" tokenSavedEstimate: 3766 Enter fullscreen mode Exit fullscreen mode The agent fixes the bug in a single turn without wandering through node internal modules.

You can run termlens immediately without installing anything:

# Run your test suite with instant compaction npx termlens npm test # Run TypeScript type check npx termlens tsc --noEmit # Run Python tests npx termlens pytest # Run Docker build npx termlens docker build -t my-app. Enter fullscreen mode Exit fullscreen mode Or install globally:

npm install -g termlens termlens npm test Enter fullscreen mode Exit fullscreen mode Formatter Modes: Human View (Default): Minimal, high-contrast visual display with clean failure cards. Agent View ( --format agent ): Ultra-compact structured output designed for AI token efficiency. CI View ( --format ci ): Strict machine-readable telemetry with zero color artifacts. 7. Open Source & Contributions termlens is 100% open-source under the MIT license.

If you are building AI agents, tooling, or developer infrastructure, try it on your test suite today and see how many tokens you save. We welcome feedback, benchmark results, and new lens contributions!

Quelle: dev.to (dev.to). Original öffnen