Abstract
Large Language Models (LLMs) increasingly serve as agents interacting with external tools via API calls. However, ensuring these calls are both syntactically correct and faithful to specific API documentation remains a significant challenge. We present FANTASE (FANTAstic SEquences), a framework that improves API call generation by combining State-tracked Constrained Decoding (SCD) with a lightweight discriminator-guided reranker. FANTASE ensures 100% faithfulness to API schemas while simultaneously improving inference speed by up to 2.4x, effectively bridging the gap between accuracy and computational efficiency.
1. Introduction
While LLMs demonstrate remarkable conversational capabilities, their performance as tool-using agents is often hampered by a "faithfulness gap." To successfully execute an API call, a model must adhere to strict syntactic rules, including correct function names, required arguments, and valid enumerated values. Current solutions typically rely on In-Context Learning (ICL), which consumes valuable context window space, or Supervised Fine-Tuning (SFT), which is computationally expensive and difficult to scale as APIs evolve.
Even when an LLM possesses the knowledge to generate a correct call, the valid sequence may not be the model's highest-probability output. Furthermore, the generation process is often slowed by the model's tendency to hallucinate parameters or get distracted by conversational history. FANTASE addresses these issues by moving optimization to the output phase, using structural constraints to guide the model toward valid sequences.
2. The FANTASE Framework
FANTASE introduces a two-pillar approach to API generation: State-tracked Constrained Decoding (SCD) and Discriminator-Guided Reranking.
2.1 State-tracked Constrained Decoding (SCD)
SCD restricts the LLM's output space to ensure every generated token contributes to a valid API call. This is achieved through a Constrained Token Search Trie (CTST) that maps the API documentation into a searchable state machine.
As the model generates a call (e.g., Restaurants_1.FindTable(...)), the system tracks the current state—identifying whether the model is currently producing a package name, a function, or an argument. At each step, the trie filters the model's vocabulary, allowing only tokens that are valid according to the documentation.
A key innovation in SCD is the single-child optimization. If the trie indicates that only one specific token is valid at a given point (such as a period following a package name or a specific closing parenthesis), the system automatically appends that token. This bypasses the expensive GPU forward pass required for standard token generation, resulting in significant speedups.
2.2 Discriminator-Guided Reranking
Because the most probable sequence from an LLM is not always the most contextually accurate, FANTASE employs a lightweight RoBERTa-base model (125M parameters) as a reranker. This discriminator evaluates the conversation context against the top-10 candidate API calls generated by the LLM. By scoring these candidates, the reranker can identify the correct call even if it was originally ranked lower by the primary LLM.
3. Experimental Evaluation
3.1 Datasets and Setup
The framework was evaluated on two primary benchmarks:
- DSTC8 (Dialogue System Technology Challenge 8): Multi-turn dialogues across domains like hotels and flights, requiring complex argument handling.
- API Bank: A benchmark focused on the planning and execution of tool-augmented tasks.
Experiments were conducted using Alpaca-7B and 13B models to demonstrate the framework's impact on open-source LLMs.
3.2 Results: Accuracy and Faithfulness
On the API Bank dataset, the application of SCD alone improved zero-shot accuracy from 24.06% to 56.64%. When combined with the RoBERTa reranker, accuracy reached 64.41%, outperforming much larger models like GPT-4 in specific settings. By design, SCD guarantees 100% structural faithfulness, eliminating common errors like missing commas or hallucinated function names.
3.3 Results: Efficiency and Prompt Compression
Unlike traditional constrained decoding, which often introduces overhead, FANTASE improves performance. On the DSTC8 dataset, SCD reduced average inference time per sample from 15.12 seconds to 6.33 seconds.
Furthermore, because the SCD component encodes API rules within the trie, the need for extensive documentation in the prompt is reduced. The authors found that SCD maintains high accuracy even with zero documentation in the prompt, allowing for significant prompt compression and saving hundreds of tokens per request.
4. Discussion and Key Insights
- Constraints as an Accelerator: The study proves that structural constraints can be a performance feature rather than a burden. By skipping redundant computations for deterministic tokens, FANTASE achieves a 1.5x to 2.4x speedup.
- Hybrid Intelligence: The success of the RoBERTa reranker demonstrates that small, specialized models can effectively supervise much larger general-purpose models, providing a cost-effective way to boost agentic performance.
- Prompt Efficiency: The ability to generate accurate calls without including documentation in the prompt suggests a new path for managing long-context interactions in tool-use scenarios.
5. Conclusion and Future Work
FANTASE provides a robust framework for faithful and efficient API call generation. While currently optimized for Python-like API formats, future work could extend this state-tracked approach to JSON, SQL, or general reasoning tasks. While the benefits are most pronounced in 7B and 13B parameter models, the efficiency gains and faithfulness guarantees remain highly relevant for the deployment of LLM agents at scale.
Summary updated