Function Calling

Enable AI models to call your functions and APIs. Build intelligent agents that take action in the real world.

How Function Calling Works

Function calling allows AI models to generate structured JSON that matches function signatures you define. The model decides when to call functions and with what argumentsβ€”you execute the function and return results.

1

Define Tools

β†’
2

Send Request

β†’
3

Model Calls

β†’
4

Execute

β†’
5

Return Result

Features

πŸ”§ Multiple Tools

Define up to 128 tools per request. Model picks the right one automatically.

πŸ“ JSON Schema

Define parameters with JSON Schema for validated, structured output.

πŸ”„ Parallel Calls

Model can call multiple functions in parallel for efficiency.

🎯 Forced Calls

Force the model to call a specific function or any function.

πŸ“‘ Streaming

Stream function call arguments as they're generated.

πŸ”— Tool Choice

Control whether and which tools the model should use.

Example: Weather Bot

Here's a complete example of a weather chatbot that uses function calling:

Python
# Define your function
def get_weather(location: str, unit: str = "celsius") -> dict:
    # Your actual weather API call here
    return {"temp": 22, "condition": "sunny"}

# Define the tool for the API
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current weather for a location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "City name, e.g. San Francisco"
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"]
                    }
                },
                "required": ["location"]
            }
        }
    }
]

# Make the API call
response = client.chat.completions.create(
    model="mythic-4",
    messages=[{"role": "user", "content": "What's the weather in Paris?"}],
    tools=tools
)

# Check if model wants to call a function
if response.choices[0].message.tool_calls:
    tool_call = response.choices[0].message.tool_calls[0]
    args = json.loads(tool_call.function.arguments)
    
    # Execute the function
    result = get_weather(**args)
    
    # Send result back to the model
    final_response = client.chat.completions.create(
        model="mythic-4",
        messages=[
            {"role": "user", "content": "What's the weather in Paris?"},
            response.choices[0].message,
            {
                "role": "tool",
                "tool_call_id": tool_call.id,
                "content": json.dumps(result)
            }
        ],
        tools=tools
    )
    
    print(final_response.choices[0].message.content)
    # "The current weather in Paris is 22Β°C and sunny!"

Tool Definition Schema

Tool Object
Property Type Description
type string Must be "function"
function.name string Function name (a-z, 0-9, underscores)
function.description string What the function does. Be detailed!
function.parameters object JSON Schema for function parameters
function.strict boolean Enable strict schema validation

πŸ’‘ Pro Tip: Descriptions Matter

Write detailed function and parameter descriptions. The model uses these to decide when and how to call your functions. Include examples, valid values, and edge cases.

Tool Choice

Control how the model uses tools with the tool_choice parameter:

Tool Choice Options
# Let model decide (default)
tool_choice="auto"

# Force model to call a function
tool_choice="required"

# Disable function calling
tool_choice="none"

# Force specific function
tool_choice={"type": "function", "function": {"name": "get_weather"}}

Use Cases

πŸ€– AI Agents

  • Browse the web
  • Execute code
  • Manage files
  • Send emails

πŸ’¬ Smart Chatbots

  • Book appointments
  • Track orders
  • Query databases
  • Process returns

πŸ“Š Data Pipelines

  • Extract entities
  • Classify documents
  • Transform data
  • Generate reports

πŸ”Œ API Orchestration

  • Multi-step workflows
  • Conditional logic
  • Error handling
  • Retries

Best Practices

πŸ“ Write Clear Descriptions

Detailed descriptions help the model know when to call functions.

βœ… Validate Arguments

Always validate function arguments before executing.

πŸ”’ Secure Execution

Never execute arbitrary code. Whitelist allowed operations.