Step 3 of 5
Single-agent, multi-agent orchestration, router, sequential pipeline patterns
Design patterns สำหรับ agents — single, multi-agent, router, pipeline
Not every task needs one agent. As work gets complex, you arrange agents and steps into a pattern. Koog supports all of these. A basic AIAgent covers the first; the rest are compositions you build with Kotlin coroutines and Koog's strategy graphs.
Diagram: Four common patterns side by side.
Loading diagram...
| Pattern | Use when |
|---|---|
| Single agent | One task, a few tools. Most cases start here. |
| Router | Different question types need different experts. |
| Sequential pipeline | Fixed steps: search, then summarize, then translate. |
| Parallel | Independent subtasks you can run at the same time. |
Start with a single agent. Only split into multiple agents when one model prompt gets too big or too slow.
One AIAgent with all tools. Simple, fast to build. See 02-defining-tools-and-functions.md.
A small "router" agent reads the input and picks a category. Then you run the matching specialist agent.
suspend fun routeAndAnswer(question: String): String {
val router = AIAgent(
promptExecutor = simpleOpenAIExecutor(apiKey),
llmModel = OpenAIModels.Chat.GPT4o,
systemPrompt = "Reply with exactly one word: MATH, WEATHER, or OTHER."
)
val category = router.run(question).trim().uppercase()
return when (category) {
"MATH" -> mathAgent.run(question)
"WEATHER" -> weatherAgent.run(question)
else -> generalAgent.run(question)
}
}
Chain agents so each output feeds the next. This is also expressible as a Koog strategy graph (nodeStart -> nodeA -> nodeB -> nodeFinish), but a plain function chain is clear and easy to test:
suspend fun pipeline(topic: String): String {
val research = researchAgent.run("Find facts about: $topic")
val summary = summaryAgent.run("Summarize: $research")
return translationAgent.run("Translate to Thai: $summary")
}
When subtasks are independent, run them with async and collect with awaitAll. Koog itself uses this for parallel tool calls and parallel strategy nodes.
import kotlinx.coroutines.*
suspend fun parallelAnswer(question: String): String = coroutineScope {
val parts = listOf(
async { agent.run("Part 1: $question") },
async { agent.run("Part 2: $question") },
async { agent.run("Part 3: $question") }
).awaitAll()
parts.joinToString("\n---\n")
}
All three calls run at the same time. Total time is close to the slowest call, not the sum.
MATH questions to a calculator agent and everything else to a general agent.