What is function calling (tool calling)? How does it work under the hood?
Think first
Think about: schemas, model decision, execution, observation, the loop.
Strong answer
Function calling lets an LLM decide to invoke a function you've defined. You provide tool definitions as JSON schemas (name, description, parameter types). The model, given a task, returns a structured response with the function name and arguments. Your code executes the function and returns the result as an observation. The model then reasons about the observation and either calls another tool or produces a final answer. Under the hood: the model is fine-tuned to output structured tool-call JSON when it determines a tool is needed, rather than free text. Production requirements: validate arguments with Pydantic, set per-tool timeouts, return structured errors (not exceptions) so the model can recover, sanitise tool output (injection defence), and audit-log every call. Tool calling is the foundation of all agents.
Weak-answer indicators
- Confuses function calling with RAG
- Doesn't mention schemas
- Skips validation and error handling
- Doesn't explain the loop
Likely follow-up questions
- How do you secure tool calling against injection?
- What is MCP and how does it relate to function calling?