***

title: switch_context
slug: /reference/python/agents/function-result/switch-context
description: Perform an advanced context switch with prompt replacement and history control.
max-toc-depth: 3
---------------------

For a complete index of all SignalWire documentation pages, fetch https://signalwire.com/docs/llms.txt

[swml-change-context]: /docs/server-sdks/reference/python/agents/function-result/swml-change-context

[functionresult]: /docs/server-sdks/reference/python/agents/function-result

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()`][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**

<ParamField path="system_prompt" type="Optional[str]" default="None" toc={true}>
  New system prompt to replace the current one.
</ParamField>

<ParamField path="user_prompt" type="Optional[str]" default="None" toc={true}>
  A user message to inject into the conversation after the context switch.
</ParamField>

<ParamField path="consolidate" type="bool" default="False" toc={true}>
  When `True`, the existing conversation history is summarized into a condensed
  form before applying the new context. Reduces token usage on long conversations.
</ParamField>

<ParamField path="full_reset" type="bool" default="False" toc={true}>
  When `True`, performs a complete context reset, clearing all conversation
  history and starting fresh with the new prompt.
</ParamField>

## **Returns**

[`FunctionResult`][functionresult] — self, for chaining.

## **Examples**

### Simple Prompt Swap

```python {11}
from signalwire import AgentBase
from signalwire import FunctionResult

agent = AgentBase(name="my-agent", route="/agent")
agent.set_prompt_text("You are a helpful assistant.")

@agent.tool(name="switch_to_technical", description="Switch to technical support mode")
def switch_to_technical(args, raw_data):
    return (
        FunctionResult("Switching to technical support mode.")
        .switch_context(
            system_prompt="You are a technical support specialist. "
                         "Help the customer with their technical issue."
        )
    )

agent.serve()
```

### Full Reset with User Message

```python {11}
from signalwire import AgentBase
from signalwire import FunctionResult

agent = AgentBase(name="my-agent", route="/agent")
agent.set_prompt_text("You are a helpful assistant.")

@agent.tool(name="escalate_to_supervisor", description="Escalate to a supervisor")
def escalate_to_supervisor(args, raw_data):
    return (
        FunctionResult("Connecting you to a supervisor.")
        .switch_context(
            system_prompt="You are a customer service supervisor.",
            user_prompt="A customer has been escalated to you.",
            full_reset=True
        )
    )

agent.serve()
```