> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gnani.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Dynamic Variables & Prompt Validation

Dynamic variables inject user-specific context into the agent's initial message and system prompt at runtime. Prompt validation checks that Jinja templates are syntactically correct before you save or test an agent.

### Path

**Manage Agent → Customize tab → Dynamic Variables**

### What Dynamic Variables Do

Dynamic Variables allow your AI agent to personalize its **Initial Message** and \*\*System Prompt \*\*using **runtime data** fetched from an external API.

They replace placeholder text like:

```text theme={null}
{{user_name}}
{{plan_type}}
{{ticket_status}}
```

…with real values like:

* Rahul
* premium
* pending

All **before** the request is sent to the LLM.

### Why Use Dynamic Variables?

* Personalizes the agent’s tone, message, and instructions
* Ensures responses are relevant to the **current user context**
* Allows the same agent to adapt dynamically for different users and use cases
* Integrates with your **backend systems** for context-aware conversations

### Where They’re Used

Dynamic Variables can be embedded in:

* **Initial Message** – The very first thing the agent says
* **System Prompt** – The underlying instruction set that guides the LLM

### Placeholder Syntax

* Always wrap variable names in **double curly brackets**
* Use lowercase with underscores for clarity

**Examples:**

```markdown expandable theme={null}
{{user_name}}
{{account_balance}}
{{ticket_status}}
```

### How It Works

1. **Agent Invoked**

   User starts a chat or API-triggered interaction.

2. **Variables Detected**

   The system scans templates for `{{variable_name}}` placeholders.

3. **API Call Made**

   If variables exist, the agent calls your configured **Dynamic Message API**.

4. **Values Returned**

   API responds with a JSON object, e.g.:

```json theme={null}
{
	"user_name": "Rahul",
	"plan_type": "premium",
	"ticket_status": "pending"
}
```

5. **Template Compiled**

Placeholders are replaced with actual values.

6. **LLM Receives Context**

The prompt sent to the LLM is fully contextualized.

### Example

System Prompt Template:

```
You are a support agent assisting {{user_name}}, a {{plan_type}} customer.
Their last ticket was marked {{ticket_status}}.
```

API Response:

```
{
	"user_name": "Rahul",
	"plan_type": "premium",
	"ticket_status": "pending"
}
```

Final Prompt Sent to LLM:

```
You are a support agent assisting Rahul, a premium customer.
Their last ticket was marked pending.
```

### Best Practices

| Area | Recommendation |
| - | - |
| Variable Naming | Use clear, lowercase, underscore-separated names |
| API Reliability | Ensure low-latency, high-uptime APIs |
| Defaults | Define fallback values for missing variables |
| Message Integrity | Keep variables simple — avoid complex objects |
| Testing | Test with real data before going live |

## Related feature: Prompt validation

Your system prompt is treated as a Jinja2 template. That supports conditionals and loops, but requires validation before you save or test an agent.

### What validation checks

* Syntax correctness for variables, `if` blocks, and `for` loops
* Undeclared variables referenced in the prompt
* Proper block closures (every opening tag has a matching closing tag)

### Invalid syntax behavior

If syntax errors are found:

* Save and Test are disabled
* A clear error message is shown

Common errors:

```text theme={null}
Missing braces: {{ user_name }
Unclosed block: {% if condition %} ... (missing {% endif %})
```

### Validation rules

| Condition | Save / Test | Message |
| - | - | - |
| Valid syntax | Yes | – |
| Syntax error | No | "Invalid Jinja syntax." |
| Missing variables at runtime | Yes (save works) | "Call triggered failed." |

### Agent behavior

* Existing agents keep working until the system prompt is edited and saved again.
* New and updated agents must follow Jinja formatting rules.

### Valid example

```text theme={null}
Hello {{ customer_name }}, your booking is on {{ booking_date }}.
```

### Invalid example

```text theme={null}
Hello {{ customer_name }
```

Unclosed curly braces disable Save and Test until corrected.

### Tip

If variables appear in the system prompt, declare them in **pre-call variables** or return them from the **Dynamic Message API**. Otherwise calls may fail at runtime.

### Summary

| Field | Value |
| - | - |
| Feature | Runtime variable injection |
| Syntax | `variable_name` with double curly braces |
| Input source | External API (JSON) |
| Supports | Initial message, system prompt, ending message |
| Resolved at | Runtime, before the LLM call |
| Failure handling | Graceful fallback |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.