Skip to navigation
CallingaiSWAIG

functions

View as MarkdownOpen in Claude

An array of JSON objects to define functions that can be executed during the interaction with the AI.

Each function needs somewhere to run: a web_hook_url, set on the function or inherited from SWAIG.defaults.web_hook_url, or a data_map. A function with none of these is not added to the agent.

A function’s property names are matched without regard to case, so Gates is read as gates. Write each property once, in the case shown on this page: when a function has the same property in two casings, only the first is read, and the other is ignored without a warning.

Properties

SWAIG.functions
object[]

An array of JSON objects that accept the following properties.

functions[].description
stringRequired

A description of the context and purpose of the function, to explain to the agent when to use it. A function with neither description nor the deprecated purpose isn’t added to the agent.

functions[].function
stringRequired

A unique name for the function. This can be any user-defined string or can reference a reserved function. Reserved functions are SignalWire functions that will be executed at certain points in the conversation. To learn more about reserved functions, see Reserved Functions.

functions[].active
booleanDefaults to true

Whether the function is active.

functions[].data_map
object

An object that processes function inputs and executes operations through expressions, webhooks, or direct output. Properties are evaluated in strict priority order: (1) expressions, (2) webhooks, (3) output. Evaluation stops at the first property that returns a valid output result, similar to a return statement in a function.

See data_map for additional details.

functions[].parameters
object

A JSON object that defines the expected user input parameters and their validation rules for the function.

See parameters for additional details.

functions[].fillers
object

An object containing language-specific arrays of filler phrases that are played when calling a SWAIG function. These fillers help break silence between responses and are played asynchronously during the function call. Each key is a language code and each value is an array of filler phrases selected from randomly.

functions[].skip_fillers
booleanDefaults to false

Skips the top-level fillers specified in ai.languages (which includes speech_fillers and function_fillers). When set to true, only function-specific fillers defined directly on SWAIG.functions.fillers will play.

functions[].meta_data
object

A powerful and flexible environmental variable which can accept arbitrary data that is set initially in the SWML script or from the SWML set_meta_data action. This data can be referenced locally to the function. All contained information can be accessed and expanded within the prompt - for example, by using a template string.

functions[].meta_data_token
stringDefaults to Set by SignalWire

Scoping token for meta_data. If not supplied, metadata will be scoped to function’s web_hook_url.

functions[].wait_file
string

A file to play while the function is running. wait_file_loops can specify the amount of times that files should continously play.

functions[].wait_file_loops
string | integer

The amount of times that wait_file should continuously play/loop.

functions[].wait_for_fillers
booleanDefaults to false

Whether to wait for fillers to finish playing before continuing with the function.

functions[].web_hook_url
string

Function-specific URL to send status callbacks and reports to. Takes precedence over a default setting. Authentication can also be set in the url in the format of username:password@url. Credentials embedded in the URL take precedence over web_hook_auth_user and web_hook_auth_password.

functions[].web_hook_auth_user
string

Username for basic auth on this function’s webhook. Falls back to defaults.web_hook_auth_user.

functions[].web_hook_auth_password
string

Password for basic auth on this function’s webhook. Falls back to defaults.web_hook_auth_password.

functions[].gates
object[]

Semantic gates: 1 to 8 yes/no questions about the call that must all pass before the function runs. When any gate fails, the function doesn’t run, and the AI gets the first failing gate’s on_fail response instead. A gated function needs a description and a web_hook_url (its own or from SWAIG.defaults) or a data_map. If any gate is invalid, the function isn’t added to the agent at all.

See gates for additional details.

functions[].gate_fillers
object

What the agent says while the function’s gates are checked, shaped like fillers. Ignored on a function without gates.

functions[].fillers_at_gates
boolean

For a gated function without gate_fillers, whether its fillers play as soon as the AI calls it, during the gate check (true), or only after the gates pass (false). Set it to false when the function’s fillers announce the action (“OK, cancelling your account now”), so the caller doesn’t hear them before a gate blocks it. When unset, the function inherits SWAIG.defaults.fillers_at_gates, which is true unless you change it.

functions[].visual_input
boolean | integer

How many of the caller’s newest video frames your web_hook_url receives with the function call, as visual_input in the request body: true is 1, a whole number from 0 to 5, and false or 0 is none. On a gated function, the frames are taken when its gates are checked, never later, and a blocked call sends none. Ignored on a data_map function.

functions[].purpose
stringDeprecated

Deprecated. Use description instead. When a function sets both, purpose is used.

functions[].argument
objectDeprecated

Deprecated. Use parameters instead.

functions[].web_hook_auth_pass
stringDeprecated

Password for basic auth on this function’s webhook. Deprecated — use web_hook_auth_password instead. The alias is used only when web_hook_auth_password is not set.

Tool webhook

When the agent calls one of your SWAIG functions, the platform sends an HTTP POST to that function’s web_hook_url (or the SWAIG defaults.web_hook_url). Your endpoint runs the function and returns a JSON object with a response (the result the AI reads next) and, optionally, an action — a single object or an array — telling the agent what to do. Every field is optional.

Request

functionstringRequired
The name of the function the AI is calling.
argumentobjectRequired
The arguments the AI passed to your function.
argument_descobjectRequired

The function's parameter definition, as you declared it in parameters.

descriptionstringRequired

The description you gave the function in SWAIG.functions.

call_idstringRequired
The ID of the call. On a chat conversation this carries the conversation id instead.
ai_session_idstringRequired
The ID of the AI session on the call.
app_namestringRequired
The name of your AI application.
meta_data_tokenstringRequired

The token that scopes meta_data. This is the meta_data_token you set on the function, or a value derived from the function's web_hook_url and credentials when you did not set one.

meta_dataobjectRequired

Metadata scoped to meta_data_token. An empty object when the function has none yet.

channel_activebooleanRequired
Whether the call is still up.
channel_offhookbooleanRequired
Whether the call is answered.
channel_readybooleanRequired
Whether the AI session is ready to take actions.
content_typestringRequired

The content type of the request body. Always text/swaig.

versionstringRequired
The SWAIG protocol version.
content_dispositionstringRequired

How the body is delivered. Always SWAIG Function.

conversation_idstringOptional

The conversation ID, when the AI session has one. A text conversation held over the AI chat endpoint always has one, and it matches call_id.

global_dataobjectOptional

The AI session's current global_data, when it has any.

caller_id_namestringOptional
The caller's name, when available.
caller_id_numstringOptional
The caller's number, when available.
conversation_typeenumOptional

The kind of conversation the agent is running: a call, or a text conversation held over the AI chat endpoint. Not included on function calls made during a call.

Allowed values:
project_idstringOptional
Your project ID, when available.
space_idstringOptional
Your Space ID, when available.
fatal_errorbooleanOptional

true when the AI session has hit an unrecoverable error. Included only in that case.

error_reasonstringOptional

A description of the error. Included only when fatal_error is set.

SWMLVarsobjectOptional

SWML variables for the call. Included when you enable swaig_post_swml_vars.

SWMLCallobjectOptional

SWML call state. Included when you enable swaig_post_swml_vars.

call_loglist of objectsOptional

The conversation so far, with sensitive values redacted. Included when you enable swaig_post_conversation.

raw_call_loglist of objectsOptional

The full, unredacted conversation so far. Included when you enable swaig_post_conversation.

visual_inputlist of objectsOptional

The caller's newest video frames, oldest first, up to the function's visual_input count. Included only for a function that sets visual_input; an empty array when the call has no video, and for functions called after the call ends. On a gated function, the frames are taken when its gates are checked, never later.

visual_input_evaluation_idstringOptional

The evaluation_id of the semantic gate check that judged the frames in visual_input. Included when one of the function's gates looked at them.

Payload
{
"function": "get_weather",
"argument": {
"parsed": [
{
"city": "San Francisco"
}
],
"raw": "{\"city\":\"San Francisco\"}"
},
"argument_desc": {
"properties": {
"city": {
"description": "Name of the city",
"type": "string"
}
},
"required": [
"city"
],
"type": "object"
},
"description": "Look up the current weather for a city.",
"call_id": "2e1e66e5-5d07-413d-9668-55542992eec0",
"ai_session_id": "a0d4e6e5-5d07-413d-9668-55542992eec0",
"app_name": "ai",
"meta_data_token": "my-token",
"meta_data": {
"order_number": "12345"
},
"channel_active": true,
"channel_offhook": true,
"channel_ready": true,
"content_type": "text/swaig",
"version": "2.0",
"content_disposition": "SWAIG Function"
}

See the AI SWAIG tool webhook webhook page for the full field reference.

Reply

Return a JSON object with an optional response and an optional action. The response is the result the AI reads next, and action carries objects that change what the call does next.

response
string | object

The result the agent reads next. A string becomes the tool message as written. An object splits the result into tool_result, the data the agent reads, and tool_prompt, an instruction added right after it. When omitted, the agent gets a default result.

post_process
booleanDefaults to false

If true, the actions run after the agent has spoken its reply rather than immediately.

action
object | object[]

An action, or a list of actions, for the platform to perform on the call.

action[].SWML
object

A SWML object to be executed.

action[].say
string

A message to be spoken by the AI agent.

action[].stop
boolean

Whether to stop the conversation.

action[].hangup
boolean

Whether to hang up the call. When set to true, the call will be terminated after the AI agent finishes speaking.

action[].hold
integer | object

Places the caller on hold while playing hold music (configured via the params.hold_music parameter). During hold, speech detection is paused and the AI agent will not respond to the caller.

The value specifies the hold timeout in seconds. Can be:

  • An integer (e.g., 120 for 120 seconds)
  • An object with timeout, and optionally step and timeout_step

Default timeout is 300 seconds (5 minutes). Maximum timeout is 900 seconds (15 minutes).

Unholding a call

There is no unhold SWAIG action because the AI agent is inactive during hold and cannot process actions. To take a caller off hold, either:

  • Let the hold timeout expire (the AI will automatically resume with a default message), or
  • Use the Calling API ai_unhold command to programmatically unhold the call with a custom prompt.
hold.timeout
integerDefaults to 300

The duration to hold the caller in seconds. Maximum is 900 seconds (15 minutes).

hold.step
string

The step to move to when the caller is taken off hold. When omitted, the conversation stays on the current step. Applies only if the hold starts.

hold.timeout_step
string

The step to move to when the hold times out without the caller being taken off hold. When omitted, the conversation stays on the current step. Applies only if the hold starts.

action[].change_context
string

The name of the context to switch to. The context must be defined in the AI’s prompt.contexts configuration. This action triggers an immediate context switch during the execution of a SWAIG function.

Visit the contexts documentation for details on defining contexts.

action[].change_step
string

The name of the step to switch to. The step must be defined in prompt.contexts.{context_name}.steps for the current context. This action triggers an immediate step transition during the execution of a SWAIG function.

Visit the steps documentation for details on defining steps.

action[].toggle_functions
object[]

An array of objects to toggle SWAIG functions on or off during the conversation. Each object identifies a function by name and sets its active state. A function can only toggle functions that have the same meta_data_token as itself.

See toggle_functions for additional details.

toggle_functions[].function
stringRequired

The name of the SWAIG function to toggle. One name per object; to toggle several functions, add an object for each.

toggle_functions[].active
booleanDefaults to true

Whether to activate or deactivate the function.

action[].set_global_data
object

A JSON object containing any global data, as a key-value map. This action sets the data in the global_data to be globally referenced.

action[].set_meta_data
object

A JSON object containing any metadata, as a key-value map. This action sets the data in the meta_data to be referenced locally in the function.

See set_meta_data for additional details.

action[].unset_global_data
string | string[]

The key to remove from global_data, or an array of keys to remove.

action[].unset_meta_data
string | string[]

The key to remove from the function’s meta_data, or an array of keys to remove.

action[].playback_bg
object

A JSON object containing the audio file to play.

playback_bg.file
string

URL or filepath of the audio file to play. Authentication can also be set in the url in the format of username:password@url.

playback_bg.wait
booleanDefaults to false

Whether to wait for the audio file to finish playing before continuing.

action[].stop_playback_bg
boolean

Stops the background audio file. The action runs whenever the key is present, whatever its value, so false also stops playback. Leave the key out to keep playing.

action[].user_input
string

Used to inject text into the users queue as if they input the data themselves.

action[].context_switch
object

Replaces the agent’s instructions mid-conversation. You can use variables in system_prompt, user_prompt, system_pom, and user_pom. In a data_map function they’re expanded along with the rest of the output; in a reply from your web_hook_url, they’re expanded when the action runs. Either way they’re expanded only once, so a value that itself contains ${...} is left as it is.

See context_switch for additional details.

context_switch.system_prompt
string

The new instructions for the agent. Set this or system_pom.

context_switch.system_pom
object

The new instructions as an object with text or pom, the same shape as a prompt. Used instead of system_prompt when both are set.

context_switch.consolidate
booleanDefaults to false

If true, the conversation so far is summarized and the summary is carried into the new context. When the caller hasn’t spoken yet there’s nothing to summarize, so full_reset is ignored.

context_switch.full_reset
booleanDefaults to false

If true, the new instructions replace the agent’s original prompt. Otherwise the conversation is cleared and the new instructions are added.

context_switch.user_prompt
string

A string serving as simulated user input for the AI Agent. During a context_switch in the AI’s prompt, the user_prompt offers the AI pre-established context or guidance.

context_switch.user_pom
object

The simulated user input as an object with text or pom. Used instead of user_prompt when both are set.

action[].settings
object

Changes the AI model’s settings for the rest of the conversation, as a key-value map. Only settings the selected model supports are applied. Requires params.swaig_allow_settings, which is on by default. The new map replaces the current settings: any setting you leave out, including one set on prompt, returns to the model’s default.

action[].user_event
object

Sends a custom event to the client connected to the call, with this object as its payload, like the user_event method.

action[].transfer
boolean | string | object

Transfer the call to a new destination. Accepts two forms depending on whether it accompanies a sibling action[].SWML payload:

  • Boolean — use alongside a sibling action[].SWML payload in the same action object. When true, ends the AI session and hard-transfers the call to that SWML. When omitted or false, the SWML executes inline and the AI session continues afterward.
  • Object or string — use on its own, without SWML, to end the AI session and transfer the call to the destination configured with the fields below. A string is shorthand for transfer.dest.
transfer.dest
stringRequired

The destination to transfer to: the name of a section in the current SWML document, or a URL that returns SWML to execute.

transfer.summarize
booleanDefaults to false

Whether to include a conversation summary when transferring.

{
"response": "Oh wow, it's 82.0°F in Tulsa. Bet you didn't see that coming! Humidity at 38%. Your hair is going to love this! Wind speed is 2.2 mph. Hold onto your hats, or don't, I'm not your mother! Looks like Sunny. Guess you'll survive another day.",
"action": [
{
"set_meta_data": {
"temperature": 82.0,
"humidity": 38,
"wind_speed": 2.2,
"weather": "Sunny"
}
},
{
"SWML": {
"version": "1.0.0",
"sections": {
"main": [
{
"play": {
"url": "https://example.com/twister.mp3"
}
}
]
}
}
}
]
}

Variables

SWAIG functions don’t set SWML variables of their own. The ai method sets ai_result when the agent ends.

Reserved Functions

Reserved functions are special SignalWire functions that are automatically triggered at specific points during a conversation. You define them just like any other SWAIG function, but their names correspond to built-in logic on the SignalWire platform, allowing them to perform specific actions at the appropriate time.

Function name conflicts

Do not use reserved function names for your own SWAIG functions unless you want to use the reserved function’s built-in behavior. Otherwise, your function may not work as expected.

List of Reserved Functions

startup_hook
function

Runs when the conversation starts. Sends the set properties of the function to the defined web_hook_url.

hangup_hook
function

Runs when the conversation ends. Sends the set properties of the function to the defined web_hook_url.

summarize_conversation
function

Triggered when the call is ended. The post_prompt must be defined for this function to be triggered. Provides a summary of the conversation and any set properties to the defined web_hook_url.

Where are my function properties?

If the AI is not returning the properties you set in your SWAIG function, it may be because a reserved function was triggered before those properties were available. To ensure your function receives all necessary information, make sure the AI has access to the required property values before the reserved function is called. Any property missing at the time the reserved function runs will not be included in the data sent back.

Diagram examples

SWML Examples

Using SWAIG Functions

version: 1.0.0
sections:
main:
- ai:
post_prompt_url: "https://example.com/my-api"
prompt:
text: |
You are a helpful assistant that can provide information to users about a destination.
At the start of the conversation, always ask the user for their name.
You can use the appropriate function to get the phone number, address,
or weather information.
post_prompt:
text: "Summarize the conversation."
SWAIG:
includes:
- functions:
- get_phone_number
- get_address
url: https://example.com/functions
auth_user: me
auth_password: secret
defaults:
web_hook_url: https://example.com/my-webhook
web_hook_auth_user: me
web_hook_auth_password: secret
functions:
- function: get_weather
description: To determine what the current weather is in a provided location.
parameters:
properties:
location:
type: string
description: The name of the city to find the weather from.
type: object
- function: summarize_conversation
description: Summarize the conversation.
parameters:
type: object
properties:
name:
type: string
description: The name of the user.