The contexts system enables multi-step, multi-persona conversations. Each context defines a system prompt and a sequence of steps. The AI navigates between steps within a context and can switch between contexts when allowed.
<?php
require 'vendor/autoload.php';
use SignalWire\Agent\AgentBase;
$agent = new AgentBase(name: 'sales-flow', route: '/sales');
// Base prompt (always present)
$agent->promptAddSection('Instructions', 'Follow the structured sales workflow.');
// Define contexts (fluent API)
$ctx = $agent->defineContexts();
$sales = $ctx->addContext('sales')
->setSystemPrompt('You are Franklin, a friendly computer sales consultant.')
->setConsolidate(true);
$sales->addStep(
'greeting',
task: 'Greet the customer and ask what they need.',
criteria: 'Customer has stated their general needs.',
valid_steps: ['needs_assessment'],
);
$sales->addStep(
'needs_assessment',
task: 'Ask about budget, use case, and requirements.',
criteria: 'Budget and use case are known.',
valid_steps: ['recommendation'],
)->setValidContexts(['support']);
$sales->addStep(
'recommendation',
task: 'Recommend a computer based on requirements.',
criteria: 'Customer has received a recommendation.',
)->setValidContexts(['support']);
$support = $ctx->addContext('support')
->setSystemPrompt('You are Rachael, a technical support specialist.')
->setFullReset(true);
$support->addStep(
'diagnose',
task: 'Help with technical questions or issues.',
criteria: 'Issue identified or question answered.',
)->setValidContexts(['sales']);
$agent->addLanguage(name: 'English', code: 'en-US', voice: 'inworld.Mark');
$agent->setParams(['ai_model' => 'gpt-4.1-nano']);
$agent->run();A Context returned by $ctx->addContext($name) is configured with chainable
setters:
| Method | Description |
|---|---|
setSystemPrompt(string) |
Override the system prompt when entering this context |
setConsolidate(bool) |
Summarize conversation history when entering (reduces token usage) |
setFullReset(bool) |
Clear conversation history entirely when entering |
addStep(string $name, ...) |
Add an ordered step; returns the Step for further chaining |
setConsolidate(true)-- The AI summarizes the conversation so far into a compact form. The new context starts with that summary, but the full verbatim history is discarded.setFullReset(true)-- The conversation history is wiped entirely. The new context starts fresh with no memory of the previous context.- Both false (default) -- Full conversation history carries over.
addStep() accepts the step name plus optional named parameters, and the
returned Step exposes further setters:
addStep() parameter / Step setter |
Description |
|---|---|
$name (required) |
Unique step identifier |
task: / setText(string) |
Instructions for the AI in this step |
criteria: / setStepCriteria(string) |
Completion criteria for advancing |
valid_steps: / setValidSteps(array) |
Steps the AI can move to next |
setValidContexts(array) |
Contexts the AI can switch to |
functions: / setFunctions(array|string) |
Restrict which SWAIG tools this step may call — an allow-list of tool names, or 'none' to disable tools for the step |
The AI automatically navigates based on:
- Within a context: When step criteria are met, the AI moves to the next step in
valid_steps - Between contexts: When
valid_contextsis specified, the AI can switch contexts
From a SWAIG function handler, use FunctionResult::switchContext():
use SignalWire\SWAIG\FunctionResult;
$agent->defineTool(
name: 'escalate',
description: 'Escalate to support',
parameters: ['type' => 'object', 'properties' => []],
handler: function (array $args, array $raw): FunctionResult {
$result = new FunctionResult('Connecting you with a specialist.');
$result->switchContext(
systemPrompt: 'You are a senior support specialist.',
consolidate: true,
);
return $result;
},
);Contexts enable different AI personalities:
$ctx->addContext('greeter')
->setSystemPrompt('You are a cheerful receptionist named Sam.')
->addStep(
'welcome',
task: 'Welcome the caller and ask how you can help.',
criteria: 'Caller has stated their need.',
)->setValidContexts(['sales', 'support']);
$ctx->addContext('sales')
->setSystemPrompt('You are Alex, a knowledgeable sales advisor.')
->setConsolidate(true)
->addStep(
'qualify',
task: 'Understand the customer needs and recommend solutions.',
);- Always define a base prompt on the agent -- contexts augment it, not replace it
- Use
setConsolidate(true)for natural transitions where context matters - Use
setFullReset(true)for clean handoffs to a different persona - Keep step criteria specific and measurable
- Limit valid_steps and valid_contexts to prevent the AI from jumping randomly