> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://poly-ai.ferndocs.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://poly-ai.ferndocs.com/_mcp/server.

# Extend with code

> Write Python functions to integrate APIs, validate input, and add custom business logic to your agent.

Write Python functions to extend your agent beyond what the visual interface covers — call APIs, query databases, validate input, and encode custom business logic. The agent invokes your function like any other tool.

> **Info**
>
> **Python required.** Non-technical operators: see [Configure agents in the UI](/agent-studio/get-started/agent-studio) for the no-code path.

> **Tip**
>
> Before writing custom API code, check if a [pre-built integration](/integrations/integrations/overview) already exists for your platform (e.g., Salesforce, Zendesk, OpenTable, Stripe). Pre-built integrations handle authentication, environment configuration, and common operations for you.

## What you can build

#### [API integrations](/agent-studio/tools/classes-and-objects/api)

Call external APIs to look up bookings, check availability, or retrieve account details

#### [Business logic](/agent-studio/tools/overview)

Validate input, calculate pricing, or enforce rules that the LLM cannot handle alone

#### [CRM and data writes](/agent-studio/tools/create-tool)

Create records, update customer profiles, or log interactions in external systems

#### [Dynamic routing](/agent-studio/voice/handoffs)

Route calls to the right team based on real-time data or custom logic

#### [Multi-step workflows](/advanced/code-driven-flows/transition-functions)

Build code-driven flows with transition functions for complex conversation paths

#### [Real-time configuration](/agent-studio/real-time-config/overview)

Define schemas that adapt agent behavior based on external config data

## Get started

#### Write your first function

Follow the [create a function](/agent-studio/tools/create-tool) guide to set up a Python function with parameters, code, and a description the LLM can use to trigger it.

#### Understand the tools system

Read the [tools overview](/agent-studio/tools/overview) to learn how functions integrate with the LLM, how to use `conv` objects, and best practices for naming and triggering.

#### Explore advanced patterns

Read [flows](/agent-studio/flows/overview), [call handoffs](/agent-studio/voice/handoffs), and [secrets management](/advanced/secrets/overview) to build production integrations.

## Tools and functions

Write Python scripts that run when your agent needs to take action. Functions are the bridge between conversation and your business systems.

#### [Create a function](/agent-studio/tools/create-tool)

Set up a function with naming conventions, parameters, and Python code

#### [Start tool](/agent-studio/tools/start-tool)

Initialize conversation context before the greeting plays

#### [End tool](/agent-studio/tools/end-tool)

Run post-call processing after a conversation ends

#### [Return values](/agent-studio/tools/return-values)

Control agent behavior with string and dictionary returns

#### [Variables](/agent-studio/tools/variables)

Define, update, and persist values across turns

#### [Libraries](/agent-studio/tools/libraries)

Standard and non-standard libraries available in your functions

#### [Delay control](/agent-studio/tools/delay-control)

Add filler phrases to avoid silence during slow functions

#### [Use functions in topics](/agent-studio/tools/using-tools)

Trigger functions from FAQs

## Reference

Access conversation data and built-in utilities in your functions.

#### [conv object](/agent-studio/tools/classes-and-objects/object)

Conversation states, flows, and telephony attributes

#### [conv.log](/agent-studio/tools/classes-and-objects/log)

Structured diagnostics and PII-scoped logging

#### [conv.utils](/agent-studio/tools/classes-and-objects/utilities)

Built-in helpers for addresses, cities, and structured data

#### [conv.api](/agent-studio/tools/classes-and-objects/api)

Call configured API integrations

#### [Agent memory](/agent-studio/tools/classes-and-objects/agent-memory)

Persistent data across conversations for repeat callers

#### [Function classes](/agent-studio/tools/classes-and-objects/classes)

VoiceWeighting, TTSVoice, and provider classes

## Advanced capabilities

#### [Secrets](/advanced/secrets/overview)

Store API keys and credentials securely

#### [Call handoffs](/agent-studio/voice/handoffs)

Configure call transfers and routing rules

#### [Configuration builder](/agent-studio/real-time-config/overview)

Define real-time config schemas for dynamic agent behavior

#### [Call data export](/agent-studio/account/call-data/recordings-and-transcripts)

Access transcripts and recordings through the API

#### [Code-driven flows](/advanced/code-driven-flows/transition-functions)

Build flows with transition functions and programmatic control

#### [Script agent configuration](/api-reference/build-deploy/agents-api/overview)

Use the Agents API to create, configure, and deploy agents from code

#### [API reference](/api-reference/api-reference/overview)

Integrate with PolyAI REST APIs for chat, conversations, alerts, and more