This guide covers production-ready function calling patterns that go beyond the basics. You will learn the complete multi-turn flow, multi-function orchestration, combining custom functions with built-in tools, robust error handling, and parallel function call processing.Documentation Index
Fetch the complete documentation index at: https://docs.perplexity.ai/llms.txt
Use this file to discover all available pages before exploring further.
This guide assumes familiarity with the Agent API and its tool definitions. For parameter reference and basic usage, see the Tools documentation.
Prerequisites
Install the Perplexity SDK:Get your Perplexity API Key
Navigate to the API Keys tab in the API Portal and generate a new key.
Complete Multi-Turn Flow
The core function calling loop follows a specific pattern: send a request with tool definitions, detectfunction_call items in the response, execute your functions locally, then return the results as function_call_output items.
Multi-Function Orchestration
When you provide multiple tools, the model decides which to call and in what order. This example registers three functions that work together to answer a complex query.The model may call functions across multiple turns. The
while loop above keeps running until the model finishes all function calls and produces a final text response. In some turns the model may call one function, and in the next turn call another based on the results it received.Combining Custom Functions with web_search
You can mix built-in tools like web_search and fetch_url with your own custom functions in the same tools array. The model decides autonomously which tool to use. This is powerful for workflows that need live web data combined with actions in your own systems.
Error Handling Patterns
When a function call fails, return a structured error in thefunction_call_output so the model can adapt its response. Never silently swallow errors; the model can often recover or inform the user gracefully.
- Return errors as structured data, not exceptions. Include
"error": trueand a human-readable"message"so the model can relay the issue to the user. - Catch specific exceptions (timeouts, auth failures, validation errors) and map them to clear messages.
- Cap the number of turns to prevent infinite loops.
- Never return raw stack traces to the model. They waste tokens and may leak internal details.
Parallel Function Calls
When the model determines that multiple function calls are independent, it may return severalfunction_call items in a single response. Process all of them before sending results back in one batch.
The model may emit multiple
function_call items in a single response when it determines the calls are independent. Using ThreadPoolExecutor (Python) or Promise.all (TypeScript) lets you execute them concurrently, reducing total latency.Next Steps
Tools Reference
Review built-in tools and custom function schema fields.
Models
Choose a model for your function-calling workload.
Agent API Quickstart
Get up and running with the Agent API in minutes.
Output Control
Combine function calling with structured outputs and response shaping.