Learn how LLMs use function calling and tool use to interact with real-world APIs. A practical guide for developers building AI-powered applications.
Large language models are remarkably good at reasoning, summarizing, and generating text.
But they have a fundamental limitation: they cannot interact with the real world on their own.
They cannot check the current price of a stock. They cannot send an email. They cannot look up a user in your database.
Function calling solves this problem.
Function calling (also called tool use) is a mechanism that allows LLMs to request external actions — and then receive the result — as part of a conversation.
Instead of just generating text, the model can say:
"I need to call this API to answer the user's question."
The application executes the call and returns the result back to the model, which then uses that data to generate a final response.
Before function calling existed, developers had to work around LLM limitations using prompt engineering tricks — injecting real-time data manually into prompts, building fragile parsing logic, and hoping the model would use the data correctly.
Function calling changed everything.
It introduced a structured, reliable way for models to:
This is why function calling is considered one of the most important advances in applied AI since the release of GPT-4.
The process follows a clear pattern:
You describe the available functions to the model — their names, parameters, and purpose. This is done using a JSON schema format.
{ "name": "get_currency_exchange_rate", "description": "Returns the current exchange rate between two currencies.", "parameters": { "type": "object", "properties": { "from": { "type": "string", "description": "Source currency code, e.g. USD" }, "to": { "type": "string", "description": "Target currency code, e.g. EUR" } }, "required": ["from", "to"] } }
When a user asks a question that requires external data, the model responds with a structured function call instead of a plain text answer.
{ "function_call": { "name": "get_currency_exchange_rate", "arguments": { "from": "USD", "to": "EUR" } } }
Your code intercepts this response, calls the actual API, and retrieves the result.
You pass the API response back to the model as a function result. The model then uses this real data to generate an accurate, grounded answer.
The terms are often used interchangeably, but there is a subtle distinction.
Function calling refers specifically to the mechanism popularized by OpenAI, where the model outputs a structured call to a named function.
Tool use is the broader concept used by Anthropic (Claude) and others. Instead of functions, the model has access to a set of "tools" — which can include APIs, code execution, web search, and more.
In practice, both work on the same principle:
A support agent AI can call your database API to look up order status, then provide a real answer instead of a generic response.
An AI assistant can call currency exchange APIs, stock price APIs, and tax calculation APIs — all in a single conversation — to answer complex financial questions.
AI coding assistants can call APIs to validate IP addresses, check IBAN numbers, or verify email formats in real time while helping developers write integrations.
Multi-step AI agents use tool use to chain dozens of API calls — planning, executing, evaluating results, and adapting based on responses.
The model decides whether to call a function based entirely on its description. Vague descriptions lead to wrong decisions.
❌ Bad "description": "Gets data" ✅ Good "description": "Returns the real-time exchange rate between two currencies using ISO 4217 currency codes."
Avoid deeply nested parameter structures. LLMs work best with flat, clearly named parameters.
When your API responds, return clean, structured JSON. Unstructured or verbose responses are harder for the model to interpret accurately.
If an API call fails, return a clear error message in the function result. The model can then decide how to handle the failure — retry, ask the user, or use an alternative approach.
Modern LLMs support calling multiple functions in a single response — in parallel.
Instead of:
The model can request both simultaneously, dramatically reducing latency in multi-step workflows.
This is particularly powerful for:
Function calling introduces real risks that developers must address.
Malicious content in API responses can attempt to hijack the model's behavior. Always sanitize external data before passing it back to the model.
Only expose functions the model genuinely needs. Do not give an AI agent access to delete or modify operations unless absolutely required.
AI agents can trigger significantly more API calls than traditional applications. Design your architecture with rate limits and cost controls in mind from the start.
Never embed API keys in function descriptions or prompt text. Handle authentication server-side, outside the model's context.
Not all APIs are equally suited for LLM tool use.
The best APIs for function calling share these characteristics:
Platforms like AnyAPI provide a collection of well-structured REST APIs — for currency exchange, IP geolocation, IBAN validation, email verification, and more — that integrate cleanly into LLM tool use workflows without complex setup.
Here is what a complete function calling loop looks like in Python using the Anthropic SDK:
import anthropic import requests client = anthropic.Anthropic() tools = [ { "name": "get_exchange_rate", "description": "Returns the current exchange rate between two currencies.", "input_schema": { "type": "object", "properties": { "from_currency": {"type": "string"}, "to_currency": {"type": "string"} }, "required": ["from_currency", "to_currency"] } } ] def call_exchange_api(from_currency, to_currency): response = requests.get( "https://anyapi.io/api/v1/exchange/rate", params={"from": from_currency, "to": to_currency}, headers={"Authorization": "Bearer YOUR_API_KEY"} ) return response.json() messages = [{"role": "user", "content": "What is the current USD to EUR exchange rate?"}] response = client.messages.create( model="claude-sonnet-4-6", max_tokens=1024, tools=tools, messages=messages ) if response.stop_reason == "tool_use": tool_use = next(b for b in response.content if b.type == "tool_use") result = call_exchange_api( tool_use.input["from_currency"], tool_use.input["to_currency"] ) messages.append({"role": "assistant", "content": response.content}) messages.append({ "role": "user", "content": [{"type": "tool_result", "tool_use_id": tool_use.id, "content": str(result)}] }) final_response = client.messages.create( model="claude-sonnet-4-6", max_tokens=1024, tools=tools, messages=messages ) print(final_response.content[0].text)
Function calling and tool use are rapidly evolving.
The direction the industry is moving toward:
APIs are becoming the connective tissue of AI applications. Developers who understand how to design and integrate APIs for LLM tool use will have a significant advantage as this ecosystem matures.
Function calling transforms LLMs from isolated text generators into connected systems that can interact with the real world.
The pattern is straightforward:
When combined with well-designed APIs, tool use enables AI applications that are genuinely useful — grounded in real data, capable of real actions, and reliable enough to deploy in production.
The era of AI systems that just generate text is ending. The era of AI systems that act through APIs is here.
gRPC vs REST - Which API Protocol Should You Choose?
gRPC and REST are two of the most popular API protocols today. Learn the key differences in performance, streaming, and use cases to pick the right one for your project.
Vibe Coding and APIs What Developers Need to Know in 2026
Vibe coding is changing how developers integrate APIs. Learn what AI-generated API code gets right, where it fails, and how to ship production-safe integrations faster.
MCP Protocol and API Integration The Complete Developer Guide for 2026
MCP (Model Context Protocol) is reshaping how APIs are consumed...