AI excels at generating text, but real-world tasks often require actions beyond wordsβlike calling a weather API for current conditions instead of providing outdated information. Function calling allows Incredible to execute custom code and tools to take these actions.
What is Live-code Function Calling?
Our agentic models perform function calling by writing code in real-time. This allows them to reference data, perform analysis, and call functions in more sophisticated ways than traditional function calling approaches.
Think of functions as recipes with:
- Name and description - What the function does
- Parameters - Required and optional inputs
- Action - What the AI can accomplish by calling it
When users make requests, the AI decides whether to respond directly or use available functions to accomplish the task.
π Basic Function Calling
Simple Example
curl -X POST "https://api.incredible.one/v1/chat-completion" \
-H "Content-Type: application/json" \
-d '{
"model": "small-1",
"messages": [{"role": "user", "content": "Calculate 25 + 17"}],
"functions": [{
"name": "add_numbers",
"description": "Add two numbers together",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "number"},
"b": {"type": "number"}
},
"required": ["a", "b"]
}
}]
}'
Response:
{
"result": {
"response": [
{
"content": "I'll use the available function to add those two numbers for you.",
"role": "assistant"
},
{
"function_call_id": "ce9ea390-450f-4a7f-8872-2f0aa69c3467",
"function_calls": [
{
"input": {"a": 25, "b": 17},
"name": "add_numbers"
}
],
"type": "function_call"
}
]
}
}
π§ Function Schema
Understanding Function Definitions
When you define a function, youβre providing the AI with a βcookbookβ of available tools. Each function definition tells the model what it can do and what inputs it needs.
| Field | Description |
|---|
model | Specifies which AI model will process the request. |
functions | A cookbook of tools. Each function has a name, description, and parameters. |
parameters | Describes the function inputs with types, descriptions, and required fields. |
messages | Represents the conversation history between user and assistant. |
Required Fields
{
"name": "function_name",
"description": "What this function does",
"parameters": {
"type": "object",
"properties": {
"param1": {
"type": "string|number|boolean|array|object",
"description": "Parameter description"
}
},
"required": ["param1"]
}
}
Parameter Types
string: Text values
number: Numeric values
boolean: True/false values
array: Lists of values
object: Structured data
π Multiple Functions
Provide multiple functions and let Incredible choose:
{
"functions": [
{
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {"type": "string"}
},
"required": ["location"]
}
},
{
"name": "calculate_distance",
"description": "Calculate distance between two points",
"parameters": {
"type": "object",
"properties": {
"lat1": {"type": "number"},
"lon1": {"type": "number"},
"lat2": {"type": "number"},
"lon2": {"type": "number"}
},
"required": ["lat1", "lon1", "lat2", "lon2"]
}
}
]
}
π Function Calling in Conversations
Include function results in subsequent messages:
{
"messages": [
{"role": "user", "content": "What's the weather in New York?"},
{"role": "assistant", "content": "Let me check the weather for you."},
{"role": "function", "name": "get_weather", "content": "{\"temperature\": 72, \"condition\": \"sunny\"}"},
{"role": "user", "content": "What about tomorrow?"}
]
}
π¨ Error Handling
Common Errors
Invalid Function Schema:
{
"error": "Invalid function schema"
}
Function Not Found:
{
"error": "Function 'unknown_function' not found"
}
π‘ Best Practices
- Clear Names: Use descriptive function names
- Detailed Descriptions: Explain what the function does
- Parameter Validation: Define required vs optional clearly
- Test Functions: Ensure functions work independently
π― Whatβs Next?
π Complete Function Calling Workflow
Function calling follows a specific workflow. Hereβs the complete process:
Step 1: AI Decides to Call Function
The AI announces a function call in its response:
curl -X POST "https://api.incredible.one/v1/chat-completion" \
-H "Content-Type: application/json" \
-d '{
"model": "small-1",
"messages": [{"role": "user", "content": "Calculate 25 + 17"}],
"functions": [{
"name": "add_numbers",
"description": "Add two numbers together",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "number"},
"b": {"type": "number"}
},
"required": ["a", "b"]
}
}]
}'
Response with Function Call:
{
"result": {
"thinking": "The user wants to calculate 25 + 17. I have an add_numbers function available.",
"response": [
{
"role": "assistant",
"content": "I'll calculate that for you using the add_numbers function."
},
{
"type": "function_call",
"function_call_id": "abc-123",
"function_calls": [
{
"name": "add_numbers",
"input": {"a": 25, "b": 17}
}
]
}
]
}
}
Understanding function_call_id: This unique identifier links a function call request to its result. When you execute the function and return results, you must include the same function_call_id so the model can associate the output with the correct request. If multiple functions are called, execute them in order and return their results with the corresponding ID.
Step 2: Client Executes Function
Execute the function locally with the provided parameters:
def add_numbers(a, b):
return a + b
result = add_numbers(25, 17) # Returns 42
Important: The model can perform multiple types of server-side actions, and will include the results from those in the response. You must add ALL items from the response to your messages array when sending the next request, so the model can use them in subsequent steps.
Step 3: Client Sends Results
Send the function results back to continue the conversation:
curl -X POST "https://api.incredible.one/v1/chat-completion" \
-H "Content-Type: application/json" \
-d '{
"model": "small-1",
"messages": [
{"role": "user", "content": "Calculate 25 + 17"},
{"role": "assistant", "content": "I'\''ll calculate that for you using the add_numbers function."},
{
"type": "function_call",
"function_call_id": "abc-123",
"function_calls": [{"name": "add_numbers", "input": {"a": 25, "b": 17}}]
},
{
"type": "function_call_result",
"function_call_id": "abc-123",
"function_call_results": [42]
}
],
"functions": [{
"name": "add_numbers",
"description": "Add two numbers together",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "number"},
"b": {"type": "number"}
},
"required": ["a", "b"]
}
}]
}'
Final Response:
{
"result": {
"thinking": "The user provided the result of the addition. Now I can give them the final answer.",
"response": [
{
"role": "assistant",
"content": "The sum of 25 and 17 is 42."
}
]
}
}