add_context

View as MarkdownOpen in Claude

Create a new context and add it to the builder. Returns the Context object for method chaining.

Raises ValueError if the context name already exists or the maximum context limit (50) is exceeded.

A single-context agent must name its context "default" or validation will fail. Multi-context agents can use any names.

Parameters

name
strRequired

Unique context name. When using a single context, it must be named "default".

Returns

Context — The newly created context, ready for adding steps.

Examples

Single context

1from signalwire import AgentBase
2
3agent = AgentBase(name="order-agent", route="/order")
4
5contexts = agent.define_contexts()
6default = contexts.add_context("default")
7default.add_step("greet").set_text("Greet the caller and ask how you can help.")
8
9agent.serve()

Multiple contexts

1from signalwire import AgentBase
2
3agent = AgentBase(name="support-agent", route="/support")
4
5contexts = agent.define_contexts()
6main = contexts.add_context("default")
7sales = contexts.add_context("sales")
8support = contexts.add_context("support")
9
10main.add_step("menu").set_text("Ask whether the caller needs sales or support.")
11sales.add_step("qualify").set_text("Understand what product the caller wants.")
12support.add_step("diagnose").set_text("Understand the customer's issue.")
13
14agent.serve()