Introduction
As Large Language Models (LLMs) transition into autonomous agents, they must select and execute external tools (APIs) to solve complex, real-world problems. ToolWeaver introduces a novel framework that moves beyond the inefficient "one-token-per-tool" paradigm by representing thousands of APIs as hierarchical, compositional codes. By weaving together a tool’s functional description with its real-world usage patterns, ToolWeaver enables LLMs to scale to massive tool libraries while actually improving their ability to coordinate multiple tools for complex tasks.
The "One-Token-per-Tool" Crisis
Current approaches to tool use generally fall into two camps, both with significant flaws:
- Retrieval-based methods: These rely on external search engines to find tools. They are often clunky, add latency, and use encoders that struggle to fully capture the LLM's complex semantic needs.
- Generative methods: These teach the LLM to generate a specific "ID token" for a tool (e.g.,
<Weather_API>).
While elegant, the generative approach faces a severe scalability and semantic crisis. If a system has 50,000 tools, it requires adding 50,000 new tokens to the model's vocabulary. This "one-token-per-tool" strategy causes the vocabulary to explode, hogging memory and destroying the model's original language abilities—a phenomenon known as catastrophic degradation. Furthermore, these tokens are semantically isolated; the model has no inherent way to know that a "Weather" tool and an "Air Quality" tool are related unless it sees them together thousands of times in sparse training data.
The ToolWeaver Framework
To address these limitations, the authors introduce ToolWeaver, a framework that rethinks tool representation as a compositional sequence of codes rather than a single atomic token.
Collaborative-Aware Tokenization
Instead of assigning a random ID, ToolWeaver uses a multi-stage process to "tokenize" a tool:
- Intrinsic Semantics: It starts with the tool's documentation to create a base embedding.
- Extrinsic Collaboration: It builds a similarity matrix based on how often tools are used together in real-world trajectories.
- Residual Quantization (RQ-VAE): It compresses these embeddings into a hierarchical code (e.g., Tool A becomes
<C1_5><C2_12>). - The "Weaving" Process: Crucially, the training objective includes a Graph Laplacian regularization term. This ensures that tools that are functionally related or frequently used together share "parent" codes (the first parts of the sequence).
Generative Alignment and Inference
Once the codes are generated, the model undergoes two-stage fine-tuning. First, it learns to map a user query to the correct tool code sequence (Retrieval). Second, it learns to generate full reasoning paths, tool calls using the new codes, and final answers (Trajectory). During inference, the model employs a prefix tree (trie) as a guardrail, ensuring the LLM only generates valid code sequences that correspond to existing tools in the library.
Key Insights
- Logarithmic Scaling: By using a sequence of codes (e.g., 2 layers of 1024 tokens), ToolWeaver can represent over a million tools ($1024^2$) while only adding 2,048 tokens to the vocabulary. This makes the system incredibly memory-efficient.
- The Shared Parent Advantage: Because related tools share the first part of their code sequence, the model receives a dense collaborative signal. If it learns that
<C1_5>is related to "outdoor activities," it automatically gets a head start on every tool that begins with that code. - Preserving the "Brain": Unlike prior generative methods that pollute the LLM's vocabulary with tens of thousands of random tokens, ToolWeaver’s small vocabulary footprint allows the model to retain its general reasoning and language skills.
Performance and Results
The framework was evaluated on ToolBench, a massive dataset containing 46,985 unique APIs across 49 categories, using 641 complex user queries.
- Superior Retrieval: ToolWeaver outperformed state-of-the-art models like ToolGen across all complexities. In the most difficult "I3" scenarios (orchestrating tools across different categories), it achieved an NDCG@1 of 88.00, significantly beating ToolGen’s 81.00.
- End-to-End Success: In task completion (Solvable Pass Rate), ToolWeaver showed a massive lead in complex planning, nearly doubling the performance of some baselines in multi-tool scenarios.
- Language Preservation: To test for catastrophic degradation, models were evaluated on standard NLP benchmarks. While ToolGen’s perplexity on natural language (WikiText-2) exploded to 104.54 (indicating it "forgot" how to speak naturally), ToolWeaver stayed at a much healthier 25.36, nearly matching the base model's performance on summarization tasks.
Limitations and Future Work
While highly effective, ToolWeaver's collaborative tokenization relies on having existing trajectories of tools being used together. For entirely new toolsets with no usage history, the model must rely solely on functional descriptions. Additionally, the current implementation uses a fixed number of hierarchical levels. Future work could explore dynamic code lengths for even more flexible scaling, as well as reinforcement learning to allow the agent to autonomously discover new collaborative patterns between tools.
Summary updated