Custom system prompt sections

Give the AI agent per-conversation context before it replies, such as a customer's plan or cart.

A custom system prompt section is Python that runs before the AI agent replies and adds what it returns to the system prompt. It gives the AI agent facts about this customer before it decides anything. For example, on a phone call it can look up the caller's orders by phone number while the phone is ringing, so the AI agent knows them before the caller speaks.

Use it for context that changes per conversation: the customer's plan or loyalty tier, their cart, or the help topic they picked on your website. Most of what the AI agent knows is either static, like the business description, tone and bot rules, or fetched on demand when it calls a custom action. A prompt section covers the third case. There is one per business, and it runs on the channels you choose.

Orders and subscriptions are not loaded into the system prompt automatically. In text channels, a workflow with a lookup action is usually the better fit: the AI agent fetches the record when it needs it. On phone calls, use a prompt section so the orders are ready before the caller speaks. 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 AI agent only occasionally needs the data. A custom action it can call is cheaper.
  • The data decides which instructions to follow. A condition provider selects a workflow variant.

You need one when:

  • The AI agent should always know something specific to this customer or conversation (their plan, their cart, the page they came from).
  • Waiting for the AI agent to decide to look something up is already too late.
  • You want to steer the whole reply, not answer one question.

Write one

Set it under Settings -> Automation -> System Prompt, in the last section, Custom instructions. Editing it needs permission to edit automations (the automations:write scope). 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.

Through Copilot or MCP, set the code with update_system_prompt. Pass null or an empty string to remove it.

Return values

ReturnResult
A stringAdded as the section body
{"title": ..., "content": ...}One titled block
A list of blocks or stringsSeveral 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.

Choose the channels

Under the code editor, Runs on sets the channels the section runs on. With no channel selected it runs on every channel. Select only the channels that need it: 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.

If a section doesn't run on phone calls, check that Phone Call is selected under Runs on.

What it receives

The same context and helpers as any other Python in Octocom. See Python context for the full reference. Two fields matter most here:

  • context["conversation"]["metadata"] holds conversation metadata, including anything the chat widget attached through custom JavaScript or custom data. Read it from there rather than calling get_conversation_metadata(): there is no extra round-trip.
  • context["customer"] and context["conversation"]["messages"] let you branch on who is asking and what has been said so far.

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 AI agent can still look things up with its tools. Keep the lookups fast and the result short: the voice AI reads everything it returns 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.

Failure behavior and limits

The section 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 AI agent answers without it. Nothing is retried and the customer sees no error.

Two consequences:

  • Keep it fast. Prefer context over network calls. A slow lookup delays the reply.
  • All your blocks succeed or fail together. If you return three blocks and the code raises while building the third, none of them make it. Catch errors around anything risky inside the function so the rest survives.

To check whether the section was present for a given reply, open that reply's debug data: each build is recorded there with its size and duration.

On this page