Jido.AI.PromptBuilder (Jido AI v2.1.0)

View Source

Builds enriched user prompts with XML-tagged context sections.

Context engineering best practice: place retrieved context (memory, facts, errors) in the user message rather than the system prompt. This keeps the system prompt stable across turns, maximizing KV-cache hit rates with providers that support prompt caching (e.g., Anthropic's prompt caching).

Each context section is wrapped in an XML tag and placed before the user message so the LLM treats it as retrieved context, not user input.

Usage

alias Jido.AI.PromptBuilder

prompt = PromptBuilder.build("What meetings do I have today?", [
  {:memory_context, memory_block},
  {:known_facts, facts_string},
  {:previous_errors, error_string}
])

Sections with nil or empty string values are automatically skipped.

Custom Tags

You can use any atom as a tag name:

PromptBuilder.build("Hello", [
  {:retrieved_documents, docs_text},
  {:user_preferences, prefs_text}
])

This produces:

<retrieved_documents>
...docs...
</retrieved_documents>

<user_preferences>
...prefs...
</user_preferences>

Hello

Summary

Functions

Builds an enriched prompt by prepending XML-tagged context sections to the message.

Wraps content in an XML tag.

Functions

build(message, sections)

@spec build(term(), [{atom(), String.t() | nil}]) :: term()

Builds an enriched prompt by prepending XML-tagged context sections to the message.

Parameters

  • message — the user's message (string). Non-string messages are returned as-is.
  • sections — a list of {tag_name, content} tuples. nil or empty content is skipped.

Returns

The enriched prompt string, or the original message if no sections have content.

wrap_xml(tag, content)

@spec wrap_xml(atom(), String.t()) :: String.t()

Wraps content in an XML tag.

Examples

iex> Jido.AI.PromptBuilder.wrap_xml(:memory_context, "some memories")
"<memory_context>\nsome memories\n</memory_context>"