Agentic Design Pattern: Task Allocation Scheduling

Written by

in


    """
Pattern 119: Task Allocation & Scheduling

Description:
    Assigns tasks to agents based on capabilities, load, and constraints
    using auction-based or optimal assignment algorithms.

Components:
    - Task queue
    - Agent capabilities
    - Allocation algorithm
    - Load balancing

Use Cases:
    - Multi-agent systems
    - Distributed computing
    - Resource optimization

LangChain Implementation:
    Uses LLM-based task analysis and agent matching with LangGraph for orchestration.
"""

import os
from typing import List, Dict, Any
from dataclasses import dataclass
from datetime import datetime
from dotenv import load_dotenv

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI

load_dotenv()


@dataclass
class Task:
    """Represents a task to be allocated."""
    id: str
    description: str
    required_skills: List[str]
    priority: int
    estimated_duration: int
    deadline: str = None


@dataclass
class Agent:
    """Represents an agent with capabilities."""
    id: str
    name: str
    skills: List[str]
    current_load: int = 0
    max_capacity: int = 100
    performance_history: List[float] = None
    
    def __post_init__(self):
        if self.performance_history is None:
            self.performance_history = []


class TaskAllocationScheduler:
    """Schedules and allocates tasks to agents optimally."""
    
    def __init__(self, model_name: str = "gpt-4"):
        self.llm = ChatOpenAI(model=model_name, temperature=0.3)
        self.agents: List[Agent] = []
        self.task_queue: List[Task] = []
        self.assignments: Dict[str, str] = {}  # task_id -> agent_id
        
    def register_agent(self, agent: Agent):
        """Register an agent in the system."""
        self.agents.append(agent)
        print(f"✓ Registered agent: {agent.name} with skills {agent.skills}")
    
    def add_task(self, task: Task):
        """Add task to queue."""
        self.task_queue.append(task)
        print(f"✓ Added task: {task.description} (Priority: {task.priority})")
    
    def analyze_task_requirements(self, task: Task) -> Dict[str, Any]:
        """Analyze task requirements using LLM."""
        analysis_prompt = ChatPromptTemplate.from_messages([
            ("system", "Analyze this task and provide detailed requirements."),
            ("user", """Task: {description}
Required Skills: {skills}
Priority: {priority}

Provide:
1. Complexity level (1-10)
2. Required expertise level
3. Any special considerations""")
        ])
        
        chain = analysis_prompt | self.llm | StrOutputParser()
        analysis = chain.invoke({
            "description": task.description,
            "skills": ", ".join(task.required_skills),
            "priority": task.priority
        })
        
        return {"analysis": analysis}
    
    def calculate_agent_score(self, agent: Agent, task: Task) -> float:
        """Calculate how well an agent matches a task."""
        scoring_prompt = ChatPromptTemplate.from_messages([
            ("system", "Score how well this agent matches the task requirements (0-100)."),
            ("user", """Agent: {agent_name}
Agent Skills: {agent_skills}
Current Load: {current_load}/{max_capacity}
Performance History: {performance}

Task: {task_description}
Required Skills: {required_skills}
Priority: {priority}

Return only a numerical score (0-100):""")
        ])
        
        chain = scoring_prompt | self.llm | StrOutputParser()
        response = chain.invoke({
            "agent_name": agent.name,
            "agent_skills": ", ".join(agent.skills),
            "current_load": agent.current_load,
            "max_capacity": agent.max_capacity,
            "performance": sum(agent.performance_history) / len(agent.performance_history) if agent.performance_history else "No history",
            "task_description": task.description,
            "required_skills": ", ".join(task.required_skills),
            "priority": task.priority
        })
        
        try:
            score = float(response.strip().split()[0])
            
            # Adjust for current load
            load_factor = (agent.max_capacity - agent.current_load) / agent.max_capacity
            adjusted_score = score * load_factor
            
            return max(0.0, min(100.0, adjusted_score))
        except:
            return 0.0
    
    def allocate_tasks(self):
        """Allocate tasks to agents using optimal matching."""
        print("\n=== Task Allocation Process ===\n")
        
        # Sort tasks by priority
        sorted_tasks = sorted(self.task_queue, key=lambda t: t.priority, reverse=True)
        
        for task in sorted_tasks:
            print(f"Allocating: {task.description}")
            
            # Analyze task
            analysis = self.analyze_task_requirements(task)
            print(f"  Task Analysis: {analysis['analysis'][:100]}...")
            
            # Score all agents
            agent_scores = []
            for agent in self.agents:
                score = self.calculate_agent_score(agent, task)
                agent_scores.append((agent, score))
                print(f"  {agent.name}: {score:.1f}")
            
            # Select best agent
            if agent_scores:
                best_agent, best_score = max(agent_scores, key=lambda x: x[1])
                
                if best_score > 0:
                    self.assignments[task.id] = best_agent.id
                    best_agent.current_load += task.estimated_duration
                    print(f"  ✓ Assigned to: {best_agent.name} (Score: {best_score:.1f})")
                else:
                    print(f"  ✗ No suitable agent found")
            print()
        
        self.task_queue.clear()
    
    def get_schedule_summary(self) -> str:
        """Generate schedule summary."""
        summary_prompt = ChatPromptTemplate.from_messages([
            ("system", "Summarize this task allocation and schedule."),
            ("user", """Assignments:
{assignments}

Agent Loads:
{agent_loads}

Provide:
1. Overall load distribution
2. Potential bottlenecks
3. Optimization suggestions""")
        ])
        
        assignments_str = "\n".join([
            f"Task {task_id} -> Agent {agent_id}"
            for task_id, agent_id in self.assignments.items()
        ])
        
        agent_loads_str = "\n".join([
            f"{agent.name}: {agent.current_load}/{agent.max_capacity} ({agent.current_load/agent.max_capacity*100:.1f}%)"
            for agent in self.agents
        ])
        
        chain = summary_prompt | self.llm | StrOutputParser()
        return chain.invoke({
            "assignments": assignments_str,
            "agent_loads": agent_loads_str
        })
    
    def rebalance_load(self):
        """Rebalance load across agents if needed."""
        avg_load = sum(a.current_load for a in self.agents) / len(self.agents)
        overloaded = [a for a in self.agents if a.current_load > avg_load * 1.5]
        underloaded = [a for a in self.agents if a.current_load < avg_load * 0.5]
        
        if overloaded and underloaded:
            print("\n=== Load Rebalancing ===")
            print(f"Overloaded agents: {[a.name for a in overloaded]}")
            print(f"Underloaded agents: {[a.name for a in underloaded]}")
            print("Recommending task redistribution...")


def demonstrate_task_allocation():
    """Demonstrate task allocation and scheduling pattern."""
    print("=== Task Allocation & Scheduling Pattern ===\n")
    
    scheduler = TaskAllocationScheduler()
    
    # Register agents
    print("1. Registering Agents")
    print("-" * 50)
    
    scheduler.register_agent(Agent(
        id="agent1",
        name="Alice",
        skills=["python", "data_analysis", "machine_learning"],
        max_capacity=100,
        performance_history=[0.85, 0.90, 0.88]
    ))
    
    scheduler.register_agent(Agent(
        id="agent2",
        name="Bob",
        skills=["javascript", "frontend", "ui_design"],
        max_capacity=100,
        performance_history=[0.92, 0.89, 0.91]
    ))
    
    scheduler.register_agent(Agent(
        id="agent3",
        name="Charlie",
        skills=["devops", "cloud", "kubernetes"],
        max_capacity=100,
        performance_history=[0.88, 0.87, 0.90]
    ))
    
    scheduler.register_agent(Agent(
        id="agent4",
        name="Diana",
        skills=["python", "backend", "database"],
        max_capacity=100,
        performance_history=[0.90, 0.93, 0.91]
    ))
    
    print()
    
    # Add tasks
    print("2. Adding Tasks to Queue")
    print("-" * 50)
    
    scheduler.add_task(Task(
        id="task1",
        description="Build machine learning model for customer churn prediction",
        required_skills=["python", "machine_learning", "data_analysis"],
        priority=9,
        estimated_duration=40
    ))
    
    scheduler.add_task(Task(
        id="task2",
        description="Design and implement responsive dashboard UI",
        required_skills=["javascript", "frontend", "ui_design"],
        priority=8,
        estimated_duration=30
    ))
    
    scheduler.add_task(Task(
        id="task3",
        description="Set up CI/CD pipeline with Kubernetes deployment",
        required_skills=["devops", "cloud", "kubernetes"],
        priority=7,
        estimated_duration=35
    ))
    
    scheduler.add_task(Task(
        id="task4",
        description="Optimize database queries and add indexes",
        required_skills=["database", "backend", "python"],
        priority=8,
        estimated_duration=25
    ))
    
    scheduler.add_task(Task(
        id="task5",
        description="Implement user authentication system",
        required_skills=["backend", "python", "security"],
        priority=9,
        estimated_duration=45
    ))
    
    print()
    
    # Allocate tasks
    print("3. Allocating Tasks to Agents")
    print("-" * 50)
    scheduler.allocate_tasks()
    
    # Schedule summary
    print("4. Schedule Summary")
    print("-" * 50)
    summary = scheduler.get_schedule_summary()
    print(summary)
    print()
    
    # Check for rebalancing needs
    print("5. Load Balancing Check")
    print("-" * 50)
    scheduler.rebalance_load()
    
    print("\n=== Summary ===")
    print(f"Total assignments: {len(scheduler.assignments)}")
    print("Task allocation demonstrated with:")
    print("- Skill-based matching")
    print("- Load-aware assignment")
    print("- Priority-based scheduling")
    print("- Performance history consideration")
    print("- Load balancing recommendations")


if __name__ == "__main__":
    demonstrate_task_allocation()

    

Agentic Design Pattern: Task Allocation Scheduling

Pattern 119: Task Allocation Scheduling – Line-by-Line Explanation

Overview

This document provides a detailed line-by-line explanation of the Task Allocation Scheduling pattern implementation.


Pattern Description

Pattern 119: Task Allocation & Scheduling

Description: Assigns tasks to agents based on capabilities, load, and constraints using auction-based or optimal assignment algorithms.

Components: – Task queue – Agent capabilities – Allocation algorithm – Load balancing

Use Cases: – Multi-agent systems – Distributed computing – Resource optimization

LangChain Implementation: Uses LLM-based task analysis and agent matching with LangGraph for orchestration.


File Statistics

  • Total Lines: 342
  • Classes: 3
  • Functions: 1
  • Constants: 0

Module Documentation

"""
Pattern 119: Task Allocation & Scheduling

Description:
    Assigns tasks to agents based on capabilities, load, and constraints
    using auction-based or optimal assignment algorithms.

Components:
    - Task queue
    - Agent capabilities
    - Allocation algorithm
    - Load balancing

Use Cases:
    - Multi-agent systems
    - Distributed computing
    - Resource optimization

LangChain Implementation:
    Uses LLM-based task analysis and agent matching with LangGraph for orchestration.
"""

Purpose: This module implements the Task Allocation Scheduling pattern, providing a structured approach to agent design and implementation.


Import Statements

Standard Library Imports

import dataclasses.dataclass
import datetime.datetime
import langchain_core.output_parsers.StrOutputParser
import langchain_core.prompts.ChatPromptTemplate
import os
import typing.Any
import typing.Dict
import typing.List

Purpose: Core Python functionality: – dataclasses.dataclass: Simplified class definitions with @dataclass – datetime.datetime: Date and time handling – langchain_core.output_parsers.StrOutputParser: Regular expression operations – langchain_core.prompts.ChatPromptTemplate: Regular expression operations – os: Environment variable access and system operations – typing.Any: Type hints for better code documentation (List, Dict, Any, Optional) – typing.Dict: Type hints for better code documentation (List, Dict, Any, Optional) – typing.List: Type hints for better code documentation (List, Dict, Any, Optional)

LangChain Imports

from langchain_openai.ChatOpenAI

Purpose: LangChain framework components: – LLM Interface: Wrapper for OpenAI models – Chains: Composable building blocks for complex workflows

Other Imports

from dotenv.load_dotenv

Purpose: Additional dependencies for specialized functionality.


Classes

Class: Task

Lines 37-44

Purpose:

Represents a task to be allocated.

Class: Agent

Lines 48-59

Purpose:

Represents an agent with capabilities.

Methods:

__post_init__(self)

Lines 57-59

Purpose: Post Init operation.

Parameters:


Class: TaskAllocationScheduler

Lines 62-220

Purpose:

Schedules and allocates tasks to agents optimally.

Methods:

__init__(self, model_name)

Lines 65-69

Purpose: Initialize the class instance with required parameters.

Parameters: – model_name: Language model instance

register_agent(self, agent)

Lines 71-74

Purpose:

Register an agent in the system.

Parameters: – agent: Agent

add_task(self, task)

Lines 76-79

Purpose:

Add task to queue.

Parameters: – task: Task

analyze_task_requirements(self, task)

Lines 81-102

Purpose:

Analyze task requirements using LLM.

Parameters: – task: Task

Returns: <ast.Subscript object at 0x000001A06D765F50>

calculate_agent_score(self, agent, task)

Lines 104-141

Purpose:

Calculate how well an agent matches a task.

Parameters: – agent: Agent – task: Task

Returns: float

allocate_tasks(self)

Lines 143-176

Purpose:

Allocate tasks to agents using optimal matching.

Parameters:

get_schedule_summary(self)

Lines 178-208

Purpose:

Generate schedule summary.

Parameters:

Returns: str

rebalance_load(self)

Lines 210-220

Purpose:

Rebalance load across agents if needed.

Parameters:


Functions

demonstrate_task_allocation()

Lines 223-337

Purpose:

Demonstrate task allocation and scheduling pattern.

Key Concepts Summary

Pattern Overview

The Task Allocation Scheduling pattern provides a structured approach to building AI agents with specific capabilities and behaviors. This implementation leverages LangChain’s composable components to create a robust and maintainable solution.

Core Components

  1. Agent Class: Main implementation of the pattern logic
  2. Prompt Engineering: Carefully crafted prompts to guide LLM behavior
  3. Tool Integration: Optional tools for extended functionality
  4. Memory Management: State handling across interactions
  5. Output Parsing: Structured response processing

When to Use

✅ Good for: – Scenarios requiring this specific pattern’s strengths – Applications needing structured agent behavior – Systems where explainability is important – Production environments requiring reliability

❌ Not ideal for: – Simple single-prompt tasks – Scenarios requiring different pattern characteristics – Resource-constrained environments (if computationally intensive)


Best Practices Demonstrated

1. Environment Configuration

  • ✅ Use .env files for API keys
  • ✅ Never hardcode credentials
  • ✅ Validate environment variables on startup

2. Error Handling

  • ✅ Graceful degradation on failures
  • ✅ Informative error messages
  • ✅ Logging for debugging

3. Prompt Engineering

  • ✅ Clear, specific instructions
  • ✅ Structured output formats
  • ✅ Few-shot examples when beneficial

4. Code Organization

  • ✅ Separation of concerns (class-based design)
  • ✅ Type hints for clarity
  • ✅ Docstrings for documentation

5. Performance Optimization

  • ✅ Appropriate temperature settings
  • ✅ Token budget management
  • ✅ Caching where applicable

LangChain Components Used

  • ChatOpenAI: OpenAI model interface
  • Chains: Sequential component composition

Why LangChain? – Composable abstractions for LLM applications – Rich ecosystem of integrations – Production-ready patterns – Active community and development


Execution Flow

1. Initialize agent with configuration
   ↓
2. Load environment and dependencies
   ↓
3. Prepare prompt with input data
   ↓
4. Invoke LLM through LangChain
   ↓
5. Parse and validate response
   ↓
6. Return formatted result

Key Decision Points: – Model selection (temperature, model type) – Prompt structure (few-shot vs zero-shot) – Error handling strategy – Output format validation


Performance Considerations

Computational Cost

  • LLM Calls: Primary cost factor (based on tokens)
  • Latency: Depends on model and input size
  • Memory: Minimal for most patterns

Optimization Strategies

  1. Prompt Optimization: Minimize tokens while maintaining clarity
  2. Caching: Cache repeated queries/responses
  3. Model Selection: Use smaller models when appropriate
  4. Batch Processing: Group similar requests
  5. Streaming: For long responses, stream tokens

Cost Estimates (Approximate)

  • GPT-3.5-turbo: ~$0.002 per request
  • GPT-4: ~$0.03 per request
  • GPT-4-turbo: ~$0.01 per request

Actual costs vary based on input/output token counts


Common Pitfalls to Avoid

1. Prompt Design Issues

❌ Vague or ambiguous instructions
✅ Clear, specific, testable prompts

2. Error Handling

❌ Ignoring API errors or rate limits
✅ Implement retry logic and graceful degradation

3. Token Management

❌ Exceeding model token limits
✅ Truncate inputs, chunk processing

4. Security

❌ Exposing API keys in code
✅ Use environment variables

5. Testing

❌ Only testing happy paths
✅ Test edge cases, failures, malformed inputs

6. Cost Management

❌ Unmonitored API usage
✅ Implement budgets, alerts, and monitoring


Further Improvements

Production Enhancements

  1. Monitoring: Add logging, metrics, tracing
  2. Validation: Input/output schema validation
  3. Rate Limiting: Implement request throttling
  4. Caching: Redis or in-memory cache for responses
  5. Async Support: Non-blocking operations for scalability

Feature Extensions

  1. Multi-Model Support: Switch between providers
  2. Fallback Models: Graceful degradation strategy
  3. Custom Tools: Domain-specific function calling
  4. Advanced Memory: Vector stores for long-term memory
  5. Evaluation: Automated testing and benchmarking

Code Quality

  1. Testing: Unit tests, integration tests
  2. Documentation: API docs, usage examples
  3. Type Safety: Stricter type hints
  4. Configuration: Externalized config files

Related Patterns

  • Pattern 118
  • Pattern 120

Pattern Families: – Reasoning Patterns (001-011) – Multi-Agent Patterns (012-019) – Tool Use Patterns (020-025) – Memory Patterns (026-031) – And many more…


Summary

The Task Allocation Scheduling pattern provides a powerful approach to building AI agents with specific capabilities. This implementation demonstrates:

✅ Key Strengths: – Clean, maintainable code structure – LangChain integration for composability – Type hints and documentation – Production-ready patterns

🎯 Best Use Cases: – Applications requiring this pattern’s characteristics – Systems needing structured agent behavior – Production environments requiring reliability

📚 Learning Resources: – LangChain Documentation: https://python.langchain.com/ – Pattern Documentation: See related patterns – OpenAI API Reference: https://platform.openai.com/docs/


File: 119_task_allocation_scheduling.py
Total Lines: 342
Pattern Category: AI Agent Design Pattern #119


Generated from actual implementation code
For educational and reference purposes

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *