***

title: define_contexts
slug: /reference/python/agents/agent-base/define-contexts
description: Define multi-step conversation contexts and workflows for complex agent interactions.
max-toc-depth: 3
---------------------

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

[contextbuilder]: /docs/server-sdks/reference/python/agents/context-builder

[ref-agentbase]: /docs/server-sdks/reference/python/agents/agent-base

Define contexts and steps for multi-step conversation workflows. Contexts allow an
agent to guide the caller through a structured sequence of interactions -- such as
gathering information, verifying identity, and then performing an action.

When called with an argument, sets the context configuration directly and returns
`self`. When called without arguments, returns a
[`ContextBuilder`][contextbuilder] for fluent
context definition.

<Note>
  Contexts can coexist with traditional prompts. The only restriction is that POM
  sections and raw text cannot be mixed in the main prompt.
</Note>

## **Parameters**

<ParamField path="contexts" type="Optional[dict | ContextBuilder]" toc={true}>
  Context configuration. Pass a dictionary or `ContextBuilder` to set contexts
  directly. Omit to receive a `ContextBuilder` for fluent definition.
</ParamField>

## **Returns**

[`AgentBase`][ref-agentbase] -- When `contexts` is provided.

[`ContextBuilder`][contextbuilder] -- When called
with no arguments.

## **Examples**

### Fluent ContextBuilder

```python {4}
from signalwire import AgentBase

agent = AgentBase(name="intake", route="/intake")
ctx = agent.define_contexts()
default = ctx.add_context("default")
default.add_step("greeting").set_text("Greet the caller and ask for their name.").set_step_criteria("The caller has provided their name.").set_functions(["lookup_account"]).set_valid_steps(["verify"])
default.add_step("verify").set_text("Verify the caller's identity.").set_step_criteria("Identity verified.").set_valid_steps(["assist"])
default.add_step("assist").set_text("Help the caller with their request.").set_functions(["search_orders", "process_return"])
agent.serve()
```

### Direct dict configuration

```python {4}
from signalwire import AgentBase

agent = AgentBase(name="intake", route="/intake")
agent.define_contexts({
    "default": {
        "steps": {
            "greeting": {
                "text": "Greet the caller.",
                "valid_steps": ["verify"]
            },
            "verify": {
                "text": "Verify identity.",
                "valid_steps": ["assist"]
            }
        }
    }
})
agent.serve()
```