Skip to main content
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.
This guide assumes familiarity with the Agent API and its tool definitions. For parameter reference and basic usage, see the Agent API reference.

Prerequisites

Install the Perplexity SDK:
If you don’t have an API key yet:

Get your Perplexity API Key

Navigate to the API Keys tab in the API Portal and generate a new key.
Then export your API key as an environment variable:
For built-in tools, start with Web Search, Fetch URL Content, Finance Search, and People Search. This guide focuses on custom function orchestration patterns in application code.

Complete Multi-Turn Flow

The core function calling loop follows a specific pattern: send a request with tool definitions, detect function_call items in the response, execute your functions locally, then return the results as function_call_output items.
Always use json.loads() (Python) or JSON.parse() (TypeScript) on the arguments field. It is a JSON string, not a parsed object.

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.
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.
Built-in tools like web_search are executed server-side by the API. You only need to handle function_call items for your custom functions. The model seamlessly interleaves built-in and custom tool usage.

Error Handling Patterns

When a function call fails, return a structured error in the function_call_output so the model can adapt its response. Never silently swallow errors; the model can often recover or inform the user gracefully.
Key principles for error handling:
  • Return errors as structured data, not exceptions. Include "error": true and 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 several function_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

Agent API Reference

Review request parameters and tool schema fields.

Agent API 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.