What is function calling?

8 min read

·
┌──────────────────────────────────────────────────────────┐
│  ═══════════════════════════════════════════════════     │
│  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░     │
│  ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░     │
│  ────────────────────────────────────────────────────    │
│  ██████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░     │
│  █████████████████████████████████░░░░░░░░░░░░░░░░░░     │
│  ██████████████████████████████████████░░░░░░░░░░░░░     │
│  ████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░     │
│  ────────────────────────────────────────────────────    │
│  ███████████████████████████████████████░░░░░░░░░░░░     │
└──────────────────────────────────────────────────────────┘

Function calling is one of the most powerful capabilities in modern AI models. It lets a language model decide when to invoke external tools, APIs, or functions based on a user's request, then incorporate the results into its response. This bridges the gap between what a model knows and what it can actually do.

Why Function Calling Matters

────────────────────────────────────────

Language models are trained on static data. They can reason, summarize, and generate text, but on their own, they cannot check the weather, query a database, or book a flight. Function calling changes that. It gives models a structured way to reach out to the real world.

Without function calling, developers had to build brittle prompt-parsing logic to figure out when a user wanted an action performed. With function calling, the model itself decides which function to invoke and what arguments to pass, based on natural language input.

The Request-Execute-Return Flow

────────────────────────────────────────

Function calling follows a three-step loop:

  1. [You define available functions]: You tell the model what tools are available, including their names, descriptions, and parameter schemas.
  2. [The model decides to call a function]: Based on the user's message, the model generates a structured function call with the appropriate arguments instead of (or alongside) a text response.
  3. [You execute the function and return results]: Your application runs the actual function, then sends the result back to the model so it can formulate a final response.

This loop can repeat multiple times in a single conversation. The model might call one function, examine the result, then call another.

Practical Examples

────────────────────────────────────────

[Getting weather data]: A user asks "What's the weather in Tokyo?" The model calls a get_weather function with {"location": "Tokyo"}. Your app hits a weather API, returns the data, and the model says "It's currently 22 degrees and sunny in Tokyo."

[Querying a database]: A user asks "How many orders did we get last month?" The model calls query_orders with date parameters. Your app runs the SQL query and returns the count.

[Booking a flight]: A user says "Book me a flight from NYC to London on March 15." The model calls search_flights first, presents options, then calls book_flight once the user confirms.

[Multi-step workflows]: A user asks "Find the cheapest hotel near the conference venue and add it to my calendar." The model chains together a venue lookup, hotel search, and calendar booking.

JSON Schema Definitions

────────────────────────────────────────

Functions are defined using JSON Schema. Here is what a typical function definition looks like:

You provide the function name, a clear description of what it does, and a parameters object that specifies the expected input. The description matters a lot. Models use it to decide when a function is relevant, so write descriptions that clearly explain the function's purpose and when it should be used.

Parameter types, required fields, enums for constrained values, and nested objects are all supported. The more precise your schema, the more reliably the model will generate correct arguments.

Provider Support

────────────────────────────────────────

[OpenAI] was the first major provider to ship function calling with GPT-3.5 and GPT-4. Their implementation is mature and well-documented, supporting parallel function calls and strict mode for guaranteed schema adherence.

[Anthropic] supports tool use in Claude models. Claude can handle complex multi-tool scenarios and tends to be thoughtful about when to invoke tools versus respond directly.

[Google] offers function calling in Gemini models, tightly integrated with their Vertex AI platform. Gemini supports both single and parallel function calls.

[Mistral] supports function calling in their larger models, with a similar interface to OpenAI's approach.

[Open source models] can support function calling through frameworks like LangChain, LlamaIndex, or Ollama. Models like Llama 3, Mixtral, and Hermes have been fine-tuned for tool use. The reliability varies, but it is improving rapidly.

Parallel vs Sequential Function Calls

────────────────────────────────────────

[Parallel function calls] happen when a model decides to invoke multiple functions at once. If a user asks "What's the weather in Tokyo and New York?", the model can call get_weather twice simultaneously with different arguments. This speeds up response time. OpenAI, Google, and Anthropic all support parallel calls.

[Sequential function calls] happen when the output of one function is needed to determine the next. "Find the nearest airport to my hotel and check flights from there" requires looking up the hotel location first, then searching flights. The model handles this by making one call, examining the result, then making the next.

Best Practices for Defining Functions

────────────────────────────────────────

[Write clear descriptions]: The model relies heavily on function descriptions to decide when to use them. Be specific about what the function does and when it should be called.

[Keep parameter schemas tight]: Use enums for constrained values, mark required fields, and set reasonable defaults. The stricter the schema, the fewer malformed calls you will see.

[Limit the number of functions]: Providing too many functions can confuse the model and slow down responses. Group related functionality and only expose what is needed for the current context.

[Use descriptive parameter names]: departure_city is better than from. The model reads parameter names as part of understanding the schema.

[Handle errors gracefully]: Functions can fail. Return clear error messages that help the model explain the issue to the user or try an alternative approach.

[Validate inputs before execution]: Never trust the model's arguments blindly. Validate types, ranges, and permissions before executing any function, especially for functions that modify data.

Security Considerations

────────────────────────────────────────

Function calling introduces real security concerns because you are letting a model trigger actions in your system.

[Never expose destructive operations without confirmation]: If a function can delete data, transfer money, or make irreversible changes, add a confirmation step.

[Implement rate limiting]: Prevent the model from making excessive function calls in a loop.

[Sanitize inputs]: Treat function arguments like any user input. Guard against injection attacks, especially if arguments end up in database queries or shell commands.

[Use least privilege]: Only give the model access to functions it actually needs. Do not expose admin endpoints to a general-purpose assistant.

[Audit and log]: Record every function call, its arguments, and its results. This is essential for debugging and security monitoring.

The Bigger Picture: Tool Use, Agents, and MCP

────────────────────────────────────────

Function calling is the foundation for more advanced patterns. [AI agents] use function calling in loops to accomplish multi-step tasks autonomously. [Tool use] is the broader concept of models interacting with external systems. [Model Context Protocol (MCP)] is an emerging standard from Anthropic for defining tool interfaces that work across different models and frameworks.

As function calling becomes more reliable across providers, it is enabling a new generation of AI applications that do not just talk but actually take action on your behalf. Understanding this pattern is essential for anyone building with AI today.

Related Articles

Building with AI