π 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 Demo
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
SupervisorOrchestratorto 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.
π₯ Who Should Use This Demo
- 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.
π What Problem It Solves
- 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.
π» Demo Application Overview
Console app that:
- Lets you choose from 7 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 a streaming handler 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
quitto 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.
- Streaming Progress:
DelegateOrchestrationStreamHandlerfor real-time monitoring of delegation flow.
Built-In Models (menu)
On startup, the sample shows a model selection menu:
| Option | Model | Approx. VRAM Needed |
|---|---|---|
| 0 | Google Gemma 3 12B | ~9 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 |
| 5 | OpenAI GPT OSS 20B | ~16 GB VRAM |
| 6 | Z.ai GLM 4.7 Flash 30B | ~18 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
- Request Analysis: supervisor examines the incoming request.
- Expertise Matching: identifies which specialist(s) are needed.
- Delegation: uses
DelegateToolto route to appropriate worker(s). - Result Collection: gathers responses from delegated workers.
- Final Assembly: combines results into a coherent response.
Streaming Handler
var streamHandler = new DelegateOrchestrationStreamHandler(
onStart: (orchestrator, input) =>
{
Console.WriteLine($"Orchestration Started ({orchestrator.Name})");
},
onToken: token =>
{
switch (token.Type)
{
case OrchestrationStreamTokenType.AgentStarted:
Console.WriteLine($"[{token.AgentName}] Thinking... (Step {token.Step})");
break;
case OrchestrationStreamTokenType.Content:
Console.Write(token.Text);
break;
case OrchestrationStreamTokenType.Delegation:
var toAgent = token.Metadata["to_agent"];
Console.WriteLine($"Delegating to {toAgent}");
break;
case OrchestrationStreamTokenType.AgentCompleted:
Console.WriteLine($"[{token.AgentName}] Done");
break;
}
},
onComplete: result =>
{
Console.WriteLine($"Orchestration Complete ({result.Duration.TotalSeconds:F1}s)");
},
onError: ex =>
{
Console.WriteLine($"Error: {ex.Message}");
});
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.Agents.Orchestration.Streaming;
using LMKit.Model;
// Load model (shared by supervisor and workers)
LM model = new LM(new Uri(modelUri));
// Create specialized worker agents
// Note: .WithPersona() sets the agent name, .WithInstruction() sets the detailed behavior.
var codeExpert = Agent.CreateBuilder(model)
.WithPersona("CodeExpert")
.WithInstruction(@"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")
.WithInstruction(@"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")
.WithInstruction(@"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")
.WithInstruction(@"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("TaskCoordinator")
.WithInstruction(@"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 with streaming for real-time feedback
var streamHandler = CreateStreamHandler(); // See Streaming Handler section above
var result = await supervisor.RunStreamingAsync(
userRequest,
streamHandler,
cancellationToken: cancellationToken);
Console.WriteLine($"Agents involved: {result.AgentResults.Count}");
Console.WriteLine($"Duration: {result.Duration.TotalSeconds:F1}s");
ποΈ 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 |
|---|---|---|
| Magenta | Orchestration Status | Orchestration start/complete banners |
| Yellow | Agent Started / Delegation | Agent starting a step, or delegating to another agent |
| White | Content | Main response content from agents |
| Dark Gray | Thinking | Agent's internal reasoning |
| Blue | Tool Call | Tool invocation with arguments |
| Green | Tool Result | Tool return values |
| Dark Cyan | Agent Completed | Agent finished notification |
| Dark Yellow | Status | Status updates |
| Red | Error | Error messages |
| Cyan | Request Prompt | User input prompt |
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 (3.3-18 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:
- Select a model by typing 0-6, or paste a custom model URI.
- Wait for the model to download (first run) and load.
- Enter a request (simple or complex).
- Watch the supervisor analyze and delegate.
- See specialists handle their portions.
- Receive the combined result.
- Type
quitto 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.