Skip to main content

Patterns & Blueprints

Patterns and Blueprints are the composition layer. Patterns are reusable context templates backed by cognitive frameworks. Blueprints are multi-component architectures for production applications.

Pattern

A Pattern is a reusable, parameterized context template. All 87 cognitive templates (RootCauseAnalyzer, CodeReviewer, etc.) extend Pattern. You can also build custom patterns.

Import

from mycontext import Pattern
# or
from mycontext.structure import Pattern

Using a Cognitive Pattern

The most common usage — pick one of the 85 built-in patterns:

from mycontext.templates.free.reasoning import RootCauseAnalyzer

rca = RootCauseAnalyzer()
ctx = rca.build_context(
problem="API response times tripled after last deployment",
depth="comprehensive",
)
result = ctx.execute(provider="openai")

Each pattern's build_context() has typed, documented inputs specific to its methodology. See Cognitive Patterns for all 87 patterns.

Building a Custom Pattern

from mycontext import Pattern
from mycontext.foundation import Guidance, Constraints

competitor_analyst = Pattern(
name="competitor_analyst",
description="Structured competitive analysis for a product or company",
guidance=Guidance(
role="Senior competitive intelligence analyst",
rules=[
"Base every claim on the provided data — flag gaps explicitly",
"Quantify advantages and disadvantages where possible",
"Always include actionable recommendations",
],
style="direct, evidence-based",
expertise=["competitive analysis", "market research", "product strategy"],
),
directive_template=(
"Analyze {competitor} as a competitor to {our_product}.\n\n"
"Context: {context}\n\n"
"Focus on: {focus_areas}"
),
constraints=Constraints(
must_include=["strengths", "weaknesses", "strategic threats", "recommendations"],
format_rules=["Use a SWOT-style structure", "Bullet points throughout"],
),
input_schema={
"competitor": str,
"our_product": str,
"context": str,
"focus_areas": str,
},
tags=["strategy", "competitive-intelligence"],
version="1.0.0",
)

ctx = competitor_analyst.build_context(
competitor="Competitor X",
our_product="Our SaaS Platform",
context="We are losing deals to Competitor X in the mid-market segment.",
focus_areas="pricing model, enterprise integrations, customer support quality",
)

result = ctx.execute(provider="openai")

Pattern Fields

FieldTypeRequiredDescription
namestrYesUnique identifier
descriptionstr | NoneNoWhat this pattern does
guidanceGuidance | NoneNoDefault system guidance
directive_templatestr | NoneNoTemplate string with {variable} placeholders
constraintsConstraints | NoneNoOutput guardrails
input_schemadict[str, type]NoRequired inputs and their types
output_schemadict[str, type]NoExpected output structure
tagslist[str]NoCategorization labels
versionstrNoSemantic version, default "1.0.0"
metadatadictNoArbitrary metadata

build_context(**inputs)Context

Builds a fully assembled Context from the pattern and provided inputs.

ctx = pattern.build_context(
problem="Server crashes under load",
depth="comprehensive",
)
# Returns a Context with guidance, directive (built from template), constraints, and data

The built Context automatically carries pattern metadata:

ctx.metadata["pattern"]         # "root_cause_analyzer"
ctx.metadata["pattern_version"] # "1.0.0"

execute(provider, mode, **inputs) → response

Build and execute in a single call. Accepts all build_context inputs plus any provider kwargs (model, temperature, etc.):

result = pattern.execute(
provider="openai",
mode="full", # "full" (default) or "generic"
problem="Server crashes under load",
depth="comprehensive",
model="gpt-4o",
temperature=0.2,
)

mode="generic" uses the pattern's GENERIC_PROMPT — a zero-cost pre-authored prompt that skips context assembly.

Generic Prompts — Zero-Cost Execution

Every built-in pattern carries a GENERIC_PROMPT — a hand-crafted ~600–1200 char prompt that distills the pattern's cognitive methodology into a self-contained instruction.

from mycontext.templates.free.reasoning import RootCauseAnalyzer

rca = RootCauseAnalyzer()

# Zero-cost: no LLM call, no context assembly
prompt = rca.generic_prompt(problem="Server crashes during peak hours")
print(len(prompt)) # ~950 chars
print(prompt) # "You are performing a systematic root cause analysis..."

Two execution modes compared:

ModeMethodLLM CallsOutput
Genericpattern.generic_prompt(**inputs)0 (compilation)Pre-authored prompt string
Generic executepattern.execute(mode="generic", ...)1 (just the LLM call)Response from generic prompt
Fullpattern.build_context(**inputs).execute()1Response from rich structured context

Saving and Loading Patterns

from pathlib import Path

# Save to YAML
pattern.save(Path("patterns/competitor_analyst.yaml"))

# Load from YAML
loaded = Pattern.load("competitor_analyst", library_path=Path("patterns/"))

Blueprint

Blueprint is for production applications that need more than one template — orchestrating multiple components (guidance, dynamic knowledge, reasoning steps) with token budget management.

Import

from mycontext import Blueprint
# or
from mycontext.structure import Blueprint

Basic Usage

from mycontext import Blueprint
from mycontext.foundation import Guidance, Constraints

blueprint = Blueprint(
name="research_assistant",
description="Deep research assistant with topic-specific knowledge injection",
guidance=Guidance(
role="Expert research analyst",
rules=[
"Cite sources whenever possible",
"Distinguish between confirmed facts and reasoned inferences",
"Structure findings from most to least important",
],
style="precise, analytical",
),
directive_template="Research and explain the following: {topic}\n\nScope: {scope}",
constraints=Constraints(
must_include=["key findings", "knowledge gaps", "further reading"],
format_rules=["Use H2 headers for major sections", "Include a TL;DR at the top"],
),
token_budget=4000,
optimization="balanced",
)

ctx = blueprint.build(
topic="Quantum computing advances in error correction",
scope="Focus on developments from 2023–2025 relevant to near-term practical applications",
)

result = ctx.execute(provider="openai")

Blueprint Fields

FieldTypeDefaultDescription
namestrrequiredBlueprint identifier
descriptionstr | NoneNoneWhat this blueprint creates
guidanceGuidance | NoneNonePrimary system guidance
directive_templatestr | NoneNoneTemplate with {variable} placeholders
constraintsConstraints | NoneNoneOutput guardrails
componentslist[]Additional components (documents, sessions, etc.)
token_budgetint4000Max token target (100–1,000,000)
optimizationstr"balanced"Strategy: "speed", "quality", "cost", or "balanced"
priority_orderlist[str]see belowAssembly priority order

Default priority order: guidance → directive → constraints → knowledge → memory

Optimization Strategies

The optimize() method produces a modified copy of the blueprint tuned for a strategy:

# Quality: +30% token budget, prioritizes guidance + knowledge
quality_bp = blueprint.optimize("quality")

# Speed: -30% token budget, directive-first
speed_bp = blueprint.optimize("speed")

# Cost: -50% token budget, essentials only
cost_bp = blueprint.optimize("cost")

# Balanced (default): unchanged
balanced_bp = blueprint.optimize("balanced")
StrategyToken BudgetPriority Order
"quality"budget × 1.3guidance → knowledge → directive → constraints
"speed"budget × 0.7directive → guidance → constraints → knowledge
"cost"budget × 0.5directive → constraints → guidance
"balanced"unchangeddefault order

Token Estimation

estimated = blueprint.estimate_tokens()
print(f"Estimated tokens: {estimated}")
# Compare against your budget
print(f"Budget: {blueprint.token_budget}")

Components

Components are objects with a .render() or .to_string() method — their output is assembled into the knowledge field of the built context:

class DocumentChunk:
def __init__(self, text):
self.text = text
def render(self):
return self.text

blueprint = Blueprint(
name="doc_qa",
guidance=Guidance(role="Technical documentation expert"),
directive_template="Answer the question: {question}",
components=[
DocumentChunk("API Reference: POST /v1/contexts — creates a new context..."),
DocumentChunk("Rate limits: 1000 requests/minute on the Pro plan..."),
],
token_budget=3000,
)

ctx = blueprint.build(question="What are the rate limits for the API?")
# ctx.knowledge → combined text from both DocumentChunks

When to Use Blueprint vs. Pattern

UseTool
Single-step reasoning with a specific methodologyPattern / cognitive template
Multi-component production context with token budgetBlueprint
Parameterized template with fixed structurePattern with directive_template
Dynamic knowledge injection + guidance + constraintsBlueprint with components
Multi-step chains of reasoningChain patterns or build_workflow_chain()

Next: Cognitive Patterns →