Skip to main content

Overview

Tool calling (also known as function calling) enables AI models to request the execution of external functions and tools. Instead of trying to generate structured data directly, the model can indicate it wants to call a function with specific parameters, your code executes the function, and then you send the result back to continue the conversation. Use tool calling when you need to:
  • Access real-time data (weather, stock prices, databases)
  • Perform calculations or data processing
  • Interact with external APIs and services
  • Execute actions on behalf of users (send emails, create calendar events)
  • Retrieve information the model doesn’t have in its training data

Prerequisites

Before using tool calling, ensure you have:
  • A Concentrate AI API key (get one here)
  • A model that supports tool calling (refer to the models endpoint to see models that support tool calling)
  • An understanding of JSON Schema for defining function parameters

Quick Start

Here’s a basic example of tool calling with a weather function:

Defining Tools

Each tool is defined using the FunctionTool schema. Here are the properties:

Tool Name Pattern

Tool names must match the pattern: ^[a-zA-Z0-9_.-]+$ Valid names:
  • get_weather
  • send_email
  • user.get_profile
  • calculate-sum
Invalid names:
  • get weather (contains space)
  • send@email (contains @)

Parameters as JSON Schema

The parameters field uses JSON Schema to define the function’s inputs. This helps the model understand what data to provide.
Best practices for parameters:
  • Always include description fields to help the model understand the purpose
  • Use enum for fields with fixed options
  • Mark essential parameters in the required array
  • Keep schemas simple and focused on the task

Strict Mode

By default, strict: true is enabled. This ensures the model’s output strictly follows your schema.
When strict is true, your parameters schema must include "additionalProperties": false. Omitting it will return a 400 Bad Request error. This is a common mistake.
With strict: true, the model will only include name and email in its output—no additional properties. Set strict: false if you don’t want to enforce additionalProperties.

Cache Control

Cache tool definitions to improve performance and reduce costs:
See Prompt Caching for more details.

Tool Choice Modes

Control which tools the model can use with the tool_choice parameter:

None - Disable Tool Calling

The model will respond normally without calling any tools, even if they’re available.

Auto - Let Model Decide (Default)

The model decides whether to call a tool based on the user’s input. This is the default behavior.

Required - Force Tool Use

The model must call at least one tool before responding. Useful when you always need a function call.

Specific Tool - Force Specific Function

The model must call the specified tool. Use this when you know exactly which function should be called.

Allowed Tools - Limit to Subset

Restrict the model to only use specific tools from your full tool list. The mode can be:
  • "auto" - Model can use these tools or skip them
  • "required" - Model must use at least one of these tools

Parallel Tool Calls

Enable the model to call multiple tools simultaneously:
When to enable:
  • Multiple independent operations (e.g., get weather for multiple cities)
  • No dependencies between tool calls
  • Want faster results through parallelization
When to disable:
  • Sequential operations where order matters
  • Tool calls depend on each other’s results
  • Want more predictable, step-by-step execution
Example of parallel tool calls:

Multi-Turn Workflow

Tool calling typically follows this pattern:
  1. User sends a message with tools available
  2. Model responds with function_call indicating it wants to use a tool
  3. You execute the function in your application
  4. You send the result back with function_call_output
  5. Model provides final response using the tool result
Here’s a complete example:

Streaming with Tools

When streaming is enabled, tool calls are delivered incrementally:

Event Types

  • response.function_call_arguments.delta - Incremental tool arguments as they’re generated
  • response.function_call_arguments.done - Complete tool call with final arguments

Example

TypeScript - Streaming Tool Calls
See Streaming for more details on streaming events.

Advanced Patterns

Error Handling

When a tool execution fails, use the is_error flag:
The model will receive the error and can respond appropriately (e.g., apologizing or suggesting alternatives).

Caching Tool Definitions

For frequently used tools, cache their definitions:
This reduces costs and improves latency for repeated requests with the same tools.

Provider Support

Tool calling is supported across all major providers: Check specific model capabilities using the List Models endpoint.

Best Practices

Schema Design

  1. Keep it simple - Only include necessary parameters
  2. Clear descriptions - Help the model understand what each field does
  3. Use enums - For fields with fixed options
  4. Validate inputs - Always validate tool arguments before execution
  5. Handle errors gracefully - Return meaningful error messages

Security Considerations

  1. Validate all inputs - Never trust model-generated arguments blindly
  2. Limit permissions - Tools should have minimal necessary permissions
  3. Rate limiting - Implement rate limits on expensive operations
  4. Audit logs - Log all tool executions for monitoring
  5. Sanitize outputs - Clean tool results before sending back to model

Performance Optimization

  1. Cache definitions - Use cache_control for frequently used tools
  2. Parallel calls - Enable when tools are independent
  3. Lazy loading - Only load tools when needed
  4. Timeout handling - Set reasonable timeouts for tool execution
  5. Batch operations - Combine multiple related calls when possible

Troubleshooting

Model doesn’t call the tool

Problem: Model responds with text instead of calling the tool. Solutions:
  • Make the tool description clearer and more specific
  • Use tool_choice: "required" to force tool usage
  • Ensure the user’s input clearly requires the tool’s functionality
  • Check if the model supports tool calling

Invalid arguments

Problem: Model provides incorrect or incomplete arguments. Solutions:
  • Add detailed descriptions to parameter fields
  • Use strict: true to enforce schema validation
  • Include examples in parameter descriptions
  • Mark essential fields as required

Tool not found error

Problem: "Tool 'xyz' not found" error. Solutions:
  • Verify tool name matches exactly (case-sensitive)
  • Ensure tool name follows the pattern: ^[a-zA-Z0-9_.-]+$
  • Check that tools array is included in the request

Streaming issues

Problem: Tool calls not appearing in streaming mode. Solutions:
  • Listen for response.function_call_arguments.delta and .done events
  • Buffer the arguments until the .done event
  • Check that stream: true is set in the request

Create Response

Main API endpoint for generating responses

Request Parameters

Complete parameter reference

Streaming

Real-time response streaming

Prompt Caching

Cache tool definitions for better performance