Skip to main content

Overview

Dynamic variables allow prompt templates to be personalized at runtime using data from multiple sources. Variables use Mustache-style {{variable_name}} syntax and are resolved from a priority-ordered chain of sources before any prompt is rendered.

Variable Syntax

  • Variable names are case-sensitive
  • Unresolved variables are replaced with an empty string
  • Unresolved variables are logged as warnings for debugging
  • Nested/escaped braces are handled gracefully

Sources and Precedence

Variables are merged from highest to lowest priority (higher priority overwrites lower for the same key):

Passing Variables

Include variables in the chat request body:
  • First request (no conversationId): variables stored as initial_variables and variables
  • Subsequent requests: new variables merged into variables (existing keys overwritten)

Sensitive Variables

Sensitive variables are server-set and never returned in any client-facing API response. They are stored encrypted in conversation_variables.sensitive_variables using AES-256-GCM.

Setting via POST Endpoint

Call from your backend before or during the conversation:

Setting via Signed Context Token

Generate a signed JWT from your backend:
Response: { "token": "eyJ..." }
  • Frontend receives opaque token, passes as context_token in conversation create or chat request
  • Bot service decodes JWT server-side, extracts variables
  • Keys listed in sensitive_keys are stored encrypted; others stored as regular variables
  • Token has expiry — validated before use
Use case: Host backend pre-generates context for an embedded widget session. The frontend never sees the sensitive values, only the opaque token.

Resolution Flow

Prompt Rendering Engine

Shared utility in packages/shared/src/prompt-renderer.ts:
  • Pure function, no side effects
  • Used by bot service for all prompt types
  • Handles nested/escaped braces gracefully
  • Returns list of unresolved variables for logging

Per-Message Variable Overrides

Variables can be updated mid-conversation by including variables in any chat request:
New variables are merged into the existing conversation_variables.variables record, enabling dynamic context updates as the conversation progresses.

Security Considerations

Follow these security rules for all variable handling.
  • Never expose sensitive variable keys in client-side code or HTML. Use signed context tokens.
  • Prompt injection: Malicious variable values could attempt to override LLM behavior. Variables should be clearly delimited (e.g., wrapped in quotes or XML tags) when inserted into LLM prompts.
  • Context token expiry: Always set a reasonable expires_in (e.g., 3600 seconds). Expired tokens are rejected.
  • Sensitive variables: Encrypted at rest, never returned in GET responses, only accessible server-side during prompt rendering.
  • Query param variables: Only use for non-sensitive data — query params are logged and visible in browser history.