What Is Function Calling in an LLM? A Clear Explainer
Function calling in an LLM is a mechanism that lets a language model request that a specific piece of external code be run, with structured arguments, instead of only generating free-text answers. You define a set of functions (also called "tools") with names, descriptions, and typed parameters. The model decides, based on the conversation, whether calling one of those functions would help answer the user's request, and if so, it outputs a structured request to call it — your application executes the actual function and feeds the result back to the model.
This solves a real problem: LLMs are trained on static data and can't natively check today's weather, query your database, place an order, or do arithmetic reliably. Function calling gives the model a way to say "I need this information" or "please perform this action" in a machine-readable format, so your code — not the model — does the actual work.
How Function Calling Actually Works
The flow is always a loop between your application and the model:
- You describe available functions to the model — name, description, and a JSON schema for the parameters.
- The user sends a message, e.g. "What's the shipping status for order 4521?"
- The model decides a function is needed and responds not with plain text, but with a structured call: the function name and arguments, like
{"name": "get_order_status", "arguments": {"order_id": "4521"}}. - Your application executes the function — hits your database, calls an API, runs a calculation — and gets a result.
- You send that result back to the model as part of the conversation.
- The model produces a final natural-language answer using the function's output.
The model never runs the code itself. It only produces the intent and arguments; execution, security, and error handling are entirely your responsibility. This separation is what makes function calling safe to use in production — you control exactly what code paths are reachable.
A Minimal Example
{
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string" },
"unit": { "type": "string", "enum": ["celsius", "fahrenheit"] }
},
"required": ["city"]
}
}
If a user asks "Is it cold in Berlin right now?", the model doesn't guess — it emits a call to get_weather with {"city": "Berlin"}. Your backend calls a real weather API, returns {"temp": 4, "unit": "celsius"}, and the model turns that into "Yes, it's about 4°C in Berlin right now."
Why This Matters for Real Applications
Without function calling, developers used to parse free-text model output with regex to guess intent — fragile and error-prone. Function calling makes the model's intent explicit and structured, which means:
- Reliable integrations: databases, CRMs, payment systems, internal APIs
- Multi-step reasoning: the model can chain several tool calls before answering
- Reduced hallucination: instead of inventing an order status, the model is forced to fetch the real one
- Deterministic execution: your code decides what's actually allowed to run
This is the foundation of most "AI agent" products — a loop of model call → tool call → tool result → model call, repeated until the task is done.
Common Use Cases
- Looking up live data (stock prices, order status, inventory)
- Performing calculations the model shouldn't do in its "head"
- Triggering actions (send email, create ticket, update record)
- Retrieving documents for grounded, cited answers
- Calling other AI models or specialized tools (image generation, code execution)
Building This Yourself vs. Using an API Layer
If you're calling a model provider's API directly, you typically define your tool schemas in every request, parse the model's structured tool-call output, execute it, and append the result to the conversation history yourself. This is well documented but adds real engineering overhead: handling streaming tool calls, retries, malformed arguments, and keeping conversation state consistent across turns.
If you already have Claude access and want a straightforward HTTPS API to build against — with application API keys, streaming responses, and tool-use support already wired up — SubToAPI turns that access into a clean API surface. You generate a sub_live_... key from the dashboard, and call it like any REST API:
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4",
"max_tokens": 1024,
"tools": [
{
"name": "get_weather",
"description": "Get current weather for a city",
"input_schema": {
"type": "object",
"properties": { "city": { "type": "string" } },
"required": ["city"]
}
}
],
"messages": [
{ "role": "user", "content": "Is it cold in Berlin right now?" }
]
}'
The response includes a tool-use block with the function name and arguments when the model decides a call is needed — you run the function, send the result back, and get a final answer. Full details are in the tool use docs, with request/response shapes in the messages docs and setup steps in the quickstart.
Getting Started
If you're new to function calling, start small: pick one function that solves a real gap (like fetching live data), write a tight schema with clear parameter descriptions, and test how the model behaves with ambiguous inputs. Vague descriptions are the most common source of wrong or missed tool calls — the model can only work with what you tell it.
You can try this against Claude through SubToAPI with a free trial, and compare plans — Solo, Team, and Scale — on the pricing page once you're ready to move past testing.
Questions
Is function calling the same as an AI agent? No. Function calling is the mechanism; an agent is a system that uses function calling in a loop, often across multiple steps, to complete a broader task autonomously.
Does the model execute the function itself? No. The model only outputs the function name and arguments as structured data. Your application runs the actual code and returns the result.
Do all LLM providers support function calling the same way? The concept is the same, but request/response formats differ. Check your provider's docs — for Claude via SubToAPI, see the tool use guide.