Browse documentation
Guides

Function calling

Let the model request an action in your application and use the result in its next reply.

Choose a tool

JSON
{
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_stock",
        "description": "Look up an item’s stock.",
        "parameters": {
          "type": "object",
          "properties": {
            "sku": {
              "type": "string"
            }
          },
          "required": [
            "sku"
          ]
        }
      }
    }
  ],
  "tool_choice": {
    "type": "function",
    "function": {
      "name": "get_stock"
    }
  },
  "parallel_tool_calls": false
}

In Chat Completions, use auto to let the model choose, none to suppress tool calls, or name a function to require it. Put streamed tool arguments together by choice and tool index before decoding JSON. Execute each completed call in your client.

Responses and Messages examples

cURL
curl "https://kavren.dev/v1/responses" \
  -H "Authorization: Bearer $KAVREN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "deepseek-v4.1-flash-fast",
  "input": "How many KB-01 keyboards are in stock?",
  "tools": [
    {
      "type": "function",
      "name": "get_stock",
      "description": "Look up an item’s stock.",
      "parameters": {
        "type": "object",
        "properties": {
          "sku": {
            "type": "string"
          }
        },
        "required": [
          "sku"
        ]
      }
    }
  ],
  "tool_choice": "auto",
  "max_output_tokens": 128
}'
cURL
curl "https://kavren.dev/v1/messages" \
  -H "Authorization: Bearer $KAVREN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "deepseek-v4.1-flash-fast",
  "messages": [
    {
      "role": "user",
      "content": "How many KB-01 keyboards are in stock?"
    }
  ],
  "tools": [
    {
      "name": "get_stock",
      "input_schema": {
        "type": "object",
        "properties": {
          "sku": {
            "type": "string"
          }
        },
        "required": [
          "sku"
        ]
      }
    }
  ],
  "tool_choice": {
    "type": "tool",
    "name": "get_stock",
    "disable_parallel_tool_use": true
  },
  "max_tokens": 128
}'

For Responses, send the call and a function_call_output item carrying its call_id in the next input. For Messages, append the assistant tool_use block and a user message containing a tool_result with the matching tool_use_id.

Describe the function

cURL
curl "https://kavren.dev/v1/chat/completions" \
  -H "Authorization: Bearer $KAVREN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "deepseek-v4.1-flash-fast",
  "messages": [
    {
      "role": "user",
      "content": "How many KB-01 keyboards are in stock?"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_stock",
        "description": "Look up an item’s stock.",
        "parameters": {
          "type": "object",
          "properties": {
            "sku": {
              "type": "string"
            }
          },
          "required": [
            "sku"
          ]
        }
      }
    }
  ],
  "tool_choice": "auto"
}'

Handle the call

Example response · selected fields
{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": null,
        "tool_calls": [
          {
            "id": "call_1",
            "type": "function",
            "function": {
              "name": "get_stock",
              "arguments": "{\"sku\":\"KB-01\"}"
            }
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ]
}

Parse the arguments string as JSON, check the function name and validate the SKU before calling your inventory service. If several calls arrive, keep each call ID with its result.

Return the result

cURL
curl "https://kavren.dev/v1/chat/completions" \
  -H "Authorization: Bearer $KAVREN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "deepseek-v4.1-flash-fast",
  "messages": [
    {
      "role": "user",
      "content": "How many KB-01 keyboards are in stock?"
    },
    {
      "role": "assistant",
      "content": null,
      "tool_calls": [
        {
          "id": "call_1",
          "type": "function",
          "function": {
            "name": "get_stock",
            "arguments": "{\"sku\":\"KB-01\"}"
          }
        }
      ]
    },
    {
      "role": "tool",
      "tool_call_id": "call_1",
      "content": "{\"available\":7}"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_stock",
        "description": "Look up an item’s stock.",
        "parameters": {
          "type": "object",
          "properties": {
            "sku": {
              "type": "string"
            }
          },
          "required": [
            "sku"
          ]
        }
      }
    }
  ]
}'

In your client, append the actual assistant message and your actual inventory result. Read the next assistant message for the customer-facing answer.

Search Kavren