How does tool (function) calling work under the hood?
You give the model a set of tool schemas, each with a name, description, and typed parameters (usually JSON Schema). The model doesn't execute anything; instead, when it decides a tool is needed, it emits a structured request naming the tool and a JSON argument object. Your runtime parses that, actually runs the function, and returns the result back into the conversation as a tool-result message. The model then continues, possibly calling more tools or answering. Quality depends heavily on clear descriptions and tight schemas, since the model picks tools by their descriptions. Common pitfalls: the model invents nonexistent tools, produces malformed or hallucinated arguments, or calls tools in the wrong order. Robust systems validate arguments against the schema, handle parallel tool calls, and return structured errors so the model can retry.