Table of Contents

πŸ‘‰ Try the demo: https://github.com/LM-Kit/lm-kit-net-samples/tree/main/console_net/agents/smart_task_router

Smart Task Router for C# .NET Applications


Purpose of the Sample

Smart Task Router demonstrates how to use LM-Kit.NET to build an intelligent task delegation system using the SupervisorOrchestrator pattern where a coordinator agent analyzes requests and routes them to specialized worker agents.

The sample shows how to:

  • Create multiple specialized worker agents with distinct personas and expertise.
  • Build a supervisor agent that coordinates task routing decisions.
  • Use SupervisorOrchestrator to manage delegation workflow.
  • Observe delegation events in real-time.
  • Handle complex multi-faceted requests that require multiple specialists.

Why Supervisor Orchestration with LM-Kit.NET?

  • Dynamic routing: supervisor intelligently decides which specialist handles each task.
  • Specialization: each worker agent is optimized for specific domains.
  • Transparency: see the routing logic and delegation decisions in real-time.
  • Scalability: easily add new specialists for additional capabilities.
  • Local-first: all orchestration runs on your hardware.

Target Audience

  • Enterprise Developers: build intelligent request routing systems.
  • Support Teams: create AI-powered ticket triage and routing.
  • Platform Engineers: implement multi-capability AI assistants.
  • AI/ML Engineers: explore supervisor-worker patterns with local LLMs.
  • Automation Specialists: design flexible task delegation workflows.

Problem Solved

  • Intelligent routing: requests are analyzed and sent to the best specialist.
  • Multi-skill tasks: complex requests can involve multiple specialists.
  • Expertise matching: each task type is handled by a domain expert.
  • Transparent decisions: see why the supervisor chose each specialist.
  • Unified interface: single entry point for diverse task types.

Sample Application Description

Console app that:

  • Lets you choose from 5 models suitable for coordination and reasoning.
  • Creates four specialized worker agents (Code, Data, Writer, Researcher).
  • Creates a supervisor agent that coordinates task routing.
  • Sets up event handlers to show delegation decisions in real-time.
  • Enters an interactive request loop where you can:
    • Enter any request (simple or complex).
    • Watch the supervisor analyze and delegate.
    • See each specialist handle their portion.
    • Receive the combined final response.
  • Displays execution statistics (agents involved, success status).
  • Loops until you type quit to exit.

Key Features

  • SupervisorOrchestrator: dynamic task routing via supervisor agent.
  • Four Specialist Workers: CodeExpert, DataAnalyst, Writer, Researcher.
  • Real-Time Delegation Display: see routing decisions as they happen.
  • Multi-Step Tasks: complex requests can involve multiple specialists.
  • Chain-of-Thought Planning: workers use structured reasoning.
  • Event-Driven Progress: hooks for monitoring delegation flow.

Built-In Models (menu)

On startup, the sample shows a model selection menu:

Option Model Approx. VRAM Needed
0 Google Gemma 3 4B ~4 GB VRAM
1 Microsoft Phi-4 Mini 3.8B ~3.3 GB VRAM
2 Meta Llama 3.1 8B ~6 GB VRAM
3 Alibaba Qwen-3 8B ~5.6 GB VRAM
4 Microsoft Phi-4 14.7B ~11 GB VRAM
other Custom model URI depends on model

The same model is used for both supervisor and all workers. Larger models provide better routing decisions.


Worker Specializations

Worker Expertise Handles
CodeExpert Programming, debugging, code review Code writing, bug fixes, refactoring, design patterns
DataAnalyst Statistics, trends, visualization Data analysis, metrics, reporting, insights
Writer Documentation, content, communication Articles, docs, explanations, editing
Researcher Information synthesis, learning Research, comparisons, deep dives, learning topics

Routing & Flow

Supervisor Decision Process

  1. Request Analysis: supervisor examines the incoming request.
  2. Expertise Matching: identifies which specialist(s) are needed.
  3. Delegation: uses DelegateTool to route to appropriate worker(s).
  4. Result Collection: gathers responses from delegated workers.
  5. Final Assembly: combines results into a coherent response.

Event Hooks

supervisor.BeforeAgentExecution += (sender, e) =>
{
    Console.WriteLine($">> Executing agent...");
    Console.WriteLine($"   Input: {e.OriginalInput}");
};

supervisor.AfterAgentExecution += (sender, e) =>
{
    Console.WriteLine($"<< Agent completed (Status: {e.Result.Status})");
};

Example Requests

Try the sample with:

  • Single-specialist tasks:

    • "Write a Python function to sort a list"
    • "Analyze this sales data: Q1: $50K, Q2: $75K, Q3: $60K, Q4: $90K"
    • "Write documentation for a REST API endpoint"
  • Multi-specialist tasks:

    • "Write a Python function to sort a list and explain how it works" (CodeExpert + Writer)
    • "Research best practices for REST API design and write documentation for our team" (Researcher + Writer)
    • "Help me understand machine learning - I need both theory and a code example" (Researcher + CodeExpert)

Agent Configuration

using LMKit.Agents;
using LMKit.Agents.Orchestration;
using LMKit.Model;

// Load model (shared by supervisor and workers)
LM model = new LM(new Uri(modelUri));

// Create specialized worker agents
var codeExpert = Agent.CreateBuilder(model)
    .WithPersona(@"CodeExpert - You are an expert programmer and software developer.
Your specialties include:
- Writing clean, efficient code in multiple languages
- Debugging and code review
- Explaining programming concepts
- Suggesting best practices and design patterns")
    .WithPlanning(PlanningStrategy.ChainOfThought)
    .Build();

var dataAnalyst = Agent.CreateBuilder(model)
    .WithPersona(@"DataAnalyst - You are an expert data analyst with strong statistical skills.
Your specialties include:
- Analyzing numerical data and identifying trends
- Creating summaries and insights from data
- Statistical analysis and interpretation
- Data visualization recommendations")
    .WithPlanning(PlanningStrategy.ChainOfThought)
    .Build();

var writer = Agent.CreateBuilder(model)
    .WithPersona(@"Writer - You are a professional technical writer and content creator.
Your specialties include:
- Writing clear documentation and guides
- Creating engaging articles and explanations
- Summarizing complex topics for various audiences")
    .WithPlanning(PlanningStrategy.None)
    .Build();

var researcher = Agent.CreateBuilder(model)
    .WithPersona(@"Researcher - You are a thorough researcher and knowledge synthesizer.
Your specialties include:
- Deep diving into topics for comprehensive information
- Comparing and contrasting different approaches
- Synthesizing information from multiple perspectives")
    .WithPlanning(PlanningStrategy.ChainOfThought)
    .Build();

// Create supervisor agent
var supervisorAgent = Agent.CreateBuilder(model)
    .WithPersona(@"You are a Task Coordinator responsible for routing user requests to specialists.

Available specialists:
- CodeExpert: Programming, code writing, debugging, software development
- DataAnalyst: Data analysis, statistics, trends, metrics interpretation
- Writer: Documentation, content creation, explanations, editing
- Researcher: Information gathering, comparisons, deep dives, learning

For each user request:
1. Analyze what type of expertise is needed
2. Delegate to the most appropriate specialist(s)
3. If a task needs multiple skills, delegate to each relevant specialist
4. Combine responses into a coherent final answer")
    .WithPlanning(PlanningStrategy.ChainOfThought)
    .Build();

// Create supervisor orchestrator
var supervisor = new SupervisorOrchestrator(supervisorAgent)
    .AddWorker(codeExpert)
    .AddWorker(dataAnalyst)
    .AddWorker(writer)
    .AddWorker(researcher);

// Execute request
var result = await supervisor.ExecuteAsync(userRequest, cancellationToken);

Console.WriteLine($"Final Response: {result.Content}");
Console.WriteLine($"Agents involved: {result.AgentResults.Count}");

Architecture

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   User Input    β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   Supervisor    β”‚
                    β”‚     Agent       β”‚
                    β”‚  (Coordinator)  β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
            β”‚      DelegateTool Routes        β”‚
            β”‚        Based on Task            β”‚
            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                             β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚            β”‚           β”‚           β”‚            β”‚
    β–Ό            β–Ό           β–Ό           β–Ό            β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”       β”‚
β”‚  Code  β”‚  β”‚  Data  β”‚  β”‚ Writer β”‚  β”‚Researchβ”‚       β”‚
β”‚ Expert β”‚  β”‚Analyst β”‚  β”‚        β”‚  β”‚   er   β”‚       β”‚
β””β”€β”€β”€β”€β”¬β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”˜       β”‚
     β”‚           β”‚           β”‚           β”‚            β”‚
     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜            β”‚
                             β”‚                        β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”               β”‚
                    β”‚   Supervisor    β”‚β—€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚   Assembles     β”‚  (may delegate again)
                    β”‚    Response     β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Understanding the Output

The demo displays:

Color Content Type Description
Yellow Delegation Start Agent execution beginning with input preview
Green Delegation Complete Agent finished with status
White Final Response Combined result from all specialists
Cyan Request Prompt User input prompt
Dark Gray Statistics Agents involved and success status

Behavior & Policies

  • Model sharing: same model used for supervisor and all workers.
  • Dynamic routing: supervisor decides delegation at runtime.
  • Multi-delegation: complex tasks may involve multiple workers.
  • Sequential processing: workers execute as supervisor delegates to them.
  • Result assembly: supervisor combines worker outputs.
  • Timeout: 5-minute timeout per request.
  • Licensing: set an optional license key via LicenseManager.SetLicenseKey("").

Getting Started

Prerequisites

  • .NET 8.0 or later
  • Sufficient VRAM for the selected model (4-11 GB depending on model choice)

Download

git clone https://github.com/LM-Kit/lm-kit-net-samples
cd lm-kit-net-samples/console_net/agents/smart_task_router

Run

dotnet build
dotnet run

Then:

  1. Select a model by typing 0-4, or paste a custom model URI.
  2. Wait for the model to download (first run) and load.
  3. Enter a request (simple or complex).
  4. Watch the supervisor analyze and delegate.
  5. See specialists handle their portions.
  6. Receive the combined result.
  7. Type quit to exit.

Troubleshooting

  • Supervisor doesn't delegate

    • Request may be too simple; supervisor handles directly.
    • Try a more complex request requiring specialist expertise.
  • Wrong specialist chosen

    • Supervisor persona may need refinement.
    • Be more explicit about the type of help needed.
    • Use a larger model for better routing decisions.
  • Slow execution

    • Multiple agents means multiple inference calls.
    • Use a smaller or faster model.
    • Simplify request to involve fewer specialists.
  • Out-of-memory errors

    • Single model is shared, so VRAM usage is manageable.
    • Pick a smaller model if needed.
  • Timeout errors

    • Complex multi-agent tasks may exceed 5-minute timeout.
    • Try simpler requests or increase timeout.

Extend the Demo

  • Add more specialists: create domain-specific workers (legal, medical, financial).
  • Hierarchical routing: create sub-supervisors for specialized domains.
  • Worker tools: give specialists their own tools (calculators, databases, APIs).
  • Parallel execution: run independent specialists concurrently.
  • Routing analytics: track which specialists handle which request types.
  • Custom delegation logic: implement rules-based routing alongside LLM decisions.

Additional Resources