Compound Tools API¶
compound ¶
Compound-tool primitives — bind sub-agents to executors + evaluators.
SubAgentResult ¶
Outcome of a :meth:SubAgentTool.run invocation.
Captures the final result plus a short history of refinement attempts so consumers (metrics, trace spans, LLM tool returns) can surface "how many passes it took" without re-instrumenting.
SubAgentTool ¶
SubAgentTool(
agent: BaseAgent,
executor: Callable[[Any, StateT], Awaitable[ResultT]],
state_updater: Callable[[ResultT, StateT], None],
*,
evaluator: Callable[[ResultT], SubAgentEvalResult]
| None = None,
max_refinements: int = 0,
build_refinement_prompt: Callable[[ResultT, str], str]
| None = None,
name: str | None = None,
)
Compose a stateless sub-agent with an executor and optional refinement.
Usage — implement three domain functions and hand them to the tool:
executor(agent_output, state) -> Awaitable[ResultT]— turns the sub-agent's structured output into a real-world outcome (e.g. run the MCP pipeline and return a payload).state_updater(result, state) -> None— mutates the long- lived session state with whatever needs to persist.evaluator(result) -> SubAgentEvalResult(optional) — decides whether the result is good enough or a refinement cycle should fire.
If max_refinements > 0 and the evaluator rejects the first
result, the tool calls build_refinement_prompt(result, prompt)
to construct a new prompt and reruns the sub-agent + executor
cycle. Refinement exceptions are caught and the best prior result
is kept — matching numatics-ai v2's "best effort refinement" rule.
The tool does NOT wrap retry-on-exception; for that, wrap the
run call in :func:orqest.agents.retry.run_with_retry.
Configure the compound flow.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
BaseAgent
|
A stateless :class: |
required |
executor
|
Callable[[Any, StateT], Awaitable[ResultT]]
|
Async function that runs the real-world action using the sub-agent's output and returns a serializable result payload. |
required |
state_updater
|
Callable[[ResultT, StateT], None]
|
Synchronous mutator that writes the result back onto the state object. |
required |
evaluator
|
Callable[[ResultT], SubAgentEvalResult] | None
|
Optional quality check. When provided with
|
None
|
max_refinements
|
int
|
Number of refinement cycles allowed after
the first pass. |
0
|
build_refinement_prompt
|
Callable[[ResultT, str], str] | None
|
|
None
|
name
|
str | None
|
Optional tool name used in :class: |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in orqest/compound/sub_agent_tool.py
run
async
¶
run(
state: StateT,
prompt: str,
*,
use_enriched: bool = False,
**agent_kwargs: Any,
) -> SubAgentResult[ResultT]
Execute one (agent + executor + state-update) cycle, then
optionally refine up to max_refinements times.
The first result is always committed to state before refinement
begins — so a failed refinement leaves the original result in
place (best-effort semantics). Extra keyword arguments are
forwarded verbatim to agent.run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
StateT
|
The agent's state object. |
required |
prompt
|
str
|
The user-facing prompt for the sub-agent. |
required |
use_enriched
|
bool
|
When |
False
|
**agent_kwargs
|
Any
|
Forwarded to the agent. |
{}
|
Source code in orqest/compound/sub_agent_tool.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |