Skip to main content

Prompt blocks

Prompt blocks are reusable instruction templates managed by Editor. An agent's instructions can combine inline text, embedded prompt blocks, and references to independently versioned prompt blocks.

See Prompt blocks for the Studio workflow and common uses.

Block types
Direct link to Block types

TypeDescription
textFree-form text stored only in the agent version
prompt_blockA prompt block embedded in the agent version
prompt_block_refA reference to an independently stored and versioned prompt block

Referenced blocks resolve at runtime. A missing or unpublished reference is omitted from the final instructions. Resolved nonempty blocks are joined with two newlines.

The following example attaches a stored block and inline text to an agent:

src/scripts/attach-prompt.ts
import { mastra } from '../mastra'

const editor = mastra.getEditor()!

await editor.agent.update({
id: 'support-agent',
instructions: [
{ type: 'prompt_block_ref', id: 'brand-voice' },
{ type: 'text', content: 'Answer only questions about Acme products.' },
],
})

Template values
Direct link to Template values

Templates resolve values from the request context at runtime.

SyntaxRequest contextOutput
{{userName}}{ userName: 'Maya' }Maya
{{user.name}}{ user: { name: 'Maya' } }Maya
{{task || 'request'}}{}request
{{missingValue}}{}{{missingValue}}

Variable names must begin with a letter or underscore. Fallbacks must be single-quoted or double-quoted strings. Unresolved placeholders without a fallback remain unchanged. Objects and arrays are serialized as JSON. Other values are converted to strings.

Pass values through request context. Editor doesn't read a separate agent variables field.

Display conditions
Direct link to Display conditions

A prompt block can include a display condition that controls whether it's included in the final instructions. Each condition has three parts:

  • Key: The request-context field to check, such as user.role or account.plan.
  • Operator: The comparison to make, such as equals, contains, or exists.
  • Value: The value to compare against. The exists and not_exists operators don't need one.

For example, the condition user.role equals admin includes the block only when request context contains { user: { role: 'admin' } }.

OperatorExampleThe block is included when
equals / not_equalsuser.role equals adminThe field strictly equals, or doesn't equal, the value
contains / not_containsuser.tags contains betaA string contains the value or an array contains the item
greater_than / less_thanorder.total greater than 100The numeric field is above or below the value
greater_than_or_equal / less_than_or_equalaccount.seats greater than or equal to 10The numeric field is at or beyond the value
in / not_inuser.region in ['US', 'CA']The field is, or isn't, in the supplied array
exists / not_existsaccount.plan existsThe field has, or doesn't have, a non-null value

Groups combine conditions with AND or OR. For example, this group includes a block for admins on a paid plan:

const rules = {
operator: 'AND',
conditions: [
{
field: 'user.role',
operator: 'equals',
value: 'admin',
},
{
field: 'account.plan',
operator: 'in',
value: ['pro', 'enterprise'],
},
],
}

Dot paths are supported. An empty group evaluates to true, and an unknown operator evaluates to false. The storage type supports up to three nested group levels.

Blocks without conditions are always included.

Programmatic API
Direct link to Programmatic API

Access prompt blocks through mastra.getEditor().prompt. See the prompt namespace for complete method signatures.

Create a prompt block:

src/scripts/seed-prompts.ts
import { mastra } from '../mastra'

const editor = mastra.getEditor()!

await editor.prompt.create({
id: 'brand-voice',
name: 'Brand voice',
description: 'Acme tone and style guidelines',
content: 'Write in a friendly, concise tone. Address the user as {{userName || "there"}}.',
})

Update an existing block:

src/scripts/update-prompt.ts
await editor.prompt.update({
id: 'brand-voice',
content: 'Write in a friendly, concise tone. Greet the user by name when available.',
})

update() creates a new draft when the content changes. Use list() to paginate through stored blocks, getById() to fetch one block, and preview(blocks, context) to resolve templates and conditions with draft references.

REST API
Direct link to REST API

The default Mastra server prefix is /api. A custom server prefix changes the paths below.

MethodPathDescription
GET/api/stored/prompt-blocksList stored prompt blocks
POST/api/stored/prompt-blocksCreate a stored prompt block
GET/api/stored/prompt-blocks/:storedPromptBlockIdGet a stored prompt block
PATCH/api/stored/prompt-blocks/:storedPromptBlockIdUpdate a stored prompt block
DELETE/api/stored/prompt-blocks/:storedPromptBlockIdDelete a stored prompt block

Version resolution
Direct link to Version resolution

Runtime references resolve the active published block. Editor previews resolve the latest draft. See Editor versioning for the shared draft, publish, and restore lifecycle.