Prompts

How to create reusable LLM prompt templates for your agents.

What is a Prompt?

A prompt is a template that defines how to interact with Language Models. It supports:

  • Template text with variable placeholders
  • Variable interpolation using {{variable}} syntax
  • Reusability across multiple workflows

Important: The SDK defines the prompt template. The platform handles LLM invocation.

Creating a Prompt

from ainalyn import PromptBuilder
 
prompt = (
    PromptBuilder("data-analyzer")
    .description("Analyzes data and provides insights")
    .template("""
Please analyze the following data:
 
{{data}}
 
Focus on: {{focus_areas}}
 
Provide detailed insights and recommendations.
""")
    .variables(["data", "focus_areas"])
    .build()
)

Variable Syntax

Use {{variable_name}} to mark placeholders in your template.

template = """
Hello {{name}},
 
Your order {{order_id}} has been {{status}}.
 
Thank you!
"""
 
prompt = (
    PromptBuilder("order-notification")
    .template(template)
    .variables(["name", "order_id", "status"])
    .build()
)

At runtime:

  • Platform replaces {{name}} with actual value
  • Variables come from node inputs or previous outputs

Using Prompts in Workflows

Step 1: Define the prompt

analyzer = (
    PromptBuilder("analyzer")
    .description("Analyzes text content")
    .template("Analyze this: {{content}}")
    .variables(["content"])
    .build()
)

Step 2: Add prompt to agent

agent = (
    AgentBuilder("text-agent")
    .version("1.0.0")
    .add_prompt(analyzer)  # Register prompt
    .add_workflow(workflow)
    .build()
)

Step 3: Reference in node

from ainalyn import NodeBuilder
 
node = (
    NodeBuilder("analyze")
    .description("Analyze content")
    .uses_prompt("analyzer")  # References the prompt
    .inputs("content")
    .outputs("analysis")
    .build()
)

Common Prompt Patterns

Text Analysis

analyzer = (
    PromptBuilder("text-analyzer")
    .description("Analyzes text for sentiment and key points")
    .template("""
Analyze the following text:
 
{{text}}
 
Provide:
1. Sentiment (positive/negative/neutral)
2. Key points (3-5 bullet points)
3. Summary (1-2 sentences)
""")
    .variables(["text"])
    .build()
)

Data Summarization

summarizer = (
    PromptBuilder("data-summarizer")
    .description("Summarizes structured data")
    .template("""
Summarize this data:
 
{{data}}
 
Format: {{format}}
 
Include statistics and key findings.
""")
    .variables(["data", "format"])
    .build()
)

Content Generation

generator = (
    PromptBuilder("content-generator")
    .description("Generates content based on topic")
    .template("""
Write a {{content_type}} about {{topic}}.
 
Tone: {{tone}}
Length: {{length}} words
 
Requirements:
{{requirements}}
""")
    .variables(["content_type", "topic", "tone", "length", "requirements"])
    .build()
)

Translation

translator = (
    PromptBuilder("translator")
    .description("Translates text between languages")
    .template("""
Translate the following text from {{source_lang}} to {{target_lang}}:
 
{{text}}
 
Maintain the original tone and style.
""")
    .variables(["source_lang", "target_lang", "text"])
    .build()
)

Q&A Assistant

qa = (
    PromptBuilder("qa-assistant")
    .description("Answers questions based on context")
    .template("""
Context:
{{context}}
 
Question: {{question}}
 
Provide a clear, concise answer based only on the context provided.
""")
    .variables(["context", "question"])
    .build()
)

Multiline Templates

Use Python triple quotes for readable templates:

# Readable multiline template
template = """
Analyze the following report:
 
Title: {{title}}
Date: {{date}}
Author: {{author}}
 
Content:
{{content}}
 
Provide a summary and key takeaways.
"""
 
# Avoid hard to read single line
# template = "Analyze: {{title}} Date: {{date}}..."

Variables Best Practices

1. List all variables used

# All variables declared
PromptBuilder("analyzer")
    .template("Analyze {{data}} focusing on {{aspect}}")
    .variables(["data", "aspect"])
 
# Missing variable declaration - avoid this
PromptBuilder("analyzer")
    .template("Analyze {{data}} focusing on {{aspect}}")
    .variables(["data"])  # Missing "aspect"

2. Use descriptive variable names

# Clear variable names
.variables(["user_email", "order_id", "delivery_date"])
 
# Unclear - avoid
.variables(["e", "id", "d"])

3. Keep variable count manageable

# Focused prompt with 2-4 variables
.variables(["topic", "tone", "length"])
 
# Too many variables - consider splitting
# .variables(["v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8"])

Prompt Naming

Valid names:

"text-analyzer"       # Lowercase with hyphens
"data-summarizer"     # Descriptive
"qa-assistant-v2"     # With version

Invalid names:

"TextAnalyzer"        # Must be lowercase
"text_analyzer"       # No underscores
"my prompt"           # No spaces

Prompt Design Tips

1. Be specific

# Specific instructions
template = """
Analyze the sentiment of this customer review:
 
{{review}}
 
Classify as: positive, negative, or neutral
Provide reasoning in 1-2 sentences
"""
 
# Vague - avoid
# template = "What do you think about {{review}}?"

2. Structure the output

# Structured output request
template = """
Summarize: {{article}}
 
Format:
- Main idea: [1 sentence]
- Key points: [3-5 bullets]
- Conclusion: [1 sentence]
"""
 
# Unstructured - may give inconsistent results
# template = "Summarize {{article}}"

3. Provide context

# With context
template = """
You are a technical documentation expert.
 
Review this code documentation:
 
{{documentation}}
 
Suggest improvements for clarity and completeness.
"""
 
# No context - less effective
# template = "Review {{documentation}}"

Complete Example

from ainalyn import (
    AgentBuilder,
    WorkflowBuilder,
    NodeBuilder,
    PromptBuilder,
    NodeType
)
 
# Define prompt
review_prompt = (
    PromptBuilder("code-reviewer")
    .description("Reviews code for quality and best practices")
    .template("""
Review the following code:
 
Language: {{language}}
Code:
{{code}}
 
Provide:
1. Overall quality assessment
2. Potential issues or bugs
3. Suggestions for improvement
4. Best practice recommendations
""")
    .variables(["language", "code"])
    .build()
)
 
# Use in workflow
workflow = (
    WorkflowBuilder("code-review")
    .entry_node("review")
    .add_node(
        NodeBuilder("review")
        .description("Review submitted code")
        .uses_prompt("code-reviewer")
        .inputs("language", "code")
        .outputs("review_result")
        .build()
    )
    .build()
)
 
# Create agent
agent = (
    AgentBuilder("code-review-agent")
    .version("1.0.0")
    .description("Automated code review assistant")
    .add_prompt(review_prompt)
    .add_workflow(workflow)
    .build()
)

Prompt vs Module vs Tool

Use Prompt when:

  • You need LLM-based reasoning
  • Task involves text understanding or generation
  • Output is natural language

Use Module when:

  • You need deterministic logic
  • Task is computation or data processing
  • See Modules Guide

Use Tool when:

  • You need external services
  • Task involves APIs or databases
  • See Tools Guide

See Also