Skip to main content

Overview

Function calling (also known as tool use) allows AI models to:
  • Call external APIs - Fetch real-time data
  • Execute functions - Perform calculations or operations
  • Access databases - Query and retrieve information
  • Interact with systems - Control external services
  • Chain operations - Build complex multi-step workflows
Parallel Function Calling: MegaLLM supports executing multiple functions simultaneously for improved performance.

How It Works

1

Define Tools

Specify available functions with JSON Schema descriptions
2

AI Decides

Model determines when and how to use tools based on context
3

Execute Functions

Your application executes the requested functions
4

Return Results

Send function results back to the AI
5

AI Responds

Model incorporates results into final response

Tool Definition Format

OpenAI Format

Anthropic Format

Request Parameters

OpenAI Format

Tool Choice Options

Examples

Parallel Function Calling

Execute multiple functions simultaneously:

Advanced Patterns

Function Chaining

Build complex workflows:

Error Handling

Streaming with Functions

Real-World Use Cases

Database Query Assistant

API Integration Agent

File System Operations

Best Practices

Write clear, detailed function descriptions. The AI uses these to decide when and how to call functions.
  1. Clear descriptions - Be specific about what each function does
  2. Validate inputs - Always validate function arguments before execution
  3. Handle errors gracefully - Return error information to the AI
  4. Use type hints - Leverage TypeScript/Python types for safety
  5. Rate limit - Implement rate limiting for external API calls
  6. Security - Validate and sanitize all function inputs
  7. Async execution - Use async/await for better performance

Response Format

Tool Call Object (OpenAI)

Tool Response Format

Model Support

Function calling is supported by these models:
  • OpenAI: gpt-4, gpt-4-turbo, gpt-3.5-turbo
  • Anthropic: claude-3.5-sonnet, claude-opus-4, claude-sonnet-4
  • Others: Check Models page for full list