Router Delegate#
Source: examples/patterns/router_delegate.py
Introduction#
RouteLLM motivates specialized route selection, AutoGen demonstrates multi-agent delegation patterns, and Human-AI collaboration by design frames why explicit routing supports accountable coordination. This example shows intent-based routing across direct and multi-step agents using a shared runtime surface.
Note
This example’s checked-in local LlamaCppServerLLMClient config uses a
Qwen3-4B GGUF model. On lower-RAM machines, swap in a smaller local
model or start with Ollama Local Client.
Technical Implementation#
Configure
Tracerwith JSONL + console output so each run emits machine-readable traces and lifecycle logs.Build the runtime surface (public APIs only) and execute
RouterDelegatePattern.run(...)with a fixedrequest_id.Configure and invoke
Toolboxintegrations (core/script/MCP/callable) before assembling the final payload.Print a compact JSON payload including
trace_infofor deterministic tests and docs examples.
flowchart LR
A["Input prompt or scenario"] --> B["main(): runtime wiring"]
B --> C["RouterDelegatePattern.run(...)"]
C --> D["router delegates to specialized agent surfaces"]
C --> E["Tracer JSONL + console events"]
D --> F["ExecutionResult/payload"]
E --> F
F --> G["Printed JSON output"]
1from __future__ import annotations
2
3import json
4from pathlib import Path
5
6import design_research_agents as drag
7
8# This checked-in local config uses a Qwen3-4B GGUF model to exercise a richer
9# multi-step path. On lower-RAM machines, swap in a smaller local model or
10# start with the lighter Ollama local client example first.
11_EXAMPLE_LLAMA_CLIENT_KWARGS = {
12 "model": "Qwen_Qwen3-4B-Instruct-2507-Q4_K_M.gguf",
13 "hf_model_repo_id": "bartowski/Qwen_Qwen3-4B-Instruct-2507-GGUF",
14 "api_model": "qwen3-4b-instruct-2507-q4km",
15 "context_window": 8192,
16 "startup_timeout_seconds": 240.0,
17 "request_timeout_seconds": 240.0,
18}
19
20
21def main() -> None:
22 """Route one design prompt to the best delegate and print summary."""
23 # Fixed request id keeps traces and docs output deterministic across runs.
24 request_id = "example-workflow-router-delegate-design-001"
25 tracer = drag.Tracer(
26 enabled=True,
27 trace_dir=Path("artifacts/examples/traces"),
28 enable_jsonl=True,
29 enable_console=True,
30 )
31 # Run the router/delegate pattern using public runtime surfaces. Using this with statement will automatically
32 # shut down the managed client and tool runtime when the example is done.
33 with drag.Toolbox() as tool_runtime, drag.LlamaCppServerLLMClient(**_EXAMPLE_LLAMA_CLIENT_KWARGS) as llm_client:
34 direct_llm_agent = drag.DirectLLMCall(llm_client=llm_client, tracer=tracer)
35 json_tool_agent = drag.MultiStepAgent(
36 mode="json",
37 llm_client=llm_client,
38 tool_runtime=tool_runtime,
39 max_steps=3,
40 allowed_tools=("text.word_count",),
41 tracer=tracer,
42 )
43
44 workflow = drag.RouterDelegatePattern(
45 llm_client=llm_client,
46 tool_runtime=tool_runtime,
47 alternatives={
48 "direct_llm_agent": direct_llm_agent,
49 "json_tool_agent": json_tool_agent,
50 },
51 alternative_descriptions={
52 "direct_llm_agent": "Use for concise textual design summaries with no runtime tools.",
53 "json_tool_agent": ("Use for design requests needing runtime text analysis or tool calls."),
54 },
55 tracer=tracer,
56 )
57
58 result = workflow.run(
59 prompt=(
60 "Count the words in the phrase 'modular field service workflow' using the "
61 "appropriate delegate and return only the word_count."
62 ),
63 request_id=request_id,
64 )
65
66 # Print the results
67 summary = result.summary()
68 print(json.dumps(summary, ensure_ascii=True, indent=2, sort_keys=True))
69
70
71if __name__ == "__main__":
72 main()
Expected Results#
Run Command
PYTHONPATH=src python3 examples/patterns/router_delegate.py
Example output shape (values vary by run):
{
"success": true,
"final_output": "<example-specific payload>",
"terminated_reason": "<string-or-null>",
"error": null,
"trace": {
"request_id": "<request-id>",
"trace_dir": "artifacts/examples/traces",
"trace_path": "artifacts/examples/traces/run_<timestamp>_<request_id>.jsonl"
}
}