DEV Community

Cover image for What if AI agents could compose themselves like words form sentences?
Patrick Roebuck
Patrick Roebuck

Posted on

What if AI agents could compose themselves like words form sentences?

Most AI frameworks treat agents as black boxes. You define them once, hard-code their behaviors, and pray they work together. When requirements change, you rewrite everything.

We took a different approach.

What if agent orchestration followed the same rules as language? Words combine into sentences through grammar. Agents combine into solutions through composition patterns. Learn the grammar, and you can express any idea.

The Language Metaphor

Think about how language works:

Language Agent Orchestration
Words Individual agents (test_analyzer, security_auditor, code_reviewer)
Grammar Rules Composition patterns (sequential, parallel, debate, teaching)
Sentences Complete solutions (release preparation, test coverage boost)

A child who knows 500 words and basic grammar can express millions of ideas. An orchestration system with 20 agent templates and 10 composition patterns can solve thousands of unique tasks.

That's the power of composability.

From Static Workflows to Dynamic Teams

The Old Way (Static)

# Fixed workflow - hard-coded agents, fixed order
workflow = Workflow([
    SecurityAuditor(),
    CodeReviewer(),
    TestValidator()
])
workflow.run()  # Same agents, same order, every time
Enter fullscreen mode Exit fullscreen mode

Problems:

  • Want to add documentation? Rewrite the workflow
  • Different project needs security focus? New workflow
  • Task simpler than expected? Still runs full pipeline

The New Way (Dynamic)

# Dynamic composition - agents spawn based on requirements
result = orchestrate("prepare for release")

# System analyzes task, spawns appropriate team:
# → Security Auditor (PREMIUM tier - critical for release)
# → Test Coverage Analyzer (CAPABLE tier)
# → Documentation Checker (CHEAP tier - fast validation)
# → Code Quality Reviewer (CAPABLE tier)
#
# Executes in PARALLEL for speed
# Aggregates with weighted scoring
Enter fullscreen mode Exit fullscreen mode

The system:

  1. Analyzes what "prepare for release" requires
  2. Selects appropriate agents from a template library
  3. Chooses the optimal composition pattern
  4. Spawns agents with task-specific instructions
  5. Executes and aggregates results
  6. Learns from the outcome for next time

The 10 Grammar Rules

We've implemented 10 composition patterns—the "grammar" of agent collaboration:

Basic Patterns (Rules 1-6)

Rule 1: Sequential (A → B → C)
  └─ Tasks done in order, each depends on previous
  └─ Example: analyze_coverage → generate_tests → validate_quality

Rule 2: Parallel (A || B || C)
  └─ Independent tasks run simultaneously
  └─ Example: [security || performance || docs || tests] → aggregate

Rule 3: Debate (A ⇄ B ⇄ C → Synthesis)
  └─ Multiple perspectives, synthesize consensus
  └─ Example: architect_scale ⇄ architect_cost → synthesizer

Rule 4: Teaching (Junior → Expert Validation)
  └─ Cost-effective generation with quality assurance
  └─ Example: junior_writer(CHEAP) → expert_review(CAPABLE)

Rule 5: Refinement (Draft → Review → Polish)
  └─ Iterative improvement ladder
  └─ Example: drafter → reviewer → polisher

Rule 6: Adaptive (Classifier → Specialist)
  └─ Route based on complexity
  └─ Example: classifier → route(simple|complex) → specialist
Enter fullscreen mode Exit fullscreen mode

Advanced Patterns (Rules 7-10)

These are the new patterns we just released:

Rule 7: Conditional (IF condition THEN A ELSE B)
  └─ Branching based on runtime conditions
  └─ Example: IF confidence < 0.8 THEN expert_review ELSE auto_approve

Rule 8: Multi-Conditional (SWITCH/CASE)
  └─ Multiple condition branches
  └─ Example: CASE severity: critical→emergency | high→urgent | *→normal

Rule 9: Nested (Workflows within workflows)
  └─ Hierarchical composition with depth limits
  └─ Example: release_prep contains [security_deep_dive, test_coverage_boost]

Rule 10: Learning (Patterns that improve from experience)
  └─ Track success rates, recommend optimal patterns
  └─ Example: "For security tasks, parallel pattern has 92% success rate"
Enter fullscreen mode Exit fullscreen mode

Dynamic Agent Creation in Action

Here's what happens when you ask the system to "boost test coverage":

# You write this:
result = orchestrate("boost test coverage for auth module")

# The system does this:
#
# 1. TASK ANALYSIS
#    └─ Intent: improve_tests
#    └─ Scope: auth module
#    └─ Complexity: moderate
#
# 2. AGENT SELECTION (from template library)
#    └─ coverage_analyzer (CAPABLE) - identify gaps
#    └─ test_generator (CAPABLE) - create tests
#    └─ quality_validator (CAPABLE) - verify quality
#
# 3. PATTERN SELECTION
#    └─ Sequential: analyze → generate → validate
#    └─ Why: Each step depends on previous
#
# 4. AGENT SPAWNING (dynamic customization)
#    coverage_analyzer_a7f2:
#      role: "Coverage Expert"
#      instructions: "Focus on auth module, identify untested paths"
#      tier: CAPABLE
#      tools: [coverage_analyzer, ast_parser]
#      quality_gates: {min_coverage: 80}
#
# 5. EXECUTION
#    coverage_analyzer → test_generator → quality_validator
#
# 6. LEARNING
#    └─ Record: sequential pattern, 3 agents, success=True
#    └─ Update: success_rate for "test_boost" pattern
#    └─ Store: for future reuse
Enter fullscreen mode Exit fullscreen mode

The Agent Template System

Agents are spawned from reusable templates:

@dataclass
class AgentTemplate:
    """Reusable agent archetype."""
    id: str                        # "test_coverage_analyzer"
    role: str                      # "Test Coverage Expert"
    capabilities: list[str]        # ["analyze_gaps", "suggest_tests"]
    tier_preference: str           # "CAPABLE"
    tools: list[str]               # ["coverage_analyzer", "ast_parser"]
    default_instructions: str      # Base prompt
    quality_gates: dict[str, Any]  # {"min_coverage": 80}
Enter fullscreen mode Exit fullscreen mode

When a template spawns an agent, it's customized for the specific task:

# Template defines archetype
template = TEMPLATES["security_auditor"]

# Factory spawns customized instance
agent = factory.spawn(template, requirements={
    "focus": "authentication",
    "severity_threshold": "high",
    "compliance": ["OWASP", "GDPR"]
})

# Resulting agent has task-specific instructions:
# "Audit authentication code for vulnerabilities.
#  Flag HIGH severity issues and above.
#  Check compliance with OWASP Top 10 and GDPR requirements."
Enter fullscreen mode Exit fullscreen mode

20 templates + dynamic customization = unlimited agents

Why This Matters

1. Reduced Boilerplate

Define templates once, reuse everywhere. No more copy-pasting agent configurations.

2. Right-Sizing

System automatically selects appropriate tier (CHEAP/CAPABLE/PREMIUM) based on task complexity.

3. Automatic Learning

Every execution teaches the system. Success rates tracked. Optimal patterns discovered.

4. Composability

Combine patterns freely. Nest workflows. Add conditions. Build complex behaviors from simple rules.

5. Transparency

Clear execution plans. Visible decisions. Understand why agents were chosen.

Getting Started

# Install
pip install empathy-framework

# Dynamic orchestration
empathy orchestrate "prepare for release"

# Specific task with context
empathy orchestrate "boost test coverage" --context '{"module": "auth"}'

# See what patterns have been learned
empathy patterns report
Enter fullscreen mode Exit fullscreen mode

What's Next

This is Part 1 of a 4-part series on the Grammar of AI Collaboration:

  1. The Grammar of AI Collaboration ← You are here
  2. Dynamic Agent Creation Deep Dive — Inside the Agent Factory
  3. Building Agent Teams — Composition patterns in action
  4. Advanced Grammar — Conditional logic, nesting, and learning

The code for this article is available in Empathy Framework v4.4.0 on GitHub.

*Patrick Roebuck is the creator of the Empathy Framework.

Top comments (0)