ai
Creates an AI agent that conducts voice conversations using automatic speech recognition (ASR), large language models (LLMs), and text-to-speech (TTS) synthesis. The agent processes caller speech in real-time, generates contextually appropriate responses, and can execute custom functions to interact with external systems and databases through SignalWire AI Gateway (SWAIG).
Since the prompt configuration is central to AI agent behavior, it is recommended to read the prompt engineering guide.
Properties
ai
An object that defines an AI agent for conducting voice conversations. Accepts the following properties to configure the agent’s prompt, behavior, functions, language support, and other settings.
ai.prompt
Defines the AI agent’s personality, goals, behaviors, and instructions for handling conversations.
The prompt establishes how the agent should interact with callers, what information it should gather,
and how it should respond to various scenarios. Required unless you set ai.agent instead.
It is recommended to write prompts using markdown formatting as LLMs better understand structured content. It is also recommended to read the prompt engineering guide.
ai.agent
The ID of an AI agent saved in your SignalWire Space, such as the agent_id of an AI agent created with the
REST API. Use it instead of an inline configuration: the agent runs with its saved settings.
Set agent on its own. Other ai properties placed next to it are ignored. Don’t
combine agent with prompt: if both are present, the inline configuration runs and agent is ignored.
ai.global_data
A key-value object for storing data that persists throughout the AI session.
Can be set initially in the SWML script or modified during the conversation using the set_global_data action.
The global_data object is accessible everywhere in the AI session: prompts, AI parameters,
and SWML returned from SWAIG functions.
Access properties using template strings (e.g ${global_data.property_name})
ai.hints
An array of strings and objects that help the agent understand what the caller says.
Hints don’t change how the agent speaks; to change pronunciation, use ai.pronounce.
Hints as strings: Each string is passed to speech recognition as a hint, making that word or phrase more likely to be recognized. Recognition isn’t guaranteed.
Hints as objects: Each object is a recognition hint (hint), a rewrite rule applied to the text the agent hears
before the AI reads it (pattern with replace), or both.
One hint list covers every language the agent uses; there are no per-language hints. When speech recognition
runs in code-switching mode, because ai.multilingual is set or a languages entry’s
code is multi on Deepgram, recognition hints are not used. That covers string entries and hint values, and no
error is returned. Outside code-switching mode, every engine receives hints. Rewrite rules still apply to the
transcript in every mode.
hints[].hint
A word or phrase passed to speech recognition as a hint, the same as a string entry. Required when pattern and replace are not set.
hints[].pattern
A regular expression matched against the text the agent hears. Set it together with replace; each match is replaced with replace before the AI reads the text.
hints[].replace
The text that replaces each match of pattern. Set it together with pattern.
hints[].ignore_case
If true, pattern is matched case-insensitively.
ai.languages
An array of JSON objects defining supported languages in the conversation.
Mutually exclusive with ai.multilingual.
See languages for more details.
ai.multilingual
Configures a single agent to detect the caller’s language and answer in it, switching as the caller switches.
Mutually exclusive with ai.languages: if both are set, ai.multilingual is used and ai.languages is ignored.
See multilingual for more details.
ai.params
A JSON object containing parameters as key-value pairs.
See params for more details.
ai.post_prompt
The final set of instructions and configuration settings to send to the agent.
Set either text or pom for the instructions, the same way as in prompt.
post_prompt.text
The instructions to send to the agent, as a single block of text. Required unless pom is set.
post_prompt.pom
The instructions to send to the agent, as a Prompt Object Model (POM): an array of sections that SignalWire
renders into a markdown document. Required unless text is set. Sections take the same properties as
prompt.pom.
post_prompt.model
The AI model that writes the summary. When omitted, the agent’s model is used. Name a model from the
supported models, optionally as model@provider.
Any other name runs as gpt-4o-mini, and the call gets a non-fatal calling.error whose error is
post_model_substituted. If no model is available at all, the agent doesn’t start.
post_prompt.reasoning_effort
How much the summary model reasons before it replies. The accepted values and the default depend on the model; see reasoning and verbosity.
post_prompt.verbosity
How long the summary model’s replies are: low, medium, or high. Any other value uses the default. Applies to
the models listed in reasoning and verbosity.
post_prompt.max_tokens
Limits the number of tokens the AI model may generate for the summary.
post_prompt.temperature
Randomness setting. Float value between 0.0 and 2.0. Closer to 0 will make the output less random.
On a model that takes reasoning_effort, it applies only when reasoning_effort is none.
post_prompt.top_p
Randomness setting. Alternative to temperature. Float value between 0.0 and 1.0. Closer to 0 will make the output less random.
On a model that takes reasoning_effort, it applies only when reasoning_effort is none.
post_prompt.confidence
Has no effect and can be omitted.
post_prompt.presence_penalty
Aversion to staying on topic. Float value between -2.0 and 2.0. Positive values increase the model’s likelihood to talk about new topics. Not used on the models listed in reasoning and verbosity.
post_prompt.frequency_penalty
Aversion to repeating lines. Float value between -2.0 and 2.0. Positive values decrease the model’s likelihood to repeat the same line verbatim. Not used on the models listed in reasoning and verbosity.
ai.post_prompt_url
The URL to which to send status callbacks and reports. Authentication can also be set in the url in the format of username:password@url.
See post_prompt_url callback below.
ai.post_prompt_auth_user
The username for basic authentication on post_prompt_url. A username embedded in the URL takes precedence.
ai.post_prompt_auth_password
The password for basic authentication on post_prompt_url. A password embedded in the URL takes precedence.
ai.pronounce
An array of objects to clarify the AI’s pronunciation of certain words or expressions.
pronounce[].replace
The expression to replace.
pronounce[].with
The phonetic spelling of the expression.
pronounce[].ignore_case
Whether the pronunciation replacement should ignore case. Only the boolean true turns it on.
ai.SWAIG
An object that configures the functions the agent can call during the dialogue: user-defined functions,
shared defaults, includes, native_functions, mcp_servers, and internal_fillers.
See SWAIG for more details.
ai.voice
The voice the agent speaks with when neither ai.languages nor ai.multilingual is set.
Format: <engine id>.<voice id>, with an optional :<model id> suffix for engines that offer a choice of model,
the same as languages[].voice.
Supply an array of up to 8 voice strings to give the voice backups. The first voice speaks, and when it fails, the agent goes on with the next voice that opens. See Voice fallback.
When ai.languages or ai.multilingual is set, the voices configured there are used instead. With
ai.languages, only this voice’s engine prefix is read, as the engine the entries inherit.
With no voice set anywhere, the agent speaks with the default_voice SWML variable when
it’s set, otherwise with the platform’s default voice, elevenlabs.mark.
ai.fallback_voice
Backup voices shared by every language: one voice string, or an array of up to 8. They’re tried in order once
a language’s own voices have all failed, or the ai.voice array has, for an agent without ai.languages or
ai.multilingual. After them comes the platform’s last-resort voice. See Voice fallback.
Variables
- ai_result: (out)
success|failed. Whether the agent ran.failedalso covers anagentvalue that isn’t a valid ID. Whenagentruns a saved agent, this method doesn’t setai_resultitself. - default_voice: (in) The voices for any voice field the method leaves out. See Default voices.
post_prompt_url callback
SignalWire sends the report to your post_prompt_url as an HTTP POST, signed with your project’s
signing key so your endpoint can confirm it came from SignalWire. See
Verify SWML request signatures.
See the AI post-prompt callback webhook page for the full field reference. To use the report for debugging and analysis, such as replaying the conversation, inspecting each SWAIG function call, or measuring latency, see Conversation analytics.
Responding to post prompt requests
Answer the end-of-call report with any 2xx status. The body is not read, so {"response": "ok"} is
as good as an empty one.
The one request that does read your response is the conversation fetch, which arrives at the same
URL with "action": "fetch_conversation" when the agent starts with a
conversation_id and
save_conversation enabled.
Return the summary you stored for that conversation_id under a conversation_summary key, and the
agent resumes with it:
Voice fallback
Every voice field takes one voice string or an array of up to 8: ai.voice, languages[].voice,
multilingual.languages[].voice, and ai.fallback_voice. The first voice of an array speaks.
When the speaking voice fails to open, or fails during the call, the agent goes on with the next voice that
opens instead of going silent or ending the call.
Each language tries its voices in this order:
- The rest of its own array, in order. For an agent without
languagesormultilingual, that’s theai.voicearray. ai.fallback_voice: one voice, or its array in order.- The platform’s last-resort voice, Cartesia’s Kurt (
cartesia.efa653e5-314d-46ca-9f90-70ac7d6ca71e).
Every language shares the same fallback_voice voices and the same last-resort voice. The last-resort voice
differs from the platform’s default voice, ElevenLabs Mark, which speaks only when an agent names no voice at all.
To keep Mark ahead of Kurt, name elevenlabs.mark in fallback_voice.
An array’s elements follow these rules:
- Each element is a voice string,
<engine>.<voice>or<engine>.<voice>:<model>. An element that isn’t a non-empty string is dropped. voice,languages[].voice, andmultilingual.languages[].voicehold the speaking voice and up to 7 backups;fallback_voiceholds up to 8 voices. Elements past the eighth are dropped.- A string that holds a JSON array, such as
"[\"elevenlabs.rachel\",\"openai.alloy\"]", is read as that array. This helps when the value comes from a SWML variable. - An element after the first with no engine takes the engine its name implies: a Google-shaped name such as
en-US-Neural2-Cis a Google Cloud voice, and any other name uses ElevenLabs. Name the engine on every element to be sure. - Only the first voice takes the entry’s
modelandparams. The voices after it are opened from their own voice strings alone.
During the call:
- The agent only moves forward through the voices. A voice that failed isn’t tried again, except the last-resort voice, which is tried again on a fresh connection once the rest are used up.
- A voice that won’t open is passed over at once, and the next is tried.
- A backup voice speaks in the caller’s language, except a Google Cloud or Azure voice, whose name carries its own locale:
gcloud.en-US-Neural2-Cspeaksen-US. - The caller hears a short line at each recovery: “Excuse me, I need to adjust something.” at startup and for most failures, “Sorry about that, let me get myself sorted out.” when the engine fails while being sent text, and “One moment.” when a streaming voice stalls.
- A streaming voice that goes silent mid-sentence is first reopened as the same voice without streaming, and the agent moves on only if that fails. A voice that opens a reply’s first sentence but sends no audio for 3 seconds is tried once more, then treated as failed.
- Each voice change fires a
calling.errorevent that names the failed voice and the voice that took over. - Text sent to a voice that failed before speaking it isn’t billed.
The call ends when no voice opens, with the reason voice_open_failed, or when voices keep failing without
producing audio, with the reason voice_unrecoverable. That happens after as many voice changes without audio
as there are voices to try, plus one, and never fewer than 2; audio from any voice resets the count. Both
reasons are reported to the hangup_hook function.
A SWAIG function can change the voice mid-call with the
change_voice action. An array there replaces the current language’s own voices;
a single voice keeps the language’s backups. Either way, the agent starts again from the first voice.
Default voices
The default_voice SWML variable gives the voice for every voice field the ai method leaves out, so one
script can set the voices for several agents, or for each language, in one place. Set it before the ai
method runs, for example with set. The method reads it once, when it
starts, so a later change applies to the next ai method.
Its value can be:
- one voice string;
- an array of voice strings, read the same way as a voice field’s array;
- an object whose keys are language codes and whose values are a voice string or an array, with an optional
defaultkey; - text that holds a JSON array or object, which is read as that JSON.
For an object, a language takes the entry for its exact code, then for its base language (es for es-MX),
then default. Keys match in any case.
The variable fills:
ai.voice, when the method has novoice,languages, ormultilingual, looked up asen-US;- each
languages[]entry with novoice, looked up by the entry’s firstcode; - each
multilingual.languages[]entry with novoice, looked up by itslanguage; - a
multilingualobject with nolanguagesat all: each key of the object becomes an entry, withdefaultas the catch-all, and a string or array becomes a single catch-all entry. The keys must then be language codes the multilingual speech recognizer accepts.
A voice set on the method always wins over the variable, and the variable wins over the platform’s default
voice. Without the variable, a languages[] entry with no voice is skipped. The variable doesn’t fill
fallback_voice, change_voice, or live_translate voices.
Examples
Minimal AI agent
Saved AI agent
Hints
Pronounce
Backup voices
The agent speaks with ElevenLabs Rachel. If Rachel fails, it goes on with Cartesia’s Kurt, then OpenAI Alloy,
then Google en-US-Neural2-C, then the platform’s last-resort voice.
Default voices per language
English (en-US) takes the en entry, Spanish (es-MX) the es entry, and French the default entry.