System templates allow you to create dynamic, context-aware prompts for your AI agents using Jinja2 templating. This guide covers everything you need to know about creating and customizing system templates.
Overview
System templates in Julep are Jinja2 templates that define the initial system prompt for your AI agents. They allow you to:- Create dynamic prompts that adapt based on context
- Include user information, session data, and other variables
- Implement conditional logic for different scenarios
- Maintain consistency across agent interactions
- Inject custom metadata at the message level for dynamic behavior
Template Hierarchy
Julep uses a three-level hierarchy for system templates:- Agent Default Template: Defined when creating an agent
- Session Template: Can override the agent’s default template for specific sessions
- Chat Metadata: Can inject dynamic variables at the individual message level
The session template takes precedence over the agent’s default template. Chat metadata is available in both cases.
Default System Template
When you create an agent without specifying a custom template, Julep uses this default template:Template Variables
The following variables are available in your system templates:Core Variables
Dynamic Variables
Creating Custom Templates
Basic Example
Here’s a simple custom template for a customer service agent:Advanced Example with Conditional Logic
Using Chat Metadata
Themetadata field in chat requests allows you to pass dynamic variables that can be used in your system templates. This enables message-level customization without modifying the agent or session.
Example: Dynamic Instructions
Session vs Agent Templates
You can override an agent’s default template at the session level:Jinja2 Features
System templates support the full range of Jinja2 features:Conditionals
Loops
Filters
Comments
Best Practices
1. Keep Templates Focused
1. Keep Templates Focused
Avoid overly complex templates. If your template is becoming too large, consider breaking the logic into different agents or sessions.
2. Use Meaningful Variable Names
2. Use Meaningful Variable Names
When using metadata, choose clear, descriptive keys:
- ✅
metadata.customer_tier - ❌
metadata.ct
3. Provide Fallbacks
3. Provide Fallbacks
Always handle cases where variables might be missing:
4. Test Template Rendering
4. Test Template Rendering
Test your templates with various metadata combinations to ensure they render correctly in all scenarios.
Common Patterns
Multi-language Support
Role-based Access
Context-aware Behavior
Debugging Templates
To debug template rendering issues:- Check Variable Availability: Ensure all referenced variables exist
- Validate Jinja2 Syntax: Use a Jinja2 linter or validator
- Test Incrementally: Add template features one at a time
- Use the Render Endpoint: Test template rendering without making chat requests
Related Topics
- Agents - Learn more about agent configuration
- Sessions - Understand session management
- Tasks - Create complex workflows with templates
- Tool Integration - Add tools referenced in templates
System templates are rendered server-side before being sent to the language model. This ensures security and consistency across all API clients.