- Home
- AI & Machine Learning
- Tool Use with LLMs: A Guide to Function Calling and External APIs
Tool Use with LLMs: A Guide to Function Calling and External APIs
You know the frustration. You ask a Large Language Model (LLM) for today's stock price or the current weather in Asheville, and it confidently gives you an answer from two years ago. It sounds smart, but it’s hallucinating because its knowledge is frozen in time. This is where Function Calling comes in. It’s not magic; it’s a structured way for models to talk to your code. Instead of guessing, the model pauses, asks your application to run a specific function, gets the real data back, and then finishes its sentence. If you’re building AI applications that need to do more than just chat-like checking inventory, booking flights, or querying databases-you need to understand how this bridge between language and logic works.
| Core Mechanism | The LLM outputs JSON instructions, not code execution. Your app runs the code. |
| Main Benefit | Real-time data access and reduced hallucinations by up to 68% for factual queries. |
| Biggest Pitfall | Poorly defined schemas lead to parameter errors and silent failures. |
| Current Standard | JSON Schema validation is required by major providers like OpenAI and Anthropic. |
What Actually Happens When an LLM Calls a Function?
Let’s clear up a common misconception right away. The LLM does not execute your Python script or hit your API endpoint itself. It doesn’t have hands. Think of the model as a very articulate consultant who knows what needs to be done but lacks the authority to do it. When you enable function calling, you give the model a menu of available actions. For example, you might define a function called `get_weather` that takes a `location` string as input.
Here is the sequence of events when a user asks, "Is it raining in Seattle?"
- Intent Recognition: The model analyzes the prompt and realizes its internal knowledge isn't enough. It looks at your provided function definitions.
- JSON Generation: Instead of answering directly, it generates a JSON object: `{"name": "get_weather", "arguments": {"location": "Seattle"}}`.
- Your Code Takes Over: Your backend intercepts this JSON, parses it, and actually calls the weather API.
- Data Return: The weather API returns the data (e.g., "Rainy, 55°F").
- Synthesis: You send this result back to the LLM. Now, with fresh context, the model writes the final response: "Yes, it is currently raining in Seattle."
This separation of concerns is crucial. It keeps the model safe from executing arbitrary code while allowing it to act as a reasoning engine that orchestrates complex workflows.
Defining the Contract: JSON Schemas and Parameters
The quality of your tool use depends entirely on how well you describe your functions. This description is called a Schema. If your schema is vague, the model will guess wrong. If it’s precise, the model succeeds. Major providers like OpenAI, Anthropic, and Google all rely on JSON Schema standards to validate these inputs.
Consider a simple function for searching a product database. A bad definition might look like this:
{"name": "search", "description": "Searches for stuff"}
This is terrible. What kind of stuff? By name? By ID? The model has no idea what arguments to provide.
A good definition uses strict typing and clear descriptions:
{
"name": "search_products",
"description": "Finds products in the inventory by category and max price.",
"parameters": {
"type": "object",
"properties": {
"category": {"type": "string", "description": "The product category, e.g., 'electronics'"},
"max_price": {"type": "number", "description": "The maximum price in USD"}
},
"required": ["category"]
}
}
Notice the specificity. We tell the model exactly what types are expected (`string`, `number`) and which fields are mandatory. According to recent industry benchmarks, strict schema validation reduces error rates significantly, though some developers find it frustratingly rigid compared to older, looser approaches. But remember: garbage in, garbage out. If you want reliable automation, you must be rigorous here.
Choosing Your Provider: OpenAI, Anthropic, or Google?
Not all function calling implementations are created equal. As of late 2025, the landscape has matured, but distinct differences remain in how each major provider handles tool integration. Your choice should depend on your specific needs: ecosystem size, accuracy requirements, or cost.
| Provider | Model Example | Strengths | Weaknesses |
|---|---|---|---|
| OpenAI | GPT-4 Turbo / GPT-5 | Largest ecosystem (2,400+ tools), high success rate on multi-step workflows (87%). | Strict validation can cause frequent retries; higher latency on complex chains. |
| Anthropic | Claude 3.5 Sonnet | Superior handling of ambiguous parameters (94% accuracy); flexible "tool_use" blocks. | Smaller third-party integration library (~850 tools). |
| Gemini 1.5 Pro | Excellent multi-turn refinement; reduces parameter errors by 38% via self-correction. | Slower response times due to extra reasoning steps; less community support. | |
| Alibaba | Qwen3 | "Thinking trace" transparency builds user trust; great for debugging. | Lower adoption in Western markets; fewer English-language resources. |
If you are building a consumer-facing app where natural conversation flow is paramount, Claude often feels smoother because it tolerates messy user inputs better. However, if you are integrating with a massive suite of existing SaaS tools, OpenAI's vast plugin repository makes life easier. Don't overlook Gemini if your workflow requires chaining multiple tools together; its ability to refine its own parameters after a failed attempt saves you from writing complex retry logic.
Building Reliable Workflows: Handling Errors and Loops
Once you get past the "hello world" phase, reality hits hard. Developers report spending 20-40 hours just debugging implementation issues. Why? Because things break. The API might timeout. The model might pick the wrong function. The user might ask something nonsensical.
You need robust error handling. Never assume a function call will succeed. Wrap your execution logic in try-catch blocks. If a function fails, don't just crash. Send the error message back to the LLM. Say, "The database query failed with error X. Please try again or inform the user." This allows the model to adapt. Sometimes, it will rephrase the query or choose a different tool.
Another major risk is the infinite loop. Imagine the model calls a function, gets a confusing result, tries again, gets the same result, and repeats forever. Most frameworks include a `max_turns` or `step_limit` parameter. Set this strictly. Limiting tool usage to 5-10 steps per conversation turn prevents runaway costs and keeps the user experience snappy. If the limit is reached, force the model to summarize what it has so far rather than continuing to fail silently.
Security Risks: The Injection Attack Surface
When you let an LLM decide what code to run, you open new doors for attackers. This isn't theoretical. Research indicates that nearly 37% of tested implementations are vulnerable to parameter injection attacks. In these scenarios, a malicious user crafts a prompt designed to trick the model into passing unexpected values to your function.
For example, if your function accepts a `user_id` string, an attacker might inject SQL-like syntax or special characters that your backend wasn't prepared for. Always treat the output of the LLM as untrusted input. Validate every argument against your business rules before executing the actual function. Sanitize strings, check integer ranges, and verify permissions. Just because the JSON format is correct doesn't mean the content is safe.
Future-Proofing Your Implementation
The field is moving fast. We are seeing a shift toward "adaptive validation," where models learn from their mistakes within a session. New features like "tool grounding" help cross-reference results with the model's internal knowledge to spot contradictions. Furthermore, regulatory pressures, such as the EU AI Act, now require transparent disclosure when AI accesses external systems. Users need to know when they are talking to a bot versus when the bot is fetching live data.
Keep your schemas modular. As new tools emerge, you shouldn't have to rewrite your entire prompt structure. Use standard naming conventions and keep descriptions updated. The goal is to create a system where adding a new capability is as simple as defining a new JSON block, not refactoring your core application logic.
Does the LLM execute the function itself?
No. The LLM only generates a JSON object specifying which function to call and with what arguments. Your application code is responsible for parsing this JSON, executing the actual function or API request, and sending the result back to the model.
Why is my function calling failing frequently?
Common causes include vague function descriptions, incorrect JSON schema definitions, or mismatched data types. Ensure your parameter names match exactly what you expect in your code and that descriptions clearly explain what each argument does. Ambiguity leads to hallucinated parameters.
Can I use multiple functions in one request?
Yes, most modern models support parallel function calling, where the model requests multiple independent functions simultaneously. However, sequential dependencies (where Function B needs the output of Function A) usually require separate turns or specialized chaining logic depending on the provider.
How do I handle timeouts or API errors?
Implement robust error handling in your backend. If a function fails, return a descriptive error message to the LLM instead of crashing. This allows the model to apologize to the user, retry, or suggest an alternative approach based on the error context.
Is function calling secure?
It introduces new risks like parameter injection. Since the LLM generates the arguments, you must validate them strictly in your backend code just as you would any other user input. Never trust the LLM's output blindly.
Susannah Greenwood
I'm a technical writer and AI content strategist based in Asheville, where I translate complex machine learning research into clear, useful stories for product teams and curious readers. I also consult on responsible AI guidelines and produce a weekly newsletter on practical AI workflows.
Popular Articles
1 Comments
Write a comment Cancel reply
About
EHGA is the Education Hub for Generative AI, offering clear guides, tutorials, and curated resources for learners and professionals. Explore ethical frameworks, governance insights, and best practices for responsible AI development and deployment. Stay updated with research summaries, tool reviews, and project-based learning paths. Build practical skills in prompt engineering, model evaluation, and MLOps for generative AI.
Technically speaking the model does not execute code it merely emits a structured token sequence that your application interprets as an instruction to invoke a specific function this distinction is critical for security architecture and sandboxing protocols.