Function Calling With LLMs: A Practical Guide
Function calling with LLMs explained: how tools work, structured schemas, execution loops, and best practices for building reliable AI apps.
Function calling lets an LLM request an action instead of just answering: given a list of tool schemas, the model returns a structured call, your code executes it, and the result goes back into context. It is how chatbots book flights, query databases, and drive agents.
How function calling works
- Define tools as JSON schemas (name, description, parameters).
- Send the user request plus tool definitions.
- The model returns a structured tool call — not prose.
- Your code validates and executes the function.
- Send the result back so the model can continue.
Writing good tool schemas
- Give every parameter a clear description — the model chooses based on it.
- Keep parameter sets small and typed.
- Make required vs optional explicit.
- Use enums where values are fixed.
The execution loop
Function calling is a loop, not a single call. Model → tool call → execute → feed result → model again. Set a max iteration count and a token budget, or an agent can loop forever and spend your whole allowance.
Security rules
- Validate arguments against the schema before executing.
- Least-privilege tools: the model can only do what you expose.
- Require confirmation for destructive actions.
- Keep secrets out of tool arguments where possible.
FAQ
What is function calling in LLMs?+
A capability where the model returns structured calls to tools you define, letting your app execute real actions instead of just getting text.
Is function calling the same as tool use?+
Roughly, yes. Function calling is the API mechanism; tool use is the broader pattern of the model invoking external capabilities.
How do I stop a function-calling loop?+
Limit iterations, budget tokens, and validate every call. Terminate after N rounds or when results stop changing the answer.
Related posts
Aug 11, 2026 · Prompt engineering
Structured Outputs: Getting Reliable JSON From LLMsStructured outputs and JSON mode for LLMs: guaranteed JSON, schemas, validation, and patterns to make model output parseable and reliable.
Aug 11, 2026 · AI gateway
Function Calling With LLMs: A Practical GuideFunction calling with LLMs explained: how tools work, structured schemas, execution loops, and best practices for building reliable AI apps.
Aug 11, 2026 · AI gateway
Streaming LLM Responses: How It Works and Best PracticesStreaming LLM responses explained: how token streaming works, SSE vs WebSocket, and best practices for latency, UX, and cost in your app.