新闻详情

新闻详情

首页 / 资讯中心 / 详情

如何用 Headroom 的 simulate 模拟调用预览压缩节省而不发送请求到 LLM

发布时间:2026/9/9 22:16:20来源:尧图网络
如何用 Headroom 的 simulate 模拟调用预览压缩节省而不发送请求到 LLM
如何用 Headroom 的 simulate 模拟调用预览压缩节省而不发送请求到 LLM【免费下载链接】headroomCompress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.项目地址: https://gitcode.com/GitHub_Trending/head/headroom如果你想知道 Headroom 对你的一组消息实际能压缩掉多少 token、会应用哪些压缩变换但又不想把消息真正发给 LLM 产生费用和延迟可以用simulate模拟调用。它在本地跑完整个变换管线返回压缩后的消息、节省的 token 数和浪费信号分析全程不调用 LLM APISimulation 文档。本文基于 Headroom Python SDK走通从安装、构造消息、执行模拟到读取结果的完整路径最后给出 TypeScript SDK 的等价做法。安装 Python SDK按 Quickstart 给出的方式安装# Python 项目或 virtualenv pip install headroom-ai[all] # 或者作为独立 CLI 工具安装CLI/proxy/wrap 场景 uv tool install --python 3.13 headroom-ai[all]准备一组消息并执行第一次模拟先用一组 OpenAI 格式的消息作为输入。下面沿用 Quickstart 文档中的示例消息一个系统提示、一条用户消息、一条带tool_calls的 assistant 消息、一条包含 500 条搜索结果 JSON 数组的tool输出以及一条收尾用户消息。工具输出是 Headroom 压缩收益的主要来源所以示例特意放大了这个部分from openai import OpenAI from headroom import HeadroomClient, OpenAIProvider import json messages [ {role: system, content: You analyze search results.}, {role: user, content: Search for Python tutorials.}, { role: assistant, content: None, tool_calls: [{ id: call_1, type: function, function: {name: search, arguments: {q: python}}, }], }, { role: tool, tool_call_id: call_1, content: json.dumps({ results: [ {title: fResult {i}, snippet: fDescription {i}, score: 100 - i} for i in range(500) ] }), }, {role: user, content: What are the top 3 results?}, ] client HeadroomClient( original_clientOpenAI(), providerOpenAIProvider(), ) plan client.chat.completions.simulate( modelgpt-4o, messagesmessages, )client.chat.completions.simulate()的签名见 API Reference实现位于 ChatCompletions.simulate。model用于 token 计数与上下文限制messages为要预览的会话headroom_mode默认值为optimize其余透传的参数会被忽略。读取 SimulationResultsimulate返回SimulationResult字段定义见 config.py字段含义tokens_before/tokens_after/tokens_saved压缩前后 token 数与节省量transforms本次应用的变换列表list[str]estimated_savings人类可读的费用估算messages_optimized压缩后的完整消息列表block_breakdown按块类型统计的 token 分布dict[str, int]waste_signals各类 token 浪费来源dict[str, int]stable_prefix_hash/cache_alignment_score前缀哈希与缓存对齐评分基本输出print(fTokens before: {plan.tokens_before}) print(fTokens after: {plan.tokens_after}) print(fWould save: {plan.tokens_saved} tokens ({plan.tokens_saved/plan.tokens_before*100:.1f}%)) print(fTransforms: {plan.transforms})Quickstart 中展示的示例输出文档示例数值会随你的消息内容变化Tokens before: 45000 Tokens after: 4500 Tokens saved: 40500 Compression: 90% Transforms: [smart_crusher, cache_aligner]用 waste_signals 定位浪费来源waste_signals告诉你输入里哪些部分贡献了最多的不必要 tokenwaste plan.waste_signals # dict[str, int] print(fJSON bloat: {waste[json_bloat]} tokens) print(fHTML noise: {waste[html_noise]} tokens) print(fWhitespace: {waste[whitespace]} tokens) print(fDynamic dates: {waste[dynamic_date]} tokens) print(fRepetition: {waste[repetition]} tokens)用 block_breakdown 看 token 集中在哪类消息解析器会把会话拆成块block_breakdown给出每类块的 token 数。块类型如下Simulation 文档块类型说明system系统提示user用户消息assistant模型回复tool_call函数调用请求tool_result工具输出最大的浪费来源rag检索文档上下文没有压缩时先查什么如果plan.tokens_saved 0文档给出的排查方向是Simulation 文档if plan.tokens_saved 0: print(No compression applied. Possible reasons:) print(- Messages are too short ( 200 tokens per tool output)) print(- No tool outputs with compressible JSON arrays) print(- Content is already compact (code, grep results)) else: print(fTransforms applied: {plan.transforms}) print(json.dumps(plan.messages_optimized, indent2))即依次对照单条工具输出是否短于 200 token、是否存在可压缩的 JSON 数组合成工具输出、内容是否本来就紧凑代码、grep 结果。有压缩时plan.messages_optimized可以直接查看压缩后的消息长什么样。对一组样本估算整体节省在正式开启压缩前可以用simulate对一份代表性工作负载做批量估算import json total_before 0 total_after 0 for messages in sample_conversations: plan client.chat.completions.simulate( modelgpt-4o, messagesmessages, ) total_before plan.tokens_before total_after plan.tokens_after savings_pct (1 - total_after / total_before) * 100 print(fEstimated savings: {savings_pct:.1f}%) print(fTokens saved: {total_before - total_after:,})其中sample_conversations是你自己准备的消息样本列表。实际节省取决于内容冗余程度Quickstart 指出 savings depend heavily on how repetitive the content is所以估算结果只对所用样本有效。可选对比不同的压缩配置如果想在同一组消息上比较不同配置的压缩强度可以为每个配置单独建一个客户端再模拟from headroom import HeadroomClient, HeadroomConfig, OpenAIProvider from headroom.transforms import SmartCrusherConfig configs [ SmartCrusherConfig(max_items_after_crush10), SmartCrusherConfig(max_items_after_crush25), SmartCrusherConfig(max_items_after_crush50), ] for smart_crusher_config in configs: client HeadroomClient( original_clientOpenAI(), providerOpenAIProvider(), configHeadroomConfig(smart_crushersmart_crusher_config), ) plan client.chat.completions.simulate(modelgpt-4o, messagesmessages) print(fmax_items{smart_crusher_config.max_items_after_crush}: f{plan.tokens_saved} tokens saved ({plan.tokens_saved/plan.tokens_before*100:.1f}%))这条路径只服务于「找适合自己负载的参数」日常预览不需要。TypeScript SDK 等价做法TypeScript 侧没有simulate方法但compress()返回相同的结果结构直接不把它发给 LLM 就是模拟Simulation 文档import { compress } from headroom-ai; const result await compress(messages, { model: gpt-4o, baseUrl: http://localhost:8787, }); console.log(Would save: ${result.tokensSaved} tokens); console.log(Compression ratio: ${(result.compressionRatio * 100).toFixed(1)}%); console.log(Transforms: ${result.transformsApplied.join(, )});注意前提TypeScript SDK 是通过本地 Headroom proxy 执行压缩管线的必须先启动 proxyQuickstartuv tool install --python 3.13 headroom-ai[proxy] # 或者在 Python 项目内pip install headroom-ai[proxy] headroom proxy --port 8787限制说明模拟永远不会调用 LLM API它在本地运行完整的变换管线并返回结果因此没有 provider 侧的费用和延迟Simulation 文档 的说明。文档中出现的45000 - 4500、90%等均为文档示例数值不是你输入必然得到的结果不同消息的节省比例差异很大。simulate只接受model、messages以及headroom_mode、headroom_output_buffer_tokens、headroom_tool_profiles这几个 Headroom 参数额外传的参数会被忽略见 simulate 实现。想进一步了解压缩管线内部如何工作可以阅读仓库中的 How Compression Works 与 Configuration。【免费下载链接】headroomCompress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.项目地址: https://gitcode.com/GitHub_Trending/head/headroom创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

用AI辅助论文会被华宸AI智评检测出来吗?AI率超标怎么降下来? 2026/9/9 22:55:32

用AI辅助论文会被华宸AI智评检测出来吗?AI率超标怎么降下来?

用AI辅助论文会被华宸AI智评检测出来吗?AI率超标怎么降下来? 先直接回答标题里的两个问题。会不会被检测出来:会。华宸AI智评一次检测出三份结果,第三份就是AIGC检测,官网写的是识别ChatGPT、DeepSeek、豆包等大模型痕…

阅读更多 →
Drawio Mermaid插件:实现文本图表与可视化画布的深度整合 2026/9/9 22:55:32

Drawio Mermaid插件:实现文本图表与可视化画布的深度整合

简介:drawio_mermaid_plugin 是一款面向 Drawio 桌面版用户的 Mermaid 图生成插件,可将饼状图、顺序图、甘特图、状态图、流程图、类图等以简单标记语言直接绘制到画布中,适用于需要快速产出技术图表的产品、研发与文档协作场景。压缩包共 45…

阅读更多 →
CactiEZ 10.1实战:为华为H3C中兴交换机自制监控模板 2026/9/9 22:55:32

CactiEZ 10.1实战:为华为H3C中兴交换机自制监控模板

简介:面向使用 CactiEZ 10.1 监控平台的网络运维人员,这份主机模板包针对华为、中兴、H3C 等常见网络设备,补齐了 SNMP 监控模板缺失的痛点。模板覆盖面较广,除盒式交换机、核心交换机外,还包含路由器、UPS、网络打印机…

阅读更多 →
aider 终端 AI 结对编程实用技巧:会话文件管理、任务拆解与排错工作流指南 2026/9/9 22:55:32

aider 终端 AI 结对编程实用技巧:会话文件管理、任务拆解与排错工作流指南

aider 终端 AI 结对编程实用技巧:会话文件管理、任务拆解与排错工作流指南 【免费下载链接】aider aider is AI pair programming in your terminal 项目地址: https://gitcode.com/GitHub_Trending/ai/aider 本指南面向已经能启动 aider 基本会话、希望显著提…

阅读更多 →
如何把 AI 编程工具接入 Coolify 的 MCP 服务器? 2026/9/9 22:55:32

如何把 AI 编程工具接入 Coolify 的 MCP 服务器?

如何把 AI 编程工具接入 Coolify 的 MCP 服务器? 【免费下载链接】coolify An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 280 one-click s…

阅读更多 →
Angular Agent Skills 指南:让 Coding Agent 按最新最佳实践编写与搭建 Angular 应用 2026/9/9 22:52:32

Angular Agent Skills 指南:让 Coding Agent 按最新最佳实践编写与搭建 Angular 应用

Angular Agent Skills 指南:让 Coding Agent 按最新最佳实践编写与搭建 Angular 应用 【免费下载链接】angular Deliver web apps with confidence 🚀 项目地址: https://gitcode.com/GitHub_Trending/an/angular 本篇技术指南围绕当前仓库 skill…

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞