{"name":"tooltrim: Drastically Reduce LLM Agent Tool Output Tokens, Improve Accuracy","description":"tooltrim provides drop-in compression for LLM agent tool outputs, drastically cutting tokens while often improving answer accuracy. This provider-agnostic solution offers content-aware compression, faithfulness benchmarks, and seamless integration with popular frameworks or as an OpenAI-compatible proxy.","github":"https://github.com/nac7/tooltrim","url":"https://osrepos.com/repo/nac7-tooltrim","source":"osrepos.com","sourceDescription":"This repository profile is provided by osrepos.com, an open source repository discovery platform.","repositoryProfile":"https://osrepos.com/repo/nac7-tooltrim","generatedFor":"open source discovery and AI-assisted research","markdown":"https://osrepos.com/repo/nac7-tooltrim.md","json":"https://osrepos.com/repo/nac7-tooltrim.json","topics":["Python","LLM Agents","Context Compression","Token Optimization","Prompt Engineering","Tool Use","AI","RAG"],"keywords":["Python","LLM Agents","Context Compression","Token Optimization","Prompt Engineering","Tool Use","AI","RAG"],"stars":null,"summary":"tooltrim provides drop-in compression for LLM agent tool outputs, drastically cutting tokens while often improving answer accuracy. This provider-agnostic solution offers content-aware compression, faithfulness benchmarks, and seamless integration with popular frameworks or as an OpenAI-compatible proxy.","content":"## Introduction\n\ntooltrim is an innovative Python library designed to address the significant challenge of bloated tool outputs in Large Language Model (LLM) agent contexts. It offers a drop-in solution to compress tool results, such as fetched web pages, JSON responses, or log dumps, before they consume valuable context window space. By reducing token counts by up to 99% while maintaining, and often improving, answer accuracy, tooltrim helps agents operate more efficiently and cost-effectively.\n\n## Why Use tooltrim and Its Advantages\n\nLLM agents frequently encounter tool outputs that are excessively verbose, leading to increased token usage, slower responses, and potential distraction for the model. Traditional solutions like prompt compressors or semantic caches do not target this specific issue. tooltrim directly tackles this problem by compressing tool outputs at the point they enter the agent's context.\n\nKey advantages include:\n\n*   **Massive Token Savings**: Benchmarks show reductions from tens of thousands of tokens to hundreds, resulting in a 35.6x smaller context.\n*   **Accuracy Improvement**: For smaller models, compression doesn't just preserve accuracy, it can significantly improve it by removing noise and distractions.\n*   **Provider-Agnostic**: Works with any LLM provider or framework, as it compresses strings, not APIs.\n*   **Content-Aware Compression**: Utilizes specific strategies for HTML, JSON, tabular data, logs, and free text, ensuring intelligent and relevant extraction.\n*   **Lossless by Reference**: Full original outputs are stashed and retrievable via a short reference, allowing agents to expand content on demand.\n*   **Faithfulness-Tested**: Includes a built-in benchmark to measure whether models maintain correct answers on compressed output, not just token savings.\n*   **Proxy Deployment**: An OpenAI-compatible proxy allows for zero-code integration, trimming `role:\"tool\"` messages in flight by simply changing a `base_url`.\n*   **Framework Integrations**: Seamlessly integrates with LangChain, LlamaIndex, and OpenAI Agents SDK with minimal code changes.\n\n## Installation\n\nInstallation is straightforward:\n\nbash\npip install tooltrim          # zero-dependency core (heuristic token counts)\npip install tooltrim[tokens]  # add tiktoken for exact token counts\n\n\nOptional extras for specific integrations include `tooltrim[langchain]`, `tooltrim[redis]`, `tooltrim[s3]`, `tooltrim[mcp]`, and `tooltrim[embeddings]`.\n\n## Examples\n\ntooltrim offers flexible usage patterns:\n\n**1. Decorate a tool:**\nWrap your existing functions to automatically compress their outputs.\n\npython\nfrom tooltrim import compressed_tool\n\n@compressed_tool(max_tokens=400)\ndef read_file(path: str) -> str:\n    return open(path).read()\n\n\n**2. Make it query-aware:**\nProvide a relevance query to guide the compression, ensuring critical information is retained.\n\npython\nfrom tooltrim import query_scope\n\nwith query_scope(\"find the customer's refund status\"):\n    result = run_agent_step()   # all @compressed_tool calls inside use this query\n\n\n**3. Imperative API + expand-on-demand:**\nFor fine-grained control, use the `ToolCompressor` directly and allow agents to retrieve full outputs when needed.\n\npython\nfrom tooltrim import ToolCompressor\n\ntc = ToolCompressor(max_tokens=400)\nres = tc.compress(huge_json_response, query=\"refund status for customer C-1007\")\n\n# ... later, if the agent needs the full context ...\nfull = tc.expand(res.ref)\n\n\n**4. Framework Integrations:**\nIntegrate with popular LLM frameworks like LangChain, LlamaIndex, or OpenAI Agents SDK with simple wrappers.\n\npython\n# LangChain example\nfrom tooltrim.integrations import compress_langchain_tool\n\nfetch = compress_langchain_tool(my_tool, max_tokens=400,\n                                query_from=lambda query, **_: query)\n\n\n**5. Run as a Proxy:**\nDeploy tooltrim as a proxy to compress tool results for any OpenAI or Anthropic client with zero code changes, just by updating the `base_url`.\n\nbash\npython run_proxy.py --upstream https://api.openai.com/v1\n\n\npython\nfrom openai import OpenAI\nclient = OpenAI(base_url=\"http://127.0.0.1:8800/v1\", api_key=\"<upstream key>\")\n\n\n## Links\n\nFor more details, benchmarks, and advanced usage, visit the official repository:\n*   [GitHub Repository](https://github.com/nac7/tooltrim)\n*   [PyPI](https://pypi.org/project/tooltrim)","metrics":{"detailViews":2,"githubClicks":0},"dates":{"published":null,"modified":"2026-09-16T08:38:35.000Z"}}