Context templates
Context templates allow you to customize how context is formatted and returned when calling thread.get_user_context(). Templates are reusable configurations that you set once for your project and can use across all your threads. They let you specify what information goes into your context block and how much of it to include, while Zep handles the automatic relevance detection and retrieval.
Why use context templates
Context templates let you define a custom context format once and reuse it across all threads and users in your project. They provide a balance between simplicity and control:
- More control than: Default Context Block (customize format/structure)
- Less control than: Advanced construction with graph search (cannot customize search query)
- Best for: When you need consistent custom formatting but want automatic relevance detection
See Choosing a retrieval method for a comparison of all three methods.
Define a template
Templates use variables to specify what data to include. Available variables:
%{edges}- Graph edges (facts/relationships)%{entities}- Graph entities (nodes)%{episodes}- Episode data%{user_summary}- User summary information
Variables (except user_summary) accept optional parameters:
limit=N- Limit number of results (max 1000)types=[type1,type2]- Filter by entity or edge typesinclude_attributes=true/false- Include/exclude attributes
Example template definition:
Create a template
Create a new template with a unique template ID and template content. Templates are validated when createdโZep checks for valid variable names, proper bracket balancing, valid parameter syntax, and limit values within range.
Use a template
Pass the template_id parameter when retrieving context:
Resulting context block
When you use the template above, Zep returns a formatted context block like this:
Update a template
Update an existing templateโs content:
Read a template
Retrieve a specific template by its ID or list all templates:
Delete a template
Delete a template when itโs no longer needed: