Contexts and Workflows
The features in this chapter build on the fundamentals covered earlier. While basic agents handle free-form conversations well, many real-world applications require more structure: guided workflows that ensure certain information is collected, the ability to transfer between different “departments” or personas, recording for compliance, and integration with knowledge bases.
These advanced features transform simple voice agents into sophisticated conversational applications capable of handling complex business processes.
When to Use Contexts
Contexts are the SDK’s answer to a common challenge: how do you ensure a conversation follows a specific path? Regular prompts work well for open-ended conversations, but many business processes require structure — collecting specific information in a specific order, or routing callers through a defined workflow.
Think of contexts as conversation “states” or “modes.” Each context can have its own persona, available functions, and series of steps. The AI automatically manages transitions between contexts and steps based on criteria you define.
Use contexts when you need:
- Guaranteed step completion
- Controlled navigation
- Step-specific function access
- Context-dependent personas
- Department transfers
- Isolated conversation segments
Common context patterns:
- Data collection wizard: Gather customer information step-by-step (name, address, payment)
- Triage flow: Qualify callers before routing to appropriate department
- Multi-department support: Sales, Support, and Billing each with their own persona
- Appointment scheduling: Check availability, select time, confirm details
- Order processing: Select items, confirm order, process payment
Context Architecture
Understanding how contexts, steps, and navigation work together is essential for building effective workflows.
Key concepts:
- ContextBuilder: The top-level container that holds all your contexts
- Context: A distinct conversation mode (like “sales” or “support”), with its own persona and settings
- Step: A specific point within a context where certain tasks must be completed
The AI automatically tracks which context and step the conversation is in. When step criteria are met, it advances to the next allowed step. When context navigation is permitted and appropriate, it switches contexts entirely.

How state flows through contexts:
- Caller starts in the first step of the default (or specified) context
- AI follows the step’s instructions until
step_criteriais satisfied - AI chooses from
valid_stepsto advance within the context - If
valid_contextsallows, AI can switch to a different context entirely - When switching contexts,
isolated,consolidate, orfull_resetsettings control what conversation history carries over
Basic Context Example
Python
TypeScript
Compact Step Definition
Steps can be fully configured in a single add_step() call using keyword arguments:
The task parameter creates a POM section titled “Task” with the given text. The bullets, criteria, functions, and valid_steps parameters map to add_bullets(), set_step_criteria(), set_functions(), and set_valid_steps() respectively. You can still use the method-chaining API when you need more control.
Step Configuration
set_text()
Simple text prompt for the step:
add_section() / add_bullets()
POM-style structured prompts:
set_step_criteria()
Define when the step is complete:
set_valid_steps()
Control step navigation:
set_functions()
Restrict available functions per step:
set_valid_contexts()
Allow navigation to other contexts:
Step Behavior Flags
Control how steps transition without relying on criteria evaluation:
These flags are useful for non-interactive steps like announcements, automated transitions, or terminal farewell steps.
Understanding Step Criteria
Step criteria tell the AI when a step is “complete” and it’s time to move on. Writing good criteria is crucial — too vague and the AI may advance prematurely; too strict and the conversation may get stuck.
Good criteria are:
- Specific and measurable
- Phrased as completion conditions
- Focused on what information has been collected
Examples of well-written criteria:
Problematic criteria to avoid:
Context Configuration
set_isolated()
Truncate conversation history when entering:
set_system_prompt()
New system prompt when entering context:
set_user_prompt()
Inject a user message when entering:
set_consolidate()
Summarize previous conversation when switching:
set_full_reset()
Completely reset conversation state:
add_enter_filler() / add_exit_filler()
Add transition phrases:
Multi-Context Example
Navigation Flow
Within Context (Steps)
set_valid_steps(["next"])- Go to next sequential stepset_valid_steps(["step_name"])- Go to specific stepset_valid_steps(["a", "b"])- Multiple options
Context-Level Valid Steps
You can set valid_steps at the context level to apply default step navigation for all steps in the context. Step-level valid_steps overrides context-level when set.
Between Contexts
set_valid_contexts(["other_context"])- Allow context switch- AI calls
change_context("context_name")automatically - Enter/exit fillers provide smooth transitions
Context Entry Behavior
isolated=True- Clear conversation historyconsolidate=True- Summarize previous conversationfull_reset=True- Complete prompt replacement
Context Manipulation
After defining contexts and steps, you can retrieve, modify, reorder, or remove them programmatically.
Retrieving Contexts and Steps
Removing and Reordering Steps
Clearing Step Content
Use clear_sections() to remove all POM sections and text from a step so you can repopulate it:
Gather Info Steps
The gather info system lets you define structured question sequences within a step. Questions are presented one at a time via the SignalWire platform’s built-in gather mechanism, producing zero tool_call/tool_result entries in the LLM conversation history. This keeps the history clean and focused.
Basic Gather Info
Gather Info Parameters
Gather Question Parameters
When to Use Gather Info vs Regular Steps
Validation Rules
The ContextBuilder validates your configuration:
- Single context must be named “default”
- Every context must have at least one step
valid_stepsmust reference existing steps (or “next”)valid_contextsmust reference existing contexts- Cannot mix
set_text()withadd_section()on same step - Cannot mix
set_prompt()withadd_section()on same context
Step and Context Methods Summary
Context Switching Behavior
When the AI switches between contexts, several things can happen depending on your configuration. Understanding these options helps you create smooth transitions.
Isolated Contexts
When isolated=True, the conversation history is cleared when entering the context. This is useful when:
- You want a clean slate for a new department
- Previous context shouldn’t influence the new persona
- You’re implementing strict separation between workflow segments
The caller won’t notice — the AI simply starts fresh with no memory of the previous context.
Consolidated Contexts
When consolidate=True, the AI summarizes the previous conversation before switching. This preserves important information without carrying over the full history:
The summary includes key facts and decisions, giving the new context awareness of what happened without the full transcript.
Full Reset Contexts
full_reset=True goes further than isolation — it completely replaces the system prompt and clears all state:
Use this when the new context needs to behave as if it were a completely different agent.
Combining with Enter/Exit Fillers
Fillers provide audio feedback during context switches, making transitions feel natural:
The AI randomly selects from the filler options, providing variety in the transitions.
Debugging Context Flows
When contexts don’t behave as expected, use these debugging strategies:
-
Check step criteria: If stuck on a step, the criteria may be too strict. Temporarily loosen them to verify the flow works.
-
Verify navigation paths: Ensure
valid_stepsandvalid_contextsform a complete graph. Every step should have somewhere to go (unless it’s a terminal step). -
Test with swaig-test: The testing tool shows context configuration in the SWML output:
-
Add logging in handlers: If you have SWAIG functions, log when they’re called to trace the conversation flow.
-
Watch for validation errors: The ContextBuilder validates your configuration at runtime. Check logs for validation failures.
Best Practices
DO:
- Set clear step_criteria for each step
- Use isolated=True for persona changes
- Add enter_fillers for smooth transitions
- Define valid_contexts to enable department transfers
- Test navigation paths thoroughly
- Provide escape routes from every step (avoid dead ends)
- Use consolidate=True when context needs awareness of previous conversation
DON’T:
- Create circular navigation without exit paths
- Skip setting a base prompt before define_contexts()
- Mix set_text() with add_section() on the same step
- Forget to validate step/context references
- Use full_reset unless you truly need a complete persona change
- Make criteria too vague or too strict