Function Calling: LLMs Using Tools
Function calling turns an LLM into an API translator: it reads input and emits JSON telling your code which tool to run. Use it when the model needs live data it cannot store in weights. The model never executes the call and can hallucinate arguments.
Why it exists
Large language models are frozen neural networks with a fixed knowledge cutoff. They cannot access real time stock prices, send emails, or run Python code because they are text in, text out systems with no native connection to external tools. Function calling was invented to bridge this gap by teaching the model to emit structured machine readable intent rather than unstructured text, allowing developer code to act on the model's behalf.
The mental model
Think of function calling as a universal remote control that does not actually change the channel. The LLM reads your natural language request, decides which button to press, and hands you a labeled sticky note that says press power on device X. Your application is the person who receives the note, verifies it is sane, presses the actual button, and reports back what happened. The model proposes, the code disposes.
How it works
You register one or more available functions with the model by providing a JSON schema that defines each function name, description, and parameter types. When a user asks a question that requires external data, the model outputs a special JSON object instead of normal text. This object contains the target function name and a dictionary of arguments. Your application parses this payload, validates the arguments against your own constraints, invokes the real function, and then optionally returns the function result to the model in a follow up message so the model can synthesize a natural language answer for the user.
When to use it
Use function calling whenever the LLM needs information or actions beyond its parametric memory. Common scenarios include querying live databases, retrieving weather or stock data, performing deterministic calculations with a calculator or code interpreter, booking appointments, or controlling hardware and APIs. It is also valuable when you need structured extraction from user input, such as parsing a shipping address into labeled fields that map directly to an API.
When not to use it
Avoid function calling for tasks the model can handle entirely from its internal knowledge, like summarization, translation, or creative writing, because the extra latency and complexity add no value. Do not use it when strict deterministic guarantees are required for the argument values themselves, since the model can still hallucinate parameters or choose the wrong function. If your function schema is extremely large or deeply nested, the model may struggle to generate valid JSON and you may be better off with traditional parsing or fine tuning.
One canonical example
A user asks a travel assistant, what is the cheapest flight from New York to London tomorrow. The model does not know live airfare, so it emits a JSON payload calling search_flights with arguments origin set to JFK, destination set to LHR, and date set to tomorrow's date. Your backend receives this, calls the airline API, finds a four hundred dollar ticket, and returns that result to the model. The model then replies, the cheapest flight is four hundred dollars on Delta departing at 8 PM. The user sees a natural answer, but every real action happened in your code.
Interview question
Which scenario best illustrates appropriate use of LLM function calling?
- a.Asking the model to translate a paragraph from English to Spanish
- b.Having the model summarize a long article provided in the prompt
- c.Requesting live stock prices and having the model output JSON to query a finance APICorrect
- d.Using the model to deterministically validate credit card numbers against a strict regex
Why? this is the answer
Function calling is meant for live external data the model cannot store, such as stock prices. Option D is tempting because it involves structured output, but the card warns against using function calling when strict deterministic guarantees are required since the model can hallucinate parameters.
Just read this? Test yourself on what you have been reading.
- #llms
- #function-calling
- #agents
- #api-integration
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on llms — each one lists the topics its interview covers.
See open roles