Quick Answer:
Functions are reusable, multi-step operations that wrap complex
business logic into single commands (like generate_weekly_report()
instead of explaining the same 7-step process repeatedly),
reducing 150+ token instructions to 5-10 token function calls
while ensuring consistency.
Detailed Explanation:
Functions solve the problem of repetitive, complex instructions.
If you find yourself giving AI the same multi-step process over
and over—analyzing competitors, generating reports, creating
content templates—you need a function. Functions encapsulate
your business logic into single, reusable commands.
The Repetition Problem:
How many times have you given AI nearly identical instructions?
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EXAMPLE: Repetitive Instructions Without Functions
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Week 1:
“I need you to analyze this competitor. First, research
their pricing structure. Then compare their features with
ours. Create a comparison matrix. Identify where we win
and where they win. Focus especially on enterprise
features since that’s our target. Format as a table with
clear headers.”
Token cost: 156 tokens
Week 2:
“Can you analyze another competitor? Look at their
pricing and features, compare with our offering, make a
matrix showing strengths/weaknesses for each, emphasize
enterprise capabilities, use table format.”
Token cost: 142 tokens
Week 3:
“Competitor analysis time again. Pricing and feature
comparison needed, matrix format, highlight enterprise
aspects, show where each of us is stronger…”
Token cost: 135 tokens
Same process, slightly different wording, burning tokens
and risking inconsistency.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Function Solution:
Define once:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EXAMPLE: Function Definition – Define Once
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
def analyze_competitor(competitor_name,
focus_area=”enterprise”):
“””
Performs comprehensive competitor analysis with
comparison matrix
“””
#H->AI::Directive: (Research {competitor_name}’s
pricing and features)
#H->AI::Directive: (Compare with our
PRODUCT_FEATURES)
#H->AI::Structure: (Create comparison matrix showing
wins/losses)
#H->AI::Focus: (Emphasize {focus_area} capabilities)
#H->AI::Context: (Target market is TARGET_AUDIENCE)
return structured_analysis
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Use forever:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EXAMPLE: Function Usage – Use Forever
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
analyze_competitor(“TechCorp”)
6 tokens
analyze_competitor(“CloudSoft”, “SMB”)
8 tokens
analyze_competitor(“DataFlow”)
6 tokens
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Token savings per use: 90-95%
Functions vs Simple Aliases:
This distinction is critical—functions aren’t just shorthand:
Simple Alias (Just Renames):
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EXAMPLE: Simple Alias – Not a Real Function
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
comp_analysis = “competitor analysis”
Just replaces text
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
This is just a variable. No logic, no process, no
intelligence.
Real Function (Executes Process):
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EXAMPLE: Real Function – Executes Complete Workflow
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
def analyze_competitor(competitor_name,
aspects=[“pricing”, “features”]):
“””
Performs actual multi-step analysis workflow
“””
#H->AI::Directive: (Research {competitor_name}’s
{aspects})
#H->AI::Structure: (Create comparison matrix)
#H->AI::Context: (Use our PRODUCT_FEATURES for
baseline)
#H->AI::OnError: (If data unavailable, note gaps
clearly)
return structured_analysis
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
This executes a complete workflow with error handling and
context.
Simple Functions for Common Tasks:
Start with functions for your most repetitive activities:
Weekly Report Generation:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EXAMPLE: Weekly Report Function
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
def generate_weekly_report(week_ending,
highlight_threshold=10):
“””
Automated weekly metrics report with trend analysis
“””
#H->AI::Directive: (Generate report for week ending
{week_ending})
#H->AI::Structure: (Bullet points, most important
changes first)
#H->AI::Context: (Metrics: user growth, revenue,
churn, support tickets)
#H->AI::Focus: (Highlight changes over
{highlight_threshold}%)
#H->AI::Structure: (Add summary paragraph with key
insights)
return {"report": report_content,
"subject": email_subject}
Usage:
report = generate_weekly_report(“2024-01-28”)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Job Posting Creation:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EXAMPLE: Job Posting Function
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
def create_job_posting(role, level, requirements):
“””
Generates complete job posting with company standards
“””
#H->AI::Context: (Use COMPANY_CULTURE and
BENEFITS_PACKAGE)
#H->AI::Structure: (Include: role summary,
responsibilities, requirements, benefits)
#H->AI::Constraint: (Match BRAND_VOICE, avoid
discrimination risk language)
return formatted_posting
Usage:
engineer_role = create_job_posting(
“Software Engineer”,
“Senior”,
[“5+ years Python”, “AWS experience”]
)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Prospect Outreach:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EXAMPLE: Prospect Outreach Function
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
def prospect_outreach(company, contact_role, pain_point):
“””
Creates personalized cold outreach email
“””
#H->AI::Directive: (Draft outreach to {contact_role}
at {company})
#H->AI::Focus: (Address {pain_point} with our
PRODUCT_DESCRIPTION)
#H->AI::Constraint: (Keep under 150 words, no sales
jargon)
#H->AI::Context: (Reference relevant case study if
applicable)
return email_draft
Usage:
email = prospect_outreach(
“Acme Corp”,
“VP Engineering”,
“deployment bottlenecks”
)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Function Parameters for Flexibility:
Parameters make functions adaptable while maintaining
consistency:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EXAMPLE: Flexible Content Generation Function
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
def generate_content(
topic, # Required: what to write about
content_type=”blog”, # Optional: defaults to blog
length=”medium”, # Optional: short/medium/long
tone=”professional”, # Optional: voice adjustment
include_cta=True # Optional: call-to-action
):
“””
Flexible content generation for any marketing need
“””
# Map length to approximate word counts
word_counts = {
“short”: 300,
“medium”: 800,
“long”: 1500
}
#H->AI::Directive: (Create {content_type} about
{topic})
#H->AI::Structure: (Length: {length}, approximately
{word_counts[length]} words)
#H->AI::Context: (Maintain {tone} tone throughout)
#H->AI::Context: (Target audience is
TARGET_AUDIENCE)
if include_cta:
#H->AI::Structure: (End with compelling
call-to-action)
return content
Same function, infinitely flexible:
blog = generate_content(
“AI trends”,
content_type=”blog”,
tone=”conversational”
)
email = generate_content(
“AI trends”,
content_type=”email”,
length=”short”
)
whitepaper = generate_content(
“AI trends”,
content_type=”whitepaper”,
tone=”academic”,
length=”long”
)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Composed Functions for Complex Workflows:
Functions become powerful when combined:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EXAMPLE: Composed Functions for Product Launch
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
def launch_product(product_details):
“””
Complete product launch workflow orchestrating
multiple functions
“””
# Each function handles its specialized task
messaging = create_messaging(product_details)
content = generate_launch_content(messaging)
campaign = setup_campaign(content)
tracking = configure_analytics(campaign)
return LaunchPackage(
messaging, content, campaign, tracking
)
def create_messaging(details):
#H->AI::Directive: (Create positioning and key
messages for {details.name})
#H->AI::Context: (Use PRODUCT_POSITIONING and
TARGET_AUDIENCE)
return messaging_guide
def generate_launch_content(messaging):
#H->AI::Directive: (Create blog, email, social posts
from messaging guide)
#H->AI::Structure: (Full content suite for
multi-channel launch)
return content_suite
def setup_campaign(content):
#H->AI::Directive: (Structure 8-week multi-channel
campaign using content)
#H->AI::Context: (Budget is MARKETING_BUDGET,
channels are MARKETING_CHANNELS)
return campaign_plan
One command orchestrates entire product launch:
launch = launch_product(product_v2_details)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
When to Create a Function:
The “Rule of Three”:
If you’ve given the same instructions 3+ times with minimal
variation, create a function.
Recognition Patterns:
* "I need to do that report again…"
* "Same analysis, different competitor…"
* "Let's create another version of…"
These phrases signal function opportunities.
Example:
Before Functions:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EXAMPLE: Before Creating a Function
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Every Monday morning, 150+ token explanation:
“Generate our weekly status report. Start by gathering
key metrics from the dashboard – user growth, revenue,
churn rate, and support tickets. Calculate
week-over-week changes for each. Format as bullet points
with the most significant changes at the top. Add a
summary paragraph highlighting key insights. Make sure
to flag anything that changed by more than 10%.”
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
After Creating Function:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
EXAMPLE: After Creating a Function
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
def generate_weekly_report():
#H->AI::Directive: (Generate weekly status report)
#H->AI::Context: (Metrics: user_growth, revenue,
churn, support_tickets)
#H->AI::Structure: (Bullet points, biggest changes
first + summary paragraph)
#H->AI::Focus: (Flag changes > 10%)
Every Monday morning, 6 tokens:
generate_weekly_report()
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Over 52 weeks:
* Without function: 7,800 tokens
* With function: ~350 tokens (first definition +
51 calls)
* Savings: 7,450 tokens (95%)
Key Points:
* Functions encapsulate complex, multi-step processes
into single, reusable commands
* Not aliases—they execute actual workflows with logic,
context, and error handling
* Parameters provide flexibility while maintaining
consistency (same function, different contexts)
* Create functions after repeating similar instructions
3+ times (Rule of Three)
* Composed functions orchestrate complex workflows by
combining simpler functions
* Typical savings: 90-95% token reduction per use after
initial definition
* Transform 150-token instructions into 5-10 token
function calls
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Functions are reusable, multi-step operations that wrap complex business logic into single commands (like generate_weekly_report() instead of explaining the same 7-step process repeatedly), reducing 150+ token instructions to 5-10 token function calls while ensuring consistency.
Detailed Explanation:
Functions solve the problem of repetitive, complex instructions. If you find yourself giving AI the same multi-step process over and over—analyzing competitors, generating reports, creating content templates—you need a function. Functions encapsulate your business logic into single, reusable commands.
The Repetition Problem:
How many times have you given AI nearly identical instructions?
Week 1:
"I need you to analyze this competitor. First, research their pricing structure.
Then compare their features with ours. Create a comparison matrix. Identify
where we win and where they win. Focus especially on enterprise features
since that's our target. Format as a table with clear headers."
Token cost: 156 tokens
Week 2:
"Can you analyze another competitor? Look at their pricing and features,
compare with our offering, make a matrix showing strengths/weaknesses
for each, emphasize enterprise capabilities, use table format."
Token cost: 142 tokens
Week 3:
"Competitor analysis time again. Pricing and feature comparison needed,
matrix format, highlight enterprise aspects, show where each of us is stronger..."
Token cost: 135 tokens
Same process, slightly different wording, burning tokens and risking inconsistency.
Function Solution:
Define once:
def analyze_competitor(competitor_name, focus_area="enterprise"): """
Performs comprehensive competitor analysis with comparison matrix
""" #H->AI::Directive: (Research {competitor_name}'s pricing and features) #H->AI::Directive: (Compare with our PRODUCT_FEATURES) #H->AI::Structure: (Create comparison matrix showing wins/losses) #H->AI::Focus: (Emphasize {focus_area} capabilities) #H->AI::Context: (Target market is TARGET_AUDIENCE) return structured_analysis
Use forever:
analyze_competitor("TechCorp") # 6 tokensanalyze_competitor("CloudSoft", "SMB") # 8 tokens analyze_competitor("DataFlow") # 6 tokens
Token savings per use: 90-95%
Functions vs Simple Aliases:
This distinction is critical—functions aren’t just shorthand:
Simple Alias (Just Renames):
comp_analysis = "competitor analysis" # Just replaces text
This is just a variable. No logic, no process, no intelligence.
Real Function (Executes Process):
def analyze_competitor(competitor_name, aspects=["pricing", "features"]): """
Performs actual multi-step analysis workflow
""" #H->AI::Directive: (Research {competitor_name}'s {aspects}) #H->AI::Structure: (Create comparison matrix) #H->AI::Context: (Use our PRODUCT_FEATURES for baseline) #H->AI::OnError: (If data unavailable, note gaps clearly) return structured_analysis
This executes a complete workflow with error handling and context.
Simple Functions for Common Tasks:
Start with functions for your most repetitive activities:
Weekly Report Generation:
def generate_weekly_report(week_ending, highlight_threshold=10): """
Automated weekly metrics report with trend analysis
""" #H->AI::Directive: (Generate report for week ending {week_ending}) #H->AI::Structure: (Bullet points, most important changes first) #H->AI::Context: (Metrics: user growth, revenue, churn, support tickets) #H->AI::Focus: (Highlight changes over {highlight_threshold}%) #H->AI::Structure: (Add summary paragraph with key insights) return {"report": report_content, "subject": email_subject}# Usage:report = generate_weekly_report("2024-01-28")
Job Posting Creation:
def create_job_posting(role, level, requirements): """
Generates complete job posting with company standards
""" #H->AI::Context: (Use COMPANY_CULTURE and BENEFITS_PACKAGE) #H->AI::Structure: (Include: role summary, responsibilities, requirements, benefits) #H->AI::Constraint: (Match BRAND_VOICE, avoid discrimination risk language) return formatted_posting
# Usage:engineer_role = create_job_posting("Software Engineer", "Senior", ["5+ years Python", "AWS experience"])
Prospect Outreach:
def prospect_outreach(company, contact_role, pain_point): """
Creates personalized cold outreach email
""" #H->AI::Directive: (Draft outreach to {contact_role} at {company}) #H->AI::Focus: (Address {pain_point} with our PRODUCT_DESCRIPTION) #H->AI::Constraint: (Keep under 150 words, no sales jargon) #H->AI::Context: (Reference relevant case study if applicable) return email_draft
# Usage:email = prospect_outreach("Acme Corp", "VP Engineering", "deployment bottlenecks")
Function Parameters for Flexibility:
Parameters make functions adaptable while maintaining consistency:
def generate_content( topic, # Required: what to write about content_type="blog", # Optional: defaults to blog length="medium", # Optional: short/medium/long tone="professional", # Optional: voice adjustment include_cta=True # Optional: call-to-action): """
Flexible content generation for any marketing need
""" # Map length to approximate word counts word_counts = {"short": 300, "medium": 800, "long": 1500} #H->AI::Directive: (Create {content_type} about {topic}) #H->AI::Structure: (Length: {length}, approximately {word_counts[length]} words) #H->AI::Context: (Maintain {tone} tone throughout) #H->AI::Context: (Target audience is TARGET_AUDIENCE) if include_cta: #H->AI::Structure: (End with compelling call-to-action) return content
# Same function, infinitely flexible:blog = generate_content("AI trends", content_type="blog", tone="conversational")email = generate_content("AI trends", content_type="email", length="short")whitepaper = generate_content("AI trends", content_type="whitepaper", tone="academic", length="long")
Composed Functions for Complex Workflows:
Functions become powerful when combined:
def launch_product(product_details): """
Complete product launch workflow orchestrating multiple functions
""" # Each function handles its specialized task messaging = create_messaging(product_details) content = generate_launch_content(messaging) campaign = setup_campaign(content) tracking = configure_analytics(campaign) return LaunchPackage(messaging, content, campaign, tracking)def create_messaging(details): #H->AI::Directive: (Create positioning and key messages for {details.name}) #H->AI::Context: (Use PRODUCT_POSITIONING and TARGET_AUDIENCE) return messaging_guide
def generate_launch_content(messaging): #H->AI::Directive: (Create blog, email, social posts from messaging guide) #H->AI::Structure: (Full content suite for multi-channel launch) return content_suite
def setup_campaign(content): #H->AI::Directive: (Structure 8-week multi-channel campaign using content) #H->AI::Context: (Budget is MARKETING_BUDGET, channels are MARKETING_CHANNELS) return campaign_plan
# One command orchestrates entire product launch:launch = launch_product(product_v2_details)
When to Create a Function:
The “Rule of Three”:
If you’ve given the same instructions 3+ times with minimal variation, create a function.
Recognition Patterns:
- “I need to do that report again…”
- “Same analysis, different competitor…”
- “Let’s create another version of…”
These phrases signal function opportunities.
Example:
Before Functions:
Every Monday morning, 150+ token explanation:
"Generate our weekly status report. Start by gathering key metrics from
the dashboard - user growth, revenue, churn rate, and support tickets.
Calculate week-over-week changes for each. Format as bullet points with
the most significant changes at the top. Add a summary paragraph highlighting
key insights. Make sure to flag anything that changed by more than 10%."
After Creating Function:
def generate_weekly_report(): #H->AI::Directive: (Generate weekly status report) #H->AI::Context: (Metrics: user_growth, revenue, churn, support_tickets) #H->AI::Structure: (Bullet points, biggest changes first + summary paragraph) #H->AI::Focus: (Flag changes > 10%)# Every Monday morning, 6 tokens:generate_weekly_report()
Over 52 weeks:
- Without function: 7,800 tokens
- With function: ~350 tokens (first definition + 51 calls)
- Savings: 7,450 tokens (95%)
Key Points:
- Functions encapsulate complex, multi-step processes into single, reusable commands
- Not aliases—they execute actual workflows with logic, context, and error handling
- Parameters provide flexibility while maintaining consistency (same function, different contexts)
- Create functions after repeating similar instructions 3+ times (Rule of Three)
- Composed functions orchestrate complex workflows by combining simpler functions
- Typical savings: 90-95% token reduction per use after initial definition
- Transform 150-token instructions into 5-10 token function calls
Related Topics:
- What are Variables in CRAFT and why do I need them?
- What are Objects in CRAFT?
- How much efficiency will I gain with CRAFT?