Custom System Prompt Sections
Add your own computed section to the bot's system prompt with Python. Use it for per-conversation context the bot should always have — loyalty tier, cart state, what the customer picked on your website, or a caller's orders on a phone call.
Most of what the bot knows is either static (your business description, tone, rules) or fetched on demand when it decides to call an action. A prompt section is for the third case: context that changes per conversation and that the bot should have before it decides anything.
It is Python you write once per business. It runs before every reply on the channels you choose, and whatever it returns is appended to the system prompt as an extra section.
Orders and subscriptions are not loaded automatically into the bot's system prompt. In text channels, configure a workflow with a lookup action so the bot fetches the relevant records when needed; you do not need a prompt section for those lookups. On phone calls, a prompt section is the way to have the caller's orders ready before they speak: see Phone calls.
When you need one
You don't need one if:
- The context is the same for every conversation — put it in the business description or bot rules
- The bot only occasionally needs the data — a custom action it can call is cheaper
You need one when:
- The bot should always know something specific to this customer or conversation (their plan, their cart, the page they came from)
- Waiting for the bot to decide to look something up is already too late
- You want to steer the whole reply, not answer one question
Writing it
Set it under Settings -> Automation -> System Prompt, in the last section, Custom instructions. Editing it needs permission to edit automations. Define build_prompt_sections(context):
def build_prompt_sections(context):
topic = context["conversation"]["metadata"].get("chat-widget:help_topic")
if not topic:
return None
return {
"title": "Selected help topic",
"content": (
f'The customer picked "{topic}" on the website before opening the '
"chat. Treat that as what they are here for."
),
}It is not a custom action: the model never calls it, so it has no name, no description and no arguments. There is exactly one per business; return several blocks from it when you need more than one section.
Return values:
| Return | Result |
|---|---|
| A string | Added as the section body |
{"title": ..., "content": ...} | One titled block |
| A list of blocks or strings | Several blocks, each under its own #### heading, inside the one section |
None or "" | Nothing added this turn |
Blocks with empty content are dropped. Everything lands under a single ### Custom instructions heading in the prompt.
Choosing the channels
Under the code editor, Runs on sets the channels the section runs on. With no channel selected it runs on every channel. Pick channels to save work you don't need: a section written for phone calls doesn't have to run before every email reply.
Through the API or MCP this is systemPromptSectionChannels on the system prompt, for example ["phone-call"]. An empty list means every channel. Changing it needs the same permission as the code.
Businesses that had a section before phone calls could run it were set to every channel except phone calls, so their calls didn't change. Add Phone Call to opt them in.
Phone calls
On an inbound phone call the section runs once, while the caller's phone is ringing, and its result stays in the prompt for the whole call. That makes it the place to look things up before the caller speaks, for example their orders by phone number:
import json
def build_prompt_sections(context):
if context["conversation"]["latestChannel"] != "phone-call":
return None
phone = (context.get("customer") or {}).get("phone")
if not phone:
return None
orders = find_orders_by_phone(phone) # your own helper, e.g. in a Python module
if not orders:
return None
return {
"title": "Caller's orders",
"content": "The orders on the caller's phone number:\n" + json.dumps(orders),
}A call waits at most 3 seconds for it. If it is slower, fails or returns nothing, the call starts without the section and the bot can still look things up with its tools. Keep the lookups fast, and the result short: everything it returns is read by the phone bot on every turn.
A business with a fixed system prompt for its calls still gets the section, appended at the end. Outbound campaign calls don't run it: they use the campaign's own instructions.
What it receives
The same context and helpers as any other Python here — see Python Context for the full reference. Two things are worth calling out:
context["conversation"]["metadata"]holds conversation metadata, including anything the chat widget attached through custom JavaScript or custom data. Read it from there rather than callingget_conversation_metadata()— no HTTP round-trip.context["customer"]andcontext["conversation"]["messages"]let you branch on who is asking and what has been said so far.
It fails open, on purpose
This runs inside every reply, so it is never allowed to block one. If your code raises, returns something unusable, or takes too long (about 5 seconds on a text reply, 3 seconds on a phone call), the section is dropped and the bot answers without it. Nothing is retried and the customer sees no error.
Two consequences:
- Keep it fast. Prefer
contextover network calls. A slow lookup eats the same budget the bot's own reply needs. - All your blocks share one failure domain. If you return three blocks and the code raises while building the third, none of them make it. Guard anything risky inside the function so the rest survives.
Repeated failures raise an internal alert, and each build shows up in the response debug data with its size and duration, so you can see whether the section was present for a given reply.