Mitigating Errors in LLM-Generated Web API Invocations via Retrieval-Augmented Generation and Constrained Decoding
This paper proposes and evaluates Retrieval-Augmented Generation (RAG) and Constrained Decoding (CD) as complementary strategies to improve the correctness of LLM-generated web API invocations, finding that while RAG effectively reduces hallucinations in full invocation generation, CD provides more reliable prevention of illegal parameters and significantly boosts overall correctness across different scenarios.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
Imagine you are trying to build a house, but instead of hiring a master architect, you ask a very smart, well-read, but slightly scatterbrained assistant (the Large Language Model, or LLM) to write the blueprints for you.
The problem is that the "rules" for building this house aren't inside the assistant's head. They are written in a massive, complex manual called an OpenAPI Specification (a document that tells you exactly which doors to open, which keys to use, and what size bricks to order).
If you just ask the assistant to "build a door," it might guess the wrong size, use the wrong key, or invent a door that doesn't exist in the manual. This is called hallucination.
This paper is about giving that assistant two specific tools to stop it from making up rules and start following the manual perfectly.
The Problem: The "Guessing Game"
The researchers found that when they asked AI models to write code to connect to web services (like sending a message to Slack or checking a Google Calendar), the AI made mistakes constantly.
- It would pick the wrong "door" (endpoint).
- It would try to use a key that didn't exist.
- It would invent features that weren't in the manual.
The Solution: Two New Tools
The paper tests two different ways to fix this, using a dataset of real-world coding tasks.
Tool 1: The "Cheat Sheet" (Retrieval-Augmented Generation - RAG)
The Analogy: Imagine you are taking a test, but you are allowed to bring a specific page from the textbook into the room. Before the AI writes its answer, a robot librarian (the Retriever) looks at the massive manual, finds the exact page about the "Door" you need, and hands it to the AI.
- How it worked: The researchers built a system that grabs the relevant part of the API manual and pastes it into the AI's prompt.
- The Result: It was a mixed bag.
- Good: When the AI didn't know which door to pick, the cheat sheet helped it find the right one. It stopped the AI from inventing fake doors.
- Bad: When the AI already knew which door to pick, the cheat sheet sometimes confused it. The AI saw a list of 20 possible keys in the cheat sheet and thought, "I should probably use all of them!" even though the task only needed one. This made the code messy and incorrect.
- Verdict: It helps with finding the right path, but it can make the AI "over-eager" to use extra options it doesn't need.
Tool 2: The "Train Tracks" (Constrained Decoding - CD)
The Analogy: Imagine the AI is a train. Usually, the train can go anywhere on the map, even off the tracks into a swamp (hallucinations). Constrained Decoding is like laying down steel train tracks that only go to valid destinations. The train physically cannot leave the tracks.
- How it worked: The researchers took the manual and turned it into a set of strict rules (like a maze). As the AI tried to type the next word of code, the system checked: "Is this word allowed by the rules?" If the AI tried to type a fake door or a wrong key, the system blocked it and forced the AI to pick a valid option.
- The Result: This was the clear winner.
- Zero Hallucinations: The AI could not invent a single fake URL, method, or argument. It was mathematically impossible for it to break the rules.
- Better Accuracy: Because it couldn't make up things, the code it wrote was much more likely to be correct.
- Verdict: This is the most reliable tool. It forces the AI to be obedient to the manual.
The Combo: "Cheat Sheet" + "Train Tracks"
The researchers tried using both tools at once.
- The Result: It worked very well for some models, giving the highest scores. However, it wasn't consistent. Sometimes the "Cheat Sheet" made the AI try to use too many options, and even though the "Train Tracks" stopped it from making illegal moves, it still made unnecessary moves.
- Verdict: It's powerful, but the "Train Tracks" alone are safer and more consistent.
The Bottom Line
The paper concludes that while AI is great at writing code, it's terrible at following complex, external rulebooks on its own.
- RAG (The Cheat Sheet) is like giving the AI a reference book. It helps, but the AI might get distracted by too much information.
- CD (The Train Tracks) is like building a cage around the AI that only allows valid moves. It is the most effective way to stop the AI from making up rules.
The researchers say that if you want an AI to write code that connects to web services without breaking, you shouldn't just rely on the AI's memory. You need to either give it the right manual (RAG) or, even better, force it to follow the rules strictly (CD). The "Train Tracks" approach is the most reliable way to ensure the code actually works.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.