Subagent

class Subagent : Tool

A Tool that delegates to another agent as a subagent/handoff.

When the LLM invokes this tool, it runs the specified agent as a subprocess, sharing the parent process's blackboard context. This enables composition of agents and "handoff" patterns where one agent delegates specialized tasks to another.

Usage

Create a Subagent using one of the factory methods:

// From an @Agent annotated class
Subagent.ofClass(MyAgent::class.java)
Subagent.ofClass<MyAgent>() // Kotlin reified version

// From an agent name (resolved at runtime)
Subagent.byName("MyAgent")

// From an Agent instance
Subagent.ofInstance(resolvedAgent)

// From an instance of an @Agent annotated class
Subagent.ofAnnotatedInstance(myAgentBean)

Use with withTool() on a PromptRunner:

context.ai()
.withTool(Subagent.ofClass(MyAgent::class.java))
.creating(Result::class.java)
.fromPrompt("...")

For asset tracking, wrap with AssetAddingTool:

context.ai()
.withTool(assetTracker.addReturnedAssets(Subagent.ofClass(MyAgent::class.java)))
.creating(Result::class.java)
.fromPrompt("...")

Input Type Resolution

The input type for the subagent is automatically determined by introspecting the agent's actions. It finds the first non-injected input binding from the agent's first action.

Types

Link copied to clipboard
sealed class Builder

Builder step that requires specifying the input type. Use consuming to complete the Subagent creation.

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
open override val definition: Tool.Definition

Tool definition for LLM

Link copied to clipboard
open override val metadata: Tool.Metadata

Optional metadata

Functions

Link copied to clipboard
open override fun call(input: String): Tool.Result

Execute the tool with JSON input.

Link copied to clipboard
inline fun <T : Any> Tool.requireType(message: String? = null): Tool

Wrap this tool to require a value of type T before execution (reified).

fun <T : Any> Tool.requireType(type: Class<T>, messageProvider: (String) -> String? = { null }): Tool

Wrap this tool to require a value of type T before execution.

Link copied to clipboard
fun Tool.toSpringToolCallback(): <Error class: unknown class>

Extension function to convert an Embabel Tool to a Spring AI ToolCallback.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
inline fun <T : Tool> Tool.unwrapAs(): T?

Unwrap a tool to find a specific type, or return null if not found.

Link copied to clipboard

Wrap this tool to conditionally await before execution.

Link copied to clipboard
fun Tool.withConfirmation(messageProvider: (String) -> String): Tool

Wrap this tool to always require confirmation before execution.

Link copied to clipboard
open fun withDescription(newDescription: String): Tool

Create a new tool with a different description. Useful for providing context-specific descriptions while keeping the same functionality.

Link copied to clipboard
fun Tool.withEventPublication(agentProcess: AgentProcess, action: Action?, llmOptions: <Error class: unknown class>): Tool

Extension function to wrap a Tool with event publication.

Link copied to clipboard
open fun withName(newName: String): Tool

Create a new tool with a different name. Useful for namespacing tools when combining multiple tool sources.

Link copied to clipboard
open fun withNote(note: String): Tool

Create a new tool with an additional note appended to the description. Useful for adding context-specific hints to an existing tool.