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:

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:


### (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


Exercises

  1. Basic (Difficulty: ⭐): Create a simple translation template with source_lang and target_lang parameters.
  2. Intermediate (Difficulty: ⭐⭐): Create a code review template with conditional rendering based on severity.
  3. Advanced (Difficulty: ⭐⭐⭐): Design a complete development template set (review → fix → test) and test its effectiveness.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏