> Fetch clean Markdown by appending `.md` to any page URL under https://signalwire.com/docs or requesting it with the HTTP header `Accept: text/markdown`. The root index at https://signalwire.com/docs/llms.txt lists the available documentation indexes. # ContextBuilder > Build multi-step conversation workflows with structured contexts and steps. [context]: /docs/server-sdks/reference/typescript/agents/context-builder/context [step]: /docs/server-sdks/reference/typescript/agents/context-builder/step [agentbase]: /docs/server-sdks/reference/typescript/agents/agent-base [addcontext]: /docs/server-sdks/reference/typescript/agents/context-builder/add-context [createsimplecontext]: /docs/server-sdks/reference/typescript/agents/context-builder/create-simple-context [getcontext]: /docs/server-sdks/reference/typescript/agents/context-builder/get-context [todict]: /docs/server-sdks/reference/typescript/agents/context-builder/to-dict [validate]: /docs/server-sdks/reference/typescript/agents/context-builder/validate ContextBuilder is the top-level container for defining structured conversation workflows. It holds one or more [`Context`][context] objects, each containing a sequence of [`Step`][step] objects. Use it when your agent needs guided, multi-step conversations instead of free-form prompting. Access the builder by calling `defineContexts()` on an [`AgentBase`][agentbase] instance. The builder validates the entire context tree when the SWML document is rendered. > **Note** > > You rarely create a ContextBuilder directly. Call `defineContexts()` inside > your agent class, which creates the builder and wires it into SWML generation > automatically. ## **Methods** #### [addContext](/docs/server-sdks/reference/typescript/agents/context-builder/add-context) Create a new named context and add it to the builder. #### [getContext](/docs/server-sdks/reference/typescript/agents/context-builder/get-context) Retrieve an existing context by name. #### [reset](/docs/server-sdks/reference/typescript/agents/context-builder/reset) Remove all contexts, returning the builder to its initial state. #### [toDict](/docs/server-sdks/reference/typescript/agents/context-builder/to-dict) Convert all contexts to a dictionary for SWML generation. #### [validate](/docs/server-sdks/reference/typescript/agents/context-builder/validate) Validate the entire context configuration tree. #### [createSimpleContext](/docs/server-sdks/reference/typescript/agents/context-builder/create-simple-context) Helper function to create a standalone Context without a ContextBuilder. --- ## **Limits** | Limit | Value | | ---------------------------- | ----- | | Maximum contexts per builder | 50 | | Maximum steps per context | 100 | --- ## **Examples** ### Single context with sequential steps ```typescript {4} import { ContextBuilder } from '@signalwire/sdk'; const builder = new ContextBuilder(); const order = builder.addContext('default'); order.addStep('get_item') .setText('Ask what item they want to order.') .setStepCriteria('Customer has specified an item') .setValidSteps(['get_quantity']); order.addStep('get_quantity') .setText('Ask how many they want.') .setStepCriteria('Customer has specified a quantity') .setValidSteps(['confirm']); order.addStep('confirm') .setText('Confirm the order details and thank them.') .setStepCriteria('Order has been confirmed') .setEnd(true); const swml = builder.toDict(); console.log(JSON.stringify(swml, null, 2)); ``` ### Multiple contexts ```typescript {6,13,24} import { ContextBuilder } from '@signalwire/sdk'; const builder = new ContextBuilder(); // Main menu const main = builder.addContext('default'); main.addStep('menu') .setText('Ask whether they need sales, support, or billing help.') .setFunctions('none') .setValidContexts(['sales', 'support']); // Sales context const sales = builder.addContext('sales'); sales.setSystemPrompt('You are a friendly sales representative.'); sales.addStep('qualify') .setText('Understand what product the caller is interested in.') .setFunctions(['check_inventory', 'get_pricing']) .setValidSteps(['close']); sales.addStep('close') .setText('Close the sale or schedule a follow-up.') .setValidContexts(['default']); // Support context const support = builder.addContext('support'); support.setSystemPrompt('You are a patient support engineer.'); support.addStep('diagnose') .setText('Understand the customer\'s issue.') .setFunctions(['lookup_account', 'check_status']) .setValidSteps(['resolve']); support.addStep('resolve') .setText('Resolve the issue or escalate.') .setFunctions(['create_ticket', 'transfer_call']) .setValidContexts(['default']); ``` > Build multi-step conversation workflows with structured contexts and steps. ## Docs - [addContext](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/add-context.md): Create a new named context and add it to the builder. - [Context](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context.md): A named conversation workflow containing steps, prompts, and navigation rules. - [addBullets](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/add-bullets.md): Add a POM section with bullet points to the context prompt. - [addEnterFiller](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/add-enter-filler.md): Add enter fillers for a specific language. - [addExitFiller](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/add-exit-filler.md): Add exit fillers for a specific language. - [addSection](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/add-section.md): Add a POM section to the context prompt. - [addStep](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/add-step.md): Add a new step to this context. - [addSystemBullets](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/add-system-bullets.md): Add a POM section with bullets to the system prompt. - [addSystemSection](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/add-system-section.md): Add a POM section to the system prompt. - [getStep](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/get-step.md): Get an existing step by name. - [moveStep](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/move-step.md): Move an existing step to a specific position in the step order. - [removeStep](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/remove-step.md): Remove a step from this context. - [setConsolidate](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/set-consolidate.md): Set whether to consolidate conversation history when entering this context. - [setEnterFillers](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/set-enter-fillers.md): Set all enter fillers at once. - [setExitFillers](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/set-exit-fillers.md): Set all exit fillers at once. - [setFullReset](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/set-full-reset.md): Set whether to fully reset conversation history when entering this context. - [setHistory](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/set-history.md): Set the default history visibility mode for every step in this context. - [setInitialStep](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/set-initial-step.md): Set which step the context starts on when entered. - [setIsolated](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/set-isolated.md): Set whether this context is isolated from other contexts' conversation history. - [setPostPrompt](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/set-post-prompt.md): Override the post-prompt text while this context is active. - [setPrompt](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/set-prompt.md): Set the context's prompt text directly. - [setSystemPrompt](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/set-system-prompt.md): Set a new system prompt that takes effect when this context is entered. - [setUserPrompt](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/set-user-prompt.md): Inject a user message when entering this context. - [setValidContexts](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/set-valid-contexts.md): Set which contexts the agent can navigate to from this context. - [setValidSteps](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/context/set-valid-steps.md): Set which steps can be navigated to from any step in this context. - [createSimpleContext](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/create-simple-context.md): Helper function to create a standalone Context without a ContextBuilder. - [GatherInfo & GatherQuestion](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/gather-classes.md): Structured information gathering within context steps using the server-side gather_info system. - [getCompletionAction](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/get-completion-action.md): Return the completion action for a gather info operation. - [getContext](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/get-context.md): Retrieve an existing context by name. - [getGatherInfo](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/get-gather-info.md): Return the gather info for a step. - [getQuestions](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/get-questions.md): Return all questions in a gather info operation. - [getStepOrder](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/get-step-order.md): Return the ordered list of step names in a context. - [getSteps](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/get-steps.md): Return the map of all steps in a context. - [getValidContexts](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/get-valid-contexts.md): Return the list of valid context names for a context. - [reset](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/reset.md): Remove all contexts, returning the builder to its initial state. - [Step](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step.md): An individual step in a conversation context with prompt, criteria, and navigation. - [addBullets](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/add-bullets.md): Add a POM section with bullet points to the step. - [addGatherQuestion](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/add-gather-question.md): Add a question to this step's gather_info configuration. - [addSection](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/add-section.md): Add a POM section to the step. - [clearSections](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/clear-sections.md): Remove all POM sections and direct text from this step. - [setEnd](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/set-end.md): Mark this step as terminal for the step flow. - [setFunctions](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/set-functions.md): Set which SWAIG functions are available during this step. - [setGatherInfo](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/set-gather-info.md): Enable structured info gathering for this step. - [setHistory](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/set-history.md): Control how much of the prior conversation the model sees when this step begins. - [setResetConsolidate](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/set-reset-consolidate.md): Set whether to consolidate conversation history on context switch. - [setResetFullReset](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/set-reset-full-reset.md): Set whether to perform a full conversation reset at this step. - [setResetSystemPrompt](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/set-reset-system-prompt.md): Set a new system prompt for context switching from this step. - [setResetUserPrompt](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/set-reset-user-prompt.md): Set a user message to inject when this step triggers a context switch. - [setSkipToNextStep](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/set-skip-to-next-step.md): Automatically advance to the next step without evaluating criteria. - [setSkipUserTurn](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/set-skip-user-turn.md): Skip waiting for user input when entering this step. - [setStepCriteria](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/set-step-criteria.md): Define when this step is considered complete. - [setText](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/set-text.md): Set the step's prompt text directly. - [setValidContexts](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/set-valid-contexts.md): Set which contexts the agent can navigate to from this step. - [setValidSteps](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/step/set-valid-steps.md): Set which steps the agent can navigate to from this step. - [toDict](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/to-dict.md): Convert all contexts to a dictionary for SWML generation. - [validate](https://signalwire.com/docs/server-sdks/reference/typescript/agents/context-builder/validate.md): Validate the entire context configuration tree.