将文本上下文渲染为图片,本地代理为 Claude Code 削减约 60% token 成本
pxpipe 是一个本地代理,拦截 Claude Code API 请求,将系统提示、工具文档和旧历史记录等大块文本渲染为紧凑的 PNG 图片。利用图片 token 按像素尺寸而非字符数计费的特点,将输入 token 量削减约 60%,端到端 API 费用可降低 59–70%。它改写请求负载并保留提示缓存兼容性,默认对 Claude Fable 5 和 GPT-5.6 启用,提供仪表盘实时监控和收益门槛,避免稀疏文本反而增加开销。适合使用 LLM 编程助手的开发者,在保持任务能力的同时大幅降低 API 开销。
pxpipe is a local proxy that rewrites bulky text content — system prompts, tool documentation, and older conversation history — into compact PNG images before they reach the Claude Code API. Since vision tokens are priced by pixel dimensions, not by the amount of text embedded, dense content like code, JSON, and command output can be packed at roughly 3.1 characters per image token versus 1 character per text token. On production traffic, this translates to an end-to-end reduction of 59–70% in token costs, depending on workload. The proxy sits transparently between your CLI and Anthropic's API, intercepting only the request side and leaving model responses untouched.
pxpipe 是一个本地代理,在请求到达 Claude Code API 之前,将系统提示、工具文档和较旧的对话历史等大段文本转换为紧凑的 PNG 图片。由于视觉 token 按像素尺寸计价而非文本量,像代码、JSON、命令输出这类密集文本,每图像 token 大约可承载 3.1 个字符,而文本 token 只有 1 个字符。在生产流量中,根据工作负载不同,端到端 token 成本可降低 59%–70%。该代理透明地运行在你的 CLI 与 Anthropic API 之间,只拦截请求侧,不修改模型响应。
Claude Code sessions accumulate large system prompts, tool definitions, and extensive history as the session progresses. Every turn repeats the entire static prefix and much of the history, inflating input tokens linearly with session length. Traditional compression approaches that operate on raw text yield at most a ~2x improvement, because text is already relatively dense. The vision API's fixed-pixel pricing breaks this paradigm entirely: an image block costs the same in tokens regardless of how much text is rendered inside it, allowing dense context to be compressed 3–4× more effectively than any text-based method.
Claude Code 会话会积累庞大的系统提示、工具定义以及随对话推进而增长的历史内容。每一轮都会重复整个静态前缀和大量历史记录,导致输入 token 随会话长度线性增长。传统的纯文本压缩方案最多只能提升约 2 倍,因为文本本身已经相对紧凑。视觉 API 的固定像素定价完全打破了这个模式:一个图像块无论内部渲染了多少文本,其 token 成本都不变,这使得密集上下文能以比任何文本压缩方法高出 3–4 倍的效率进行压缩。
pxpipe intercepts /v1/messages API calls, identifies three categories of eligible input: large tool_result bodies (file reads, command output, logs) above ~6k characters of token-dense content; older collapsed history (recent turns stay text); and the static system prompt + tool docs slab. Each block is wrapped at 1928px-wide columns and packed into one or more PNG images (≈92,000 chars per 1928×1928 image, costing ≈4,761 vision tokens). Crucially, a per-request profitability estimator decides whether imaging is beneficial: if the content is sparse prose (> ~19 chars per token in text form), it remains text. The system prompt prefix is preserved to maintain prompt caching compatibility. The exact savings per request are measured against a counterfactual count_tokens probe fired in parallel with the real forward, and both are logged to ~/.pxpipe/events.jsonl for auditability.
pxpipe 拦截 /v1/messages API 调用,识别三类可处理的输入:超过约 6k 字符的密集 tool_result 内容(文件读取、命令输出、日志);较早折叠的历史(近期轮到保持文本);以及静态系统提示 + 工具文档块。每个块被包裹在 1928 像素宽的列中,打包成一个或多个 PNG 图片(每张 1928×1928 图片可容纳约 92,000 字符,耗约 4,761 视觉 token)。关键在于,每个请求都有一个收益估算器来判断是否值得图像化:如果内容是稀疏散文(文本形式的 token 密度高于约 19 字符/token),则保留为文本。系统提示前缀被保留以维持提示缓存的兼容性。每个请求的真实节省通过与实际请求并行发出一个 count_tokens 探针来度量其反事实成本,两者都记录在 ~/.pxpipe/events.jsonl 中,便于审计。
The proxy runs as a local HTTP server (default on 127.0.0.1:47821). You point your Claude Code CLI at it via ANTHROPIC_BASE_URL=http://127.0.0.1:47821 claude. For each request, it: (1) receives the JSON body; (2) applies the profitability gate to each eligible text block; (3) renders blocks that pass the gate into PNG images using hard-wrapped columns and monospace fonts; (4) splices the image blocks back into the request payload, preserving the static prefix and cache_control markers; (5) forwards the rewritten request to the actual Anthropic API; (6) returns the unmodified response to the CLI. The dashboard at the same port shows live savings, side-by-side conversions, a kill switch, and model chips to control which models are imaged. The source code is structured around src/core/ with the rendering logic, src/worker.ts for the proxy server, and src/dashboard/ for the web UI.
代理作为本地 HTTP 服务器运行(默认在 127.0.0.1:47821)。通过 ANTHROPIC_BASE_URL=http://127.0.0.1:47821 claude 让 Claude Code CLI 指向它。对于每个请求,它的流程是:(1) 接收 JSON 请求体;(2) 对每个符合条件的文本块应用收益门控;(3) 将通过的块渲染为 PNG 图片(通过硬换行列和等宽字体);(4) 将图片块拼回请求负载,保留静态前缀和 cache_control 标记;(5) 将改写后的请求转发给实际的 Anthropic API;(6) 将原样响应返回给 CLI。同一端口的仪表盘显示实时节省、逐项转换对比、一个终止开关和用于控制哪些模型被图像化的模型开关。源代码结构围绕 src/core/(渲染逻辑)、src/worker.ts(代理服务器)和 src/dashboard/(Web UI)组织。
Installation is a single command: npx pxpipe-proxy starts the proxy on 127.0.0.1:47821. Then point Claude Code at it: ANTHROPIC_BASE_URL=http://127.0.0.1:47821 claude. The dashboard at http://127.0.0.1:47821/ shows tokens saved, all conversions, a kill switch, and model chips. For library use without the proxy, the package exports renderTextToPngs (text → images) and transformAnthropicMessages (rewrite a raw request).
安装只需一条命令:npx pxpipe-proxy 启动代理,默认监听 127.0.0.1:47821。然后让 Claude Code 指向它:ANTHROPIC_BASE_URL=http://127.0.0.1:47821 claude。仪表盘在 http://127.0.0.1:47821/,显示 token 节省量、所有转换、一个终止开关和模型开关。无需代理时,包导出 renderTextToPngs(文本转图片)和 transformAnthropicMessages(改写原始请求)用于库集成。
pxpipe shines on token-dense workloads like coding sessions with large file reads, logs, and tool outputs, where the end-to-end bill can drop 59–70%. It wins on tasks where the model needs gist understanding rather than byte-exact recall — the imaging is lossy. Exact 12-character hex strings in dense renders are recalled at 13/15 on Fable 5 but 0/15 on Opus, and misses are silent confabulations, not errors. Therefore, byte-exact values (IDs, hashes, secrets) must stay text, which pxpipe does by keeping recent turns in text. The verbatim-risk guard is not yet built, so subagents on non-allowlisted models pass through entirely. Models outside the default scope (Fable 5, GPT 5.6) are opt-in; Opus 4.8 and GPT 5.5 show measurable misread rates and are deliberately excluded. PNG encoding adds latency to large requests before they leave. ASCII/Latin-1 content is well tested; CJK works but with conservative sizing. The gains depend on workload — sparse prose at >19 chars/token may actually cost more when imaged.
pxpipe 在处理密集 token 的工作负载时表现优异,例如包含大型文件读取、日志和工具输出的编程会话,端到端费用可降低 59–70%。它适用于模型需要理解大意而非逐字节精确回想的任务——图像化是有损的。密集渲染中精确的 12 字符十六进制字符串召回率在 Fable 5 上是 13/15,但 Opus 上为 0/15,且失败是静默的幻觉而非报错。因此,逐字节精确的值(ID、哈希、密钥)必须保留为文本——pxpipe 通过保持近期轮到文本形式来实现。逐字风险防护尚未构建,因此不在允许列表上的子代理会全部透传。默认范围之外的模型(Fable 5、GPT 5.6)需要手动启用;Opus 4.8 和 GPT 5.5 有可测量的误读率,被故意排除。PNG 编码会在大型请求发出前增加延迟。ASCII/Latin-1 内容经过充分测试;CJK 可以工作但采用较为保守的尺寸。收益取决于工作负载—稀疏散文(>19 字符/token)在图像化后可能反而更贵。