Pi Agent: Prompt Templates
Last updated: 2026-08-31
Good prompts are like good recipes — follow them, and every dish comes out right.
1. Why Prompt Templates
Problems with writing prompts directly:
- Repeating similar prompts for similar tasks
- Inconsistent prompt quality
- Hard to share best practices across teams
Prompt templates solve these:
| Feature | Description |
|---|---|
| Reusable | Define once, call many times |
| Parameterizable | Variable substitution for different scenarios |
| Shareable | Team template library |
| Testable | Templates can be tested and optimized independently |
2. Built-in Templates
| Template | Purpose |
|---|---|
| code_review | Code review |
| bug_fix | Bug fixing |
| test_gen | Test generation |
| doc_gen | Documentation generation |
| translate | Translation |
| summarize | Summarization |
| refactor | Refactoring |
PYTHON
from pi_agent import Agent
agent = Agent(name="reviewer")
result = agent.run(template="code_review", code="def add(a,b): return a+b")
3. Custom Templates
(1) YAML Template File
Create ~/.pi-agent/templates/code_explain.yaml:
YAML
name: code_explain
description: "Explain code functionality and principles"
system: |
You are a code explanation expert. Explain code in simple language,
including: 1) Function overview 2) Key logic 3) Potential issues
template: |
Please explain the following {{ language }} code:
```{{ language }}
{{ code }}
Requirements:
- Explain functionality in plain language
- Mark key logic points
- Point out potential issues (if any) variables:
- name: language type: string required: true description: "Programming language name"
- name: code type: string required: true description: "Code to explain"
### (2) Python Template Definition
```python
from pi_agent import PromptTemplate
explain_template = PromptTemplate(
name="code_explain",
description="Explain code functionality and principles",
system="You are a code explanation expert",
template="Please explain the following {language} code:\n{code}",
variables=["language", "code"]
)
agent = Agent(name="explainer")
agent.add_template(explain_template)
result = agent.run(
template="code_explain",
language="python",
code="def fib(n): return n if n < 2 else fib(n-1) + fib(n-2)"
)
4. Template Variables & Conditionals
(1) Basic Variables
YAML
template: |
Translate the following text from {{ source_lang }} to {{ target_lang }}:
{{ text }}
(2) Conditional Rendering
YAML
template: |
Please review the following code:
{{ code }}
{% if focus_area %}
Focus on {{ focus_area }} aspects.
{% endif %}
{% if severity == "strict" %}
Please review strictly, list all issues.
{% else %}
Only list critical issues.
{% endif %}
(3) Loops
YAML
template: |
Check if the following files conform to coding standards:
{% for file in files %}
- {{ file }}
{% endfor %}
5. Template Composition
Example 1: Multi-Template Workflow (Difficulty: ⭐⭐)
PYTHON
from pi_agent import Agent, PromptTemplate
review_template = PromptTemplate(
name="review",
template="Review code: {code}\nFocus: {focus}"
)
fix_template = PromptTemplate(
name="fix",
template="Fix code based on review: {review_result}\nOriginal: {code}"
)
agent = Agent(name="pipeline")
code = "def add(a, b): return a + b"
review_result = agent.run(template="review", code=code, focus="security")
fix_result = agent.run(template="fix", review_result=review_result, code=code)
6. Template Testing & Optimization
(1) A/B Testing
PYTHON
from pi_agent import Agent
template_a = "Review this code: {code}"
template_b = "As a senior engineer, strictly review this code for security, performance, and readability: {code}"
agent = Agent(name="ab_test")
result_a = agent.run(template=template_a, code=sample_code)
result_b = agent.run(template=template_b, code=sample_code)
FAQ
Q Template variables vs Python f-strings?
A Template variables use
{{ var }} (Jinja2-style), rendered at runtime with conditionals and loops. f-strings are Python syntax and can't be stored in config files.Q How to manage many templates?
A Organize by directory:
templates/coding/, templates/writing/, templates/review/. Pi Agent recursively scans all subdirectories.Q Best way to share templates with team?
A Put template directory in a Git repo, each person clones to
~/.pi-agent/templates/. Or publish as a Pi Agent package.Summary
- Prompt templates solve reusability, consistency, and sharing
- YAML files and Python definitions both supported
- Variables support conditional rendering and loops
- Multiple templates can compose workflows
- A/B testing and scoring continuously improve template quality
Exercises
- Basic (Difficulty: ⭐): Create a simple translation template with source_lang and target_lang parameters.
- Intermediate (Difficulty: ⭐⭐): Create a code review template with conditional rendering based on severity.
- Advanced (Difficulty: ⭐⭐⭐): Design a complete development template set (review → fix → test) and test its effectiveness.