AgentsAgentBase

define_contexts

View as MarkdownOpen in Claude

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 for fluent context definition.

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

Parameters

contexts
Optional[dict | ContextBuilder]

Context configuration. Pass a dictionary or ContextBuilder to set contexts directly. Omit to receive a ContextBuilder for fluent definition.

Returns

AgentBase — When contexts is provided.

ContextBuilder — When called with no arguments.

Examples

Fluent ContextBuilder

1from signalwire import AgentBase
2
3agent = AgentBase(name="intake", route="/intake")
4ctx = agent.define_contexts()
5default = ctx.add_context("default")
6default.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"])
7default.add_step("verify").set_text("Verify the caller's identity.").set_step_criteria("Identity verified.").set_valid_steps(["assist"])
8default.add_step("assist").set_text("Help the caller with their request.").set_functions(["search_orders", "process_return"])
9agent.serve()

Direct dict configuration

1from signalwire import AgentBase
2
3agent = AgentBase(name="intake", route="/intake")
4agent.define_contexts({
5 "default": {
6 "steps": {
7 "greeting": {
8 "text": "Greet the caller.",
9 "valid_steps": ["verify"]
10 },
11 "verify": {
12 "text": "Verify identity.",
13 "valid_steps": ["assist"]
14 }
15 }
16 }
17})
18agent.serve()