Provider Support:
- OpenAIRealtime: Function calling can be defined and configured directly via API parameters (as shown in this guide). You can pass function definitions programmatically.
- ElevenLabs: Function calling must be configured through the ElevenLabs dashboard. It cannot be defined via API parameters - you need to set up functions in your ElevenLabs account settings before using them.
Define Function Tools
Define the functions that can be called by including them in the
tools array. In v2, function tools configuration is sent immediately after the WebSocket connection is established (in the onopen event), not after receiving realtime.session.created:You can define multiple functions in the
tools array. Each function should have a unique name, clear description, and well-defined parameters. Function tools configuration is sent immediately after WebSocket connection is established, not after receiving realtime.session.created. Session configuration (voice, model, instructions, etc.) is passed through WebSocket connection URL parameters.Listen for Function Call Events
Listen for function call events in your WebSocket message handler. When a function needs to be called, you’ll receive a The
realtime.response.function_call_arguments.done event:realtime.response.function_call_arguments.done event contains the following properties:Name of the function to be called.Example:
"function_call_judge"Function call parameters. May be a JSON string or already parsed object containing the parameter values.Example:
"{\"userInput\": \"What's the weather in Beijing?\"}" or {"userInput": "What's the weather in Beijing?"}Unique identifier for this function call. Required when returning results - use this
call_id when sending the function call output back to the AI.Example: "call-123456"Return Results to AI
After processing the function call, send the result back to the AI so it can continue the conversation:
The
reply: 1 field in the function call output message indicates that the AI should automatically generate a response based on the function result. The AI will process the function result and continue the conversation without needing a separate response.create message.Common Use Cases
| Use Case | Example | Implementation |
|---|---|---|
| Weather API | User asks: “What’s the weather in Beijing?” | Call weather API with location, return forecast |
| Knowledge Base | User asks: “How do I integrate function calling?” | Search knowledge base, return relevant documentation |
| Database Query | User asks: “Show me today’s sales numbers” | Query database, return formatted results |
| IoT Control | User says: “Turn on the lights” | Call IoT API to control devices |
| CRM Integration | User asks: “Update customer status for John” | Call CRM API to update records |
Function calling allows you to extend the digital human’s capabilities beyond conversation. Integrate with any REST API, database, or backend service to create powerful, context-aware applications.