Defining Functions
SWAIG Functions
SWAIG (SignalWire AI Gateway) functions let your AI agent call custom code to look up data, make API calls, and take actions during conversations.
What You’ll Learn
This chapter covers everything about SWAIG functions:
- Defining Functions - Creating functions the AI can call
- Parameters - Accepting arguments from the AI
- Results & Actions - Returning data and triggering actions
- DataMap - Serverless API integration without webhooks
- Native Functions - Built-in SignalWire functions
How SWAIG Functions Work

Quick Start Example
Here’s a complete agent with a SWAIG function:
Function Types
Chapter Contents
When to Use SWAIG Functions
Key Concepts
Handler Functions: Python code that runs on your server when the AI decides to call a function. You have full access to databases, APIs, and any Python library.
SwaigFunctionResult: The return type for all SWAIG functions. Contains the response text the AI will speak and optional actions to execute.
Parameters: JSON Schema definitions that tell the AI what arguments your function accepts. The AI will extract these from the conversation.
Actions: Side effects like call transfers, SMS sending, or context changes that execute after the function completes.
DataMap: A way to define functions that call REST APIs without running any code on your server - the API calls happen directly on SignalWire’s infrastructure.
Let’s start by learning how to define functions.
Basic Function Definition
The define_tool() Method
Required Parameters:
Optional Parameters:
Handler Function Signature
All handlers receive two arguments:
Accessing Call Data
Multiple Functions
Register as many functions as your agent needs:
Function Fillers
Add per-function filler phrases for when the function is executing:
The @tool Decorator
Alternative syntax using decorators:
define_tool() vs @tool: Choosing an Approach
Both methods register SWAIG functions with the same result. Choose based on your coding style and requirements.
Comparison
When to Use define_tool()
Conditional function registration:
Dynamic functions from configuration:
Handlers defined outside the class:
When to Use @tool Decorator
Static, self-documenting functions:
The decorator keeps the function metadata with the implementation, making it easier to see what a function does at a glance.
Mixing Both Approaches
You can use both in the same agent:
External Webhook Functions
Route function calls to an external webhook:
Function Security
By default, functions require token validation. Disable for testing:
Writing Good Descriptions
The description helps the AI decide when to use your function:
Testing Functions
Use swaig-test to test your functions: