Learn how to use Jinja2 templates to create dynamic system prompts for your AI agents
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.
When you create an agent without specifying a custom template, Julep uses this default template:
Copy
{%- if agent.name -%}You are {{agent.name}}.{{" "}}{%- endif -%}{%- if agent.about -%}About you: {{agent.about}}.{{" "}}{%- endif -%}{%- if user.name -%}You are talking to {{user.name}}.{{" "}}{%- endif -%}{%- if user.about -%}About the user: {{user.about}}.{{" "}}{%- endif -%}{{agent.instructions}}{%- if tools -%}You have access to these tools:{%- for tool in tools -%}{%- if tool.type == "function" -%}- {{tool.function.name}}{%- if tool.function.description -%}: {{tool.function.description}}{%- endif -%}{%- else -%}- {{ 0/0 }} {# Error: Other tool types aren't supported yet #}{%- endif -%}{%- endfor -%}{%- endif -%}{%- if docs -%}Relevant documents:{%- for doc in docs -%}{{doc.title}}{%- if doc.content -%}:<document>{{doc.content}}</document>{%- endif -%}{%- endfor -%}{%- endif -%}
Here’s a simple custom template for a customer service agent:
Copy
agent = client.agents.create( name="Customer Support Agent", model="gpt-4", about="A helpful customer service representative", instructions=[ "Be polite and professional", "Always offer to help further" ], default_system_template="""You are {{agent.name}}, a customer service representative.{%- if metadata.priority == "high" -%}⚠️ HIGH PRIORITY CUSTOMER - Provide expedited service{%- endif -%}{%- if metadata.customer_tier == "premium" -%}Premium customer - Offer enhanced support options{%- endif -%}Your guidelines:{{agent.instructions}}{%- if user.name -%}Customer name: {{user.name}}{%- endif -%}""")
template = """{%- if agent.name -%}You are {{agent.name}}.{%- endif -%}{# Mood-based personality adjustment #}{%- if metadata.mood == "friendly" -%}Be warm, conversational, and use a friendly tone.{%- elif metadata.mood == "professional" -%}Maintain a formal, business-appropriate tone.{%- elif metadata.mood == "playful" -%}Feel free to be creative and add appropriate humor.{%- else -%}Respond in a balanced, helpful manner.{%- endif -%}{# Language preferences #}{%- if metadata.language -%}Respond in {{metadata.language}}.{%- endif -%}{# Expertise areas #}{%- if metadata.expertise -%}You are an expert in:{%- for skill in metadata.expertise -%}- {{skill}}{%- endfor -%}{%- endif -%}{# Include instructions #}{{agent.instructions}}{# Tool availability #}{%- if tools -%}Available tools:{%- for tool in tools -%}- {{tool.function.name}}: {{tool.function.description}}{%- endfor -%}{%- endif -%}"""# Using the template with metadataresponse = client.sessions.chat( session_id=session.id, messages=[ {"role": "user", "content": "Help me with Python"} ], metadata={ "mood": "friendly", "language": "English", "expertise": ["Python", "Machine Learning", "Data Science"] })
The metadata 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.
# Create an agent with a metadata-aware templateagent = client.agents.create( name="Adaptive Assistant", default_system_template="""You are {{agent.name}}.{%- if metadata.instructions -%}Special instructions for this conversation:{{metadata.instructions}}{%- endif -%}{%- if metadata.constraints -%}Important constraints:{%- for constraint in metadata.constraints -%}- {{constraint}}{%- endfor -%}{%- endif -%}""")# Use metadata to modify behavior per messageresponse = client.sessions.chat( session_id=session.id, messages=[{"role": "user", "content": "Write a story"}], metadata={ "instructions": "Write in the style of Edgar Allan Poe", "constraints": [ "Keep it under 200 words", "Include a raven", "End with a twist" ] })
You can override an agent’s default template at the session level:
Copy
# Agent with default templateagent = client.agents.create( name="Multi-purpose Assistant", default_system_template="You are a helpful assistant.")# Session with custom templatesession = client.sessions.create( agent_id=agent.id, system_template="""You are a technical writing assistant.Focus on clarity, accuracy, and proper documentation structure.{%- if metadata.doc_type -%}You are writing a {{metadata.doc_type}}.{%- endif -%}""")
Use session templates when you need different behavior for the same agent in different contexts (e.g., customer service vs internal support).
{%- if user.subscription_level == "premium" -%}You have access to advanced features.{%- elif user.subscription_level == "basic" -%}Some features may be limited.{%- else -%}Welcome! Consider upgrading for more features.{%- endif -%}
{%- if metadata.language == "es" -%}Eres un asistente útil. Responde en español.{%- elif metadata.language == "fr" -%}Vous êtes un assistant utile. Répondez en français.{%- else -%}You are a helpful assistant. Respond in English.{%- endif -%}
{%- if metadata.user_role == "admin" -%}You have full access to all operations and sensitive information.{%- elif metadata.user_role == "user" -%}Provide general assistance. Do not share sensitive information.{%- endif -%}