Function Calling
A structured way for AI models to invoke predefined functions with typed parameters, enabling reliable integration with external systems.
In Depth
Function calling is the structured mechanism that enables AI models to invoke external tools and services during response generation. Instead of the model outputting plain text that must be parsed and interpreted, function calling produces typed, structured outputs with explicit function names, parameter values, and return expectations that can be directly executed by the hosting system.
The function calling workflow has three parts. First, you define available functions using a schema that includes the function name, description, and parameter types (using JSON Schema). For example, a file editing function might accept parameters for file path, old text, and new text, all with type string. Second, the AI model evaluates the conversation context and decides whether to call a function, which function to call, and with what arguments. Third, the system executes the function, returns the result, and the model continues its response incorporating the result.
Function calling is what makes the difference between an AI chatbot and an AI coding agent. Without function calling, the model can only suggest code changes in text. With function calling, it can actually read your files, make precise edits, run your test suite, check git status, and interact with external services. Every meaningful action a coding agent takes goes through function calling.
Both the Anthropic and OpenAI APIs support function calling, though with slightly different implementations. Anthropic uses a 'tools' parameter in the Messages API where you define tool schemas, and the model responds with 'tool_use' content blocks. OpenAI uses a similar 'tools' parameter with 'function' type definitions, and the model responds with 'tool_calls' in the assistant message. Understanding these APIs is essential for building custom AI coding tools or extending existing ones.
Examples
- An AI model might call read_file({path: 'src/index.ts'}) to view source code
- Function definitions include parameter types and descriptions for the model to understand
- Anthropic's API supports function calling through the 'tools' parameter
How Function Calling Works in AI Coding Tools
Claude Code's entire agent capability is built on function calling through the Anthropic API. Every file read, edit, search, and terminal command is defined as a tool with a structured schema, and Claude decides when and how to invoke each tool. This structured approach ensures reliable, predictable interactions with your development environment.
Cursor uses function calling internally to power its Composer feature, where the AI model calls tools to read and modify files in your project. Cline implements function calling through the Anthropic API to provide file operations, terminal access, and browser automation within VS Code. When building custom AI coding tools, both the Anthropic SDK and OpenAI SDK provide typed function calling interfaces that handle schema validation and response parsing automatically.
Practical Tips
When building custom AI tools, write detailed function descriptions as the model uses these to decide when to call each function and how to format parameters
Use JSON Schema's enum type to constrain parameter values for functions that accept a limited set of options, improving reliability of function calls
Implement error handling in your function execution layer so that when a tool call fails, the error message is returned to the model and it can retry with corrected parameters
For the Anthropic API, set tool_choice to 'auto' to let the model decide when to use tools, or 'any' to force tool usage when you know the model should take action
Test function calling with edge cases: very long file paths, binary file content, and commands that produce large output to ensure your tool implementations handle them gracefully
FAQ
What is Function Calling?
A structured way for AI models to invoke predefined functions with typed parameters, enabling reliable integration with external systems.
Why is Function Calling important in AI coding?
Function calling is the structured mechanism that enables AI models to invoke external tools and services during response generation. Instead of the model outputting plain text that must be parsed and interpreted, function calling produces typed, structured outputs with explicit function names, parameter values, and return expectations that can be directly executed by the hosting system. The function calling workflow has three parts. First, you define available functions using a schema that includes the function name, description, and parameter types (using JSON Schema). For example, a file editing function might accept parameters for file path, old text, and new text, all with type string. Second, the AI model evaluates the conversation context and decides whether to call a function, which function to call, and with what arguments. Third, the system executes the function, returns the result, and the model continues its response incorporating the result. Function calling is what makes the difference between an AI chatbot and an AI coding agent. Without function calling, the model can only suggest code changes in text. With function calling, it can actually read your files, make precise edits, run your test suite, check git status, and interact with external services. Every meaningful action a coding agent takes goes through function calling. Both the Anthropic and OpenAI APIs support function calling, though with slightly different implementations. Anthropic uses a 'tools' parameter in the Messages API where you define tool schemas, and the model responds with 'tool_use' content blocks. OpenAI uses a similar 'tools' parameter with 'function' type definitions, and the model responds with 'tool_calls' in the assistant message. Understanding these APIs is essential for building custom AI coding tools or extending existing ones.
How do I use Function Calling effectively?
When building custom AI tools, write detailed function descriptions as the model uses these to decide when to call each function and how to format parameters Use JSON Schema's enum type to constrain parameter values for functions that accept a limited set of options, improving reliability of function calls Implement error handling in your function execution layer so that when a tool call fails, the error message is returned to the model and it can retry with corrected parameters
Sources & Methodology
Definitions are curated from practical AI coding usage, workflow context, and linked tool documentation where relevant.