switch_context

View as MarkdownOpen in Claude

Perform an advanced context switch by replacing the system prompt, injecting a user message, or resetting the conversation entirely. This is more flexible than swml_change_context(), which switches to a pre-defined named context.

When only system_prompt is provided (no other arguments), it performs a simple string-based context switch. When multiple arguments are given, it constructs an object-based context switch with fine-grained control.

Parameters

system_prompt
Optional[str]Defaults to None

New system prompt to replace the current one.

user_prompt
Optional[str]Defaults to None

A user message to inject into the conversation after the context switch.

consolidate
boolDefaults to False

When True, the existing conversation history is summarized into a condensed form before applying the new context. Reduces token usage on long conversations.

full_reset
boolDefaults to False

When True, performs a complete context reset, clearing all conversation history and starting fresh with the new prompt.

Returns

FunctionResult — self, for chaining.

Examples

Simple Prompt Swap

1from signalwire import AgentBase
2from signalwire import FunctionResult
3
4agent = AgentBase(name="my-agent", route="/agent")
5agent.set_prompt_text("You are a helpful assistant.")
6
7@agent.tool(name="switch_to_technical", description="Switch to technical support mode")
8def switch_to_technical(args, raw_data):
9 return (
10 FunctionResult("Switching to technical support mode.")
11 .switch_context(
12 system_prompt="You are a technical support specialist. "
13 "Help the customer with their technical issue."
14 )
15 )
16
17agent.serve()

Full Reset with User Message

1from signalwire import AgentBase
2from signalwire import FunctionResult
3
4agent = AgentBase(name="my-agent", route="/agent")
5agent.set_prompt_text("You are a helpful assistant.")
6
7@agent.tool(name="escalate_to_supervisor", description="Escalate to a supervisor")
8def escalate_to_supervisor(args, raw_data):
9 return (
10 FunctionResult("Connecting you to a supervisor.")
11 .switch_context(
12 system_prompt="You are a customer service supervisor.",
13 user_prompt="A customer has been escalated to you.",
14 full_reset=True
15 )
16 )
17
18agent.serve()