Tools
The AgentTool Trait
Every tool implements AgentTool:
#![allow(unused)] fn main() { #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(not(target_arch = "wasm32"), async_trait)] pub trait AgentTool: rt::MaybeSend + rt::MaybeSync { fn name(&self) -> &str; fn label(&self) -> &str; fn description(&self) -> &str; fn parameters_schema(&self) -> serde_json::Value; async fn execute( &self, params: serde_json::Value, ctx: ToolContext, ) -> Result<ToolResult, ToolError>; } }
rt::MaybeSend + rt::MaybeSync is exactly Send + Sync on native targets, so
a plain #[async_trait] impl is unchanged there. On wasm32 the bound is empty;
see WebAssembly & Cloudflare Workers.
| Method | Purpose |
|---|---|
name() | Unique ID sent to LLM (e.g., "bash") |
label() | Human-readable name for UI (e.g., "Run Command") |
description() | Tells the LLM what the tool does |
parameters_schema() | JSON Schema for the tool's parameters |
execute() | Runs the tool, returns ToolResult or ToolError. Receives a ToolContext with cancellation, update, and progress callbacks. |
ToolContext
All execution context is bundled into a single struct, making the trait easier to extend in the future:
#![allow(unused)] fn main() { pub struct ToolContext { pub tool_call_id: String, pub tool_name: String, pub cancel: CancellationToken, pub on_update: Option<ToolUpdateFn>, pub on_progress: Option<ProgressFn>, } }
| Field | Purpose |
|---|---|
tool_call_id | Unique ID for this tool call (for correlating events) |
tool_name | Name of the tool being executed |
cancel | Cancellation token — check ctx.cancel.is_cancelled() in long-running tools |
on_update | Callback for streaming partial ToolResult updates to the UI (emits ToolExecutionUpdate) |
on_progress | Callback for emitting user-facing progress messages (emits ProgressMessage) |
ToolContext implements Clone and Debug. It is #[non_exhaustive]: outside
the loop (tests, or driving a tool directly) build one with
ToolContext::new(id, name) and its with_* builders. Delegation tools report
a child run's stats with ctx.report_delegated_run(..).
ToolResult
#![allow(unused)] fn main() { pub struct ToolResult { pub content: Vec<Content>, pub details: serde_json::Value, } }
The content is sent back to the LLM. The details field holds metadata (not sent to the LLM) for UI/logging.
ToolError
#![allow(unused)] fn main() { pub enum ToolError { Failed(String), NotFound(String), InvalidArgs(String), Cancelled, } }
Errors are converted to ToolResult with is_error: true and sent back to the LLM so it can recover.
Implementing a Custom Tool
#![allow(unused)] fn main() { use yoagent::types::*; use async_trait::async_trait; pub struct WeatherTool; #[async_trait] impl AgentTool for WeatherTool { fn name(&self) -> &str { "get_weather" } fn label(&self) -> &str { "Weather" } fn description(&self) -> &str { "Get current weather for a city." } fn parameters_schema(&self) -> serde_json::Value { serde_json::json!({ "type": "object", "properties": { "city": { "type": "string", "description": "City name" } }, "required": ["city"] }) } async fn execute( &self, params: serde_json::Value, _ctx: ToolContext, ) -> Result<ToolResult, ToolError> { let city = params["city"].as_str() .ok_or(ToolError::InvalidArgs("missing city".into()))?; // Call weather API... Ok(ToolResult { content: vec![Content::Text { text: format!("Weather in {}: 72°F, sunny", city), }], details: serde_json::Value::Null, }) } } }
The built-in tools (
BashTool,ReadFileTool,WriteFileTool,EditFileTool,ListFilesTool,SearchTool) anddefault_tools()need the defaultnativefeature. They do not exist withdefault-features = falseor on wasm32.
Register custom tools alongside defaults:
#![allow(unused)] fn main() { use yoagent::tools::default_tools; let mut tools = default_tools(); tools.push(Box::new(WeatherTool)); let agent = Agent::from_config(ModelConfig::anthropic("claude-sonnet-5", "Claude Sonnet 5")).with_tools(tools); }
Sandboxing the built-in file tools
ReadFileTool, WriteFileTool, EditFileTool, ListFilesTool and SearchTool accept an allowlist of directory roots. Empty (the default) means unrestricted:
#![allow(unused)] fn main() { let roots = vec!["/srv/workspace".to_string()]; let tools: Vec<Box<dyn AgentTool>> = vec![ Box::new(ReadFileTool::new().with_allowed_paths(roots.clone())), Box::new(WriteFileTool::new().with_allowed_paths(roots.clone())), Box::new(EditFileTool::new().with_allowed_paths(roots)), ]; }
Enforcement is against the resolved path, not the string, so neither .. nor a symlink pointing outside can escape. That includes a dangling symlink, which a write would follow to create its target; a symlink loop is refused. A file that does not exist yet still resolves through its real parent, so writes are checked too, and the tool then does its I/O on the path it checked. Before 0.24.2, a .. after a not-yet-existing directory, or a dangling symlink, could get past the check. Something changing the tree between the check and the I/O, such as a parallel bash call, is not covered. The rejection message deliberately does not echo the allowed roots, since tool results reach the model's transcript.
With or without an allowlist, read_file, write_file and edit_file work on regular files only: a FIFO, a device, a socket or a directory (after following symlinks) is refused with an error naming what it is. Before 0.25.8 a FIFO or /dev/zero could hang the call.
write_file after a partial read
read_file returns a long file one page at a time (500 lines unless the call gives a limit), and write_file replaces the whole file. A model that read lines 1–500 of a 2,140-line file and then writes the file back would delete lines 501–2,140 without having seen them. When read_file and write_file share a ReadLog, that write is refused:
Refusing to overwrite src/big.rs: you have read only lines 1-500 of its 2140 lines, so writing
the whole file would delete lines you have not seen. Read the rest with read_file
(offset/limit), or change it in place with edit_file.
- Refused: a
write_fileto an existing file whose latest read by this agent covered only part of it. Pages add up: reading 1–500 and then 501–2,140 counts as the whole file. - Allowed: a file read in full and unchanged since (in one read or in pages of the same version), even after it reads a page of that same version again; a file this agent last wrote itself; a file it never read (this guard is only about partial reads); a path that does not exist.
- Versions are told apart by length and modification time, so a same-length rewrite within the filesystem's timestamp resolution is not seen. A file that changed on disk after the partial read stays refused, and the message says it changed; pages of the old version do not count towards the new one, so the model reads it again. A file read in full that changed since and is then read in part is refused the same way. A change after a full read with no read since is not re-checked.
- A read that shows no lines (an
offsetpast the end,limit: 0) records nothing: what was known before stays. edit_fileis not affected: it replaces text the model gave exactly.- Paths are compared after the sandbox's resolution, so
./src/a.rs,src/a.rsandsrc/../src/a.rsare one file. Sandboxed or not makes no difference.
default_tools() shares one log between its read_file and write_file, so every agent built from it is protected (each call is its own log — one per agent). Tools built on their own record nothing and overwrite as before; share a log to protect them:
#![allow(unused)] fn main() { use yoagent::tools::{ReadFileTool, ReadLog, WriteFileTool}; let log = ReadLog::new(); let tools: Vec<Box<dyn AgentTool>> = vec![ Box::new(ReadFileTool::new().with_allowed_paths(roots.clone()).with_read_log(log.clone())), Box::new(WriteFileTool::new().with_allowed_paths(roots).with_read_log(log)), ]; }
Give a sub-agent its own log: a shared one would count the parent's reads as the child's.
Requiring a read before any overwrite
By default a file the agent never read can be overwritten: the guard above is about partial reads only. ReadLog::require_read(true) closes that case too, for a model that replaces a file it never opened:
#![allow(unused)] fn main() { let log = ReadLog::new().require_read(true); }
Refusing to overwrite src/lib.rs: it exists and you have not read it, so writing it would
replace content you have not seen. Read it with read_file first, or change it in place with
edit_file. If read_file cannot show it (it is not UTF-8 text, or an image it refuses),
write_file cannot replace it: do not retry.
- With it on, an existing file is overwritten only once the log knows it whole: read in full (in one read or in pages), or last written by this agent. A path that does not exist yet is always writable, and
edit_fileis not affected. - The setting belongs to the log, so it applies to every clone, whenever it is set. It is off by default, and
default_tools()leaves it off. - The cost: rewriting a file longer than one
read_filepage (500 lines, fewer when the page reachesmax_bytes) means reading every page first. - A file
read_filecannot show can then not be replaced withwrite_fileat all: one that is not UTF-8 text, an image format it refuses, an image too large. The refusal says so, so the model does not retry the read. A file with a line longer thanread_filereturns is shown with that line cut and stays partly read: it can be changed withedit_fileonly. - The log knows only what its own
read_fileshowed. A file the model saw throughbash(cat), through another agent's tools, or before the tools were rebuilt counts as never read. - As above, a change on disk after a full read, with no read since, is not re-checked.
The log lives as long as the tools, not the conversation: Agent::reset(), compaction and a new SubAgentTool delegation do not clear it (the tool Arcs are reused), and a read whose output is truncated or compacted afterwards still counts as read. Build a fresh default_tools() (or a fresh ReadLog) to start over.
BashTool is not a sandbox
BashTool runs whatever the model asks through bash -c, with the agent process's own environment and filesystem access.
deny_patterns is a substring check that catches typos and obvious mistakes — rm -rf / with two spaces, a base64-decoded pipe, or an equivalent find -delete all sail past it. Treat it as a guardrail, never as a security boundary.
Real isolation belongs outside the tool: run the agent in a container or VM, or gate calls through ToolMiddleware, which sees the arguments before execution and can deny them.
For credentials specifically, commands inherit every environment variable the agent process holds — including any *_API_KEY. When the model composes the command, restrict what it can read:
#![allow(unused)] fn main() { let bash = BashTool::default() .with_env_allowlist(vec!["RUST_LOG".to_string()]); // plus PATH, HOME, PWD }
Error Handling
Return Err(ToolError) on failure, not Ok with error text. When a tool returns Err, the agent loop converts it to a Message::ToolResult with is_error: true and sends it to the LLM. The LLM sees the error and can self-correct — retry with different arguments, try a different approach, or explain the failure to the user.
#![allow(unused)] fn main() { async fn execute(&self, params: serde_json::Value, _ctx: ToolContext) -> Result<ToolResult, ToolError> { let path = params["path"].as_str() .ok_or(ToolError::InvalidArgs("missing 'path'".into()))?; let content = std::fs::read_to_string(path) .map_err(|e| ToolError::Failed(format!("Cannot read {}: {}", path, e)))?; Ok(ToolResult { content: vec![Content::Text { text: content }], details: serde_json::Value::Null, }) } }
Exception: BashTool. The built-in BashTool returns Ok even on non-zero exit codes, with both stdout and stderr in the result. This is intentional — the LLM needs to see the actual error output (compilation errors, test failures, etc.) to diagnose and fix issues. Only failures outside the command return Err: a matched deny pattern, a refused confirmation, a timeout (its message carries the output printed so far), cancellation, or bash failing to start. A command that does not exist is an ordinary non-zero exit (127). On Unix, a timeout, a cancel or dropping the call kills the command's whole process group — pipeline stages, commands in a && list and background jobs included; only a process that starts its own session (setsid) escapes. A command that finishes on its own leaves its background jobs running. The call ends when bash exits: the output is read for one more second, then a job still holding the tool's output is left running, what it prints from then on is discarded, and the result says so (a NOTE: line, details.output_still_open). To keep a job's output, redirect it (npm run dev >dev.log 2>&1 &). Before 0.25.8 such a job held the call until the timeout, which killed it. Because the command runs in its own process group, the terminal's Ctrl+C no longer reaches it: a host should cancel or drop the call on Ctrl+C (the CLI example aborts the run), and a command that prompts on /dev/tty (sudo, an SSH passphrase) stops until the timeout. On Windows only the bash process is killed.
A panicking tool is contained by the loop, the same as a panicking middleware. The call ends as an error result (tool '<name>' panicked: …) that the model sees, and the run continues.
Tool Execution Flow
- LLM returns
Content::ToolCallblocks in its response - Agent loop emits
ToolExecutionStartfor each - Tool's
execute()is called with parsed arguments - Result (or error) is wrapped in
Message::ToolResult ToolExecutionEndis emitted- All tool results are added to context
- Loop continues with another LLM call
Streaming Tool Output
Long-running tools can stream progress updates to the UI via the on_update callback. Each call emits a ToolExecutionUpdate event. Partial results are for UI/logging only — they are not sent to the LLM. Only the final ToolResult returned from execute() becomes part of the conversation.
The ToolUpdateFn type
#![allow(unused)] fn main() { pub type ToolUpdateFn = Arc<dyn Fn(ToolResult) + Send + Sync>; }
Basic usage
Call on_update whenever you have progress to report:
#![allow(unused)] fn main() { use yoagent::types::*; struct DataProcessorTool; #[async_trait] impl AgentTool for DataProcessorTool { // ... name, label, description, parameters_schema ... async fn execute( &self, params: serde_json::Value, ctx: ToolContext, ) -> Result<ToolResult, ToolError> { let rows = fetch_rows(¶ms)?; let total = rows.len(); for (i, row) in rows.iter().enumerate() { // Check for cancellation if ctx.cancel.is_cancelled() { return Err(ToolError::Cancelled); } process_row(row); // Stream progress every 100 rows if i % 100 == 0 { if let Some(ref cb) = &ctx.on_update { cb(ToolResult { content: vec![Content::Text { text: format!("Processed {}/{} rows", i, total), }], details: serde_json::json!({"progress": i as f64 / total as f64}), }); } } } Ok(ToolResult { content: vec![Content::Text { text: format!("Processed all {} rows", total), }], details: serde_json::Value::Null, }) } } }
Consuming updates in your UI
Updates arrive as AgentEvent::ToolExecutionUpdate events on the same event stream as all other agent events:
#![allow(unused)] fn main() { while let Some(event) = rx.recv().await { match event { AgentEvent::ToolExecutionStart { tool_name, .. } => { println!("⏳ {} started", tool_name); } AgentEvent::ToolExecutionUpdate { tool_name, partial_result, .. } => { // Show progress in your UI if let Some(Content::Text { text }) = partial_result.content.first() { println!(" 📊 {}: {}", tool_name, text); } } AgentEvent::ToolExecutionEnd { tool_name, is_error, .. } => { println!("{} {}", if is_error { "❌" } else { "✅" }, tool_name); } AgentEvent::ProgressMessage { tool_name, text, .. } => { println!(" 💬 {}: {}", tool_name, text); } _ => {} } } }
Progress Messages
In addition to on_update (which streams partial ToolResult values), tools can emit lightweight text-only progress messages via ctx.on_progress. These appear as AgentEvent::ProgressMessage events:
#![allow(unused)] fn main() { async fn execute(&self, params: serde_json::Value, ctx: ToolContext) -> Result<ToolResult, ToolError> { if let Some(ref progress) = &ctx.on_progress { progress("Starting analysis...".into()); } // ... do work ... if let Some(ref progress) = &ctx.on_progress { progress("Almost done...".into()); } Ok(ToolResult { /* ... */ }) } }
Use on_progress for simple status text. Use on_update when you need structured data (progress percentages, partial results).
Guidelines
- Call
on_updateas often as useful — there's no rate limit. The callback is synchronous and cheap. - Always check
ctx.on_update.is_some()before building theToolResult. IfNone, the loop isn't interested in updates (e.g., testing). - Use
detailsfor structured data —contentis for human-readable text,detailscan carry progress percentages, byte counts, etc. - Don't rely on updates reaching the LLM — they won't. Only the final return value is added to context.
- Simple tools don't need it — if your tool completes in <1 second, just ignore
ctx(prefix with_ctxto suppress the warning).
End-to-end example
Here's a complete example: a CLI agent with a deploy tool that streams progress. The human sees real-time output while the LLM only gets the final result.
use yoagent::agent::Agent; use yoagent::provider::ModelConfig; use yoagent::types::*; /// A tool that deploys an app and streams each step. struct DeployTool; #[async_trait] impl AgentTool for DeployTool { fn name(&self) -> &str { "deploy" } fn label(&self) -> &str { "Deploy App" } fn description(&self) -> &str { "Deploy the application to production." } fn parameters_schema(&self) -> serde_json::Value { serde_json::json!({ "type": "object", "properties": { "env": { "type": "string", "description": "Target environment" } }, "required": ["env"] }) } async fn execute( &self, params: serde_json::Value, ctx: ToolContext, ) -> Result<ToolResult, ToolError> { let env = params["env"].as_str().unwrap_or("staging"); let steps = ["Building image", "Running tests", "Pushing to registry", "Rolling out"]; for (i, step) in steps.iter().enumerate() { if ctx.cancel.is_cancelled() { return Err(ToolError::Cancelled); } // Stream each step to the UI if let Some(ref cb) = &ctx.on_update { cb(ToolResult { content: vec![Content::Text { text: format!("[{}/{}] {}...", i + 1, steps.len(), step), }], details: serde_json::json!({ "step": i + 1, "total": steps.len(), "phase": step, }), }); } // Simulate work (`yoagent::rt::sleep` works on native and wasm32) yoagent::rt::sleep(std::time::Duration::from_secs(2)).await; } // Only this final result is sent to the LLM Ok(ToolResult { content: vec![Content::Text { text: format!("Successfully deployed to {}", env), }], details: serde_json::json!({"env": env, "status": "success"}), }) } } #[tokio::main] async fn main() { let mut agent = Agent::from_config(ModelConfig::anthropic("claude-sonnet-5", "Claude Sonnet 5")) .with_system_prompt("You are a deployment assistant.") .with_tools(vec![Box::new(DeployTool)]); let mut rx = agent.prompt("Deploy to production").await; while let Some(event) = rx.recv().await { match event { // LLM text streaming AgentEvent::MessageUpdate { delta: StreamDelta::Text { delta }, .. } => print!("{}", delta), // Tool progress streaming AgentEvent::ToolExecutionStart { tool_name, .. } => { println!("\n🚀 Starting {}...", tool_name); } AgentEvent::ToolExecutionUpdate { partial_result, .. } => { if let Some(Content::Text { text }) = partial_result.content.first() { println!(" {}", text); } } AgentEvent::ToolExecutionEnd { tool_name, is_error, .. } => { if is_error { println!(" ❌ {} failed", tool_name); } else { println!(" ✅ {} complete", tool_name); } } AgentEvent::ProgressMessage { text, .. } => { println!(" 💬 {}", text); } AgentEvent::AgentEnd { .. } => break, _ => {} } } }
Running this produces:
🚀 Starting deploy...
[1/4] Building image...
[2/4] Running tests...
[3/4] Pushing to registry...
[4/4] Rolling out...
✅ deploy complete
Successfully deployed to production. The deployment completed all 4 stages.
The human sees each step as it happens. The LLM only sees "Successfully deployed to production" and can continue the conversation from there.
How agents benefit
When an AI agent (like a coding assistant) uses yoagent, streaming tool output helps in two ways:
-
Human oversight — The human watching the agent work sees real-time progress instead of waiting for a tool to finish. A bash command running
cargo buildcan stream compiler output as it happens, so the human can interrupt early if something is wrong. -
Agent UIs — Tools like web dashboards, IDE extensions, or chat interfaces can render live progress bars, log tails, or status indicators. The
detailsfield inToolResultcarries structured data (progress percentage, byte counts, etc.) that UIs can render however they want.
The LLM itself doesn't see updates — it works with final results only. This is intentional: partial output would waste context tokens and confuse the model. The streaming is purely a human-facing feature.
Tools That Change at Runtime: ToolSource
with_tools fixes an agent's tool list until you replace it. When the set of
tools changes while the agent lives — plugins loaded and unloaded, an MCP
server that reconnects with a different list, a feature flag — add a
ToolSource instead:
#![allow(unused)] fn main() { use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::{Arc, Mutex}; use yoagent::{Agent, AgentTool, ToolSource}; /// Tools the application swaps at runtime; `swap` moves the generation, so a /// swap during a run is seen before its next request. struct Swappable(Mutex<Vec<Arc<dyn AgentTool>>>, AtomicU64); impl Swappable { fn swap(&self, tools: Vec<Arc<dyn AgentTool>>) { *self.0.lock().unwrap() = tools; self.1.fetch_add(1, Ordering::SeqCst); } } #[async_trait::async_trait] impl ToolSource for Swappable { async fn tools(&self) -> Vec<Arc<dyn AgentTool>> { self.0.lock().unwrap().clone() } fn generation(&self) -> u64 { self.1.load(Ordering::SeqCst) } } let swappable = Arc::new(Swappable(Mutex::new(vec![]), AtomicU64::new(0))); let agent = Agent::from_provider(provider, config) .with_tools(my_static_tools) .with_tool_source(swappable.clone()); // Later — between runs, or during one (seen before its next request): swappable.swap(new_tools); }
The contract:
- Consulted once per run — and again when it says it changed. Every
prompt*andcontinue_loop*call asks each source for its tools before the first request. A source whose tools can change while a run goes on (a plugin loaded mid-run, a tool that loads others on demand, an MCP server that announces a new list) also implementsgeneration(): a number that moves whenever its tools change. Before each later request — once every tool call of the last turn has returned, so no call the model made is left without its tool — the loop asks again only the sources whose generation moved; their new tools (a shorter list too) are offered from that request, and the collision rules below are applied again. Extensions' per-run tools are not asked again. A source that keeps the default generation (0) is asked only at the start of each run: its list is fixed for the run, a tool withdrawn stays offered until the run ends (make it fail cleanly if its backend is gone), and a tool added is offered from the next run. - Prompt caching. Tool definitions open the provider's cached prefix, so any change to the offered set rewrites the whole cache — once per change, mid-run too. Consulting per run, and again only on a change, bounds that cost — it does not prevent it: a run whose set differs from the previous run's pays a full prefix rewrite. Order alone never costs anything: sourced tools are sorted by name after collisions are resolved (the agent's own tools keep their order).
- No timeout. The run waits for every source. At the start of a run on
an
Agent, dropping the prompt future while it waits is the escape (sources are consulted before any agent state is touched); aSubAgentToolstops waiting when the parent run is cancelled. A source asked again mid-run is raced against the run's cancel, and keeps its previous tools if the run is cancelled while it answers. - For that run only. Sourced tools are appended after the agent's own
tools and dropped again when the run ends; they never join
with_tools' list. A model that calls a tool that is no longer offered gets the usualTool … not founderror result. - Name collisions. Providers reject two tools with one name, so exactly
one survives: the agent's own tools (including the injected
shared_statetool) win, then earlier sources in installation order, then the earlier tool within one source; an extension's per-run tools come after all of them. Each dropped duplicate is logged withtracing::warn!; the run is never refused. (The agent's own tools —with_tools, MCP adapters included — are not de-duplicated: give clashing MCP or OpenAPI tools a prefix withMcpToolAdapter::with_prefixorOpenApiConfig::with_name_prefix.) - Failures. A source that panics — while building its future or while it
runs — is contained, logged with the panic message, and contributes no
tools to that run (asked again mid-run: from then on, until its generation
moves again). A
generation()that panics counts as unchanged, logged once per run.
Several sources may be added. SubAgentTool::with_tool_source mirrors it —
consulted at the start of every delegation, and between the child's turns
when its generation moves. An extension's tools hook is
asked only at the start of a run, never again mid-run; the rutis bridge
(yoagent-rutis) uses it, so an agent sees exactly the tools of the plugins
loaded at each run start.
Execution Strategies
When the LLM returns multiple tool calls in a single response (e.g., "read file A, read file B, run bash C"), ToolExecutionStrategy controls how they run:
| Strategy | Behavior |
|---|---|
Sequential | One at a time. Steering checked between each tool. Use for debugging or tools with shared mutable state. |
Parallel (default) | All tool calls run concurrently via futures::join_all. Steering checked after all complete. Best latency for independent tools. |
Batched { size } | Run in groups of N. Steering checked between batches. Balances speed with human-in-the-loop control. |
Configuration
#![allow(unused)] fn main() { use yoagent::agent::Agent; use yoagent::types::ToolExecutionStrategy; // Default — parallel (fastest) let agent = Agent::from_config(ModelConfig::anthropic("claude-sonnet-5", "Claude Sonnet 5")); // Sequential (debug / shared state) let agent = Agent::from_config(ModelConfig::anthropic("claude-sonnet-5", "Claude Sonnet 5")) .with_tool_execution(ToolExecutionStrategy::Sequential); // Batched — 3 at a time let agent = Agent::from_config(ModelConfig::anthropic("claude-sonnet-5", "Claude Sonnet 5")) .with_tool_execution(ToolExecutionStrategy::Batched { size: 3 }); }
When to use each
- Parallel (default): Most tool calls are independent — file reads, searches, API calls. Running them concurrently can cut latency dramatically (3 tools × 50ms = ~50ms instead of ~150ms).
- Sequential: When tools have side effects that depend on order, or when you need fine-grained steering control between each tool.
- Batched: When you want parallelism but also want steering checkpoints. For example,
Batched { size: 3 }runs 3 tools concurrently, checks for user interrupts, then runs the next 3.
Steering messages are always checked between execution units (between each tool in Sequential, after all tools in Parallel, between batches in Batched). If a user interrupts, remaining tools are skipped.
Permissions: Tool Middleware
Every tool call can be gated by an async middleware chain — the mechanism behind permission prompts, policy engines, and argument rewriting. yoagent ships the hook, not a policy: with no middleware installed, every call runs.
#![allow(unused)] fn main() { use yoagent::{ToolCallRequest, ToolDecision, ToolMiddleware}; struct ReadOnlyPolicy; #[async_trait::async_trait] impl ToolMiddleware for ReadOnlyPolicy { async fn before_tool(&self, call: &ToolCallRequest<'_>) -> ToolDecision { match call.tool_name { "write_file" | "edit_file" | "bash" => { ToolDecision::Deny("read-only session".into()) } _ => ToolDecision::Allow, } } } let agent = Agent::from_config(ModelConfig::anthropic("claude-sonnet-5", "Sonnet 5")) .with_tools(default_tools()) .with_tool_middleware(ReadOnlyPolicy); }
Semantics:
Allow— the call proceeds (with the current arguments).Modify(args)— the call proceeds with replacement arguments (e.g. rewrite a path into a sandbox). Later middleware in the chain see the rewritten arguments; theToolExecutionStartevent carries what actually runs.Deny(reason)— the call never executes. The reason is returned to the LLM as an error tool result ("Tool call denied: ..."), so the model can adapt — pick another tool, ask the user — and the loop continues. A denial never aborts the run.
A middleware that panics is contained: the call is denied (reason "tool middleware panicked") and the loop continues — a buggy policy can't kill the
run.
The hook is async, so an interactive app can prompt a human before deciding.
Note that under the default Parallel execution strategy, middleware for
parallel tool calls run concurrently — if you need one-at-a-time approval UX,
serialize inside your middleware (e.g. a tokio::sync::Mutex) or switch to
ToolExecutionStrategy::Sequential.
Sub-agents gate their own tool calls the same way via
SubAgentTool::with_tool_middleware.
Middleware can also see the conversation: call.messages is the loop's
history (before transform_context, including the assistant message carrying
the call), and call.run_prompts holds the user messages this run was given,
which compaction cannot remove. For "what did the user ask?", use
call.user_request(): it skips loop-injected messages
(yoagent::is_loop_injected), never looks back past a compaction boundary,
falls back to the run's prompts, and returns None when nothing says what the
user wants — enough for policies such as "was this destructive call actually
requested?". The decision-model tool gate
is one. call.user_request_parts() gives the same selection as structured
UserRequestParts (latest, reply, source, run_prompts); the prose of
user_request() is not a stable format.
To unit-test a middleware without running an agent, build the request yourself:
#![allow(unused)] fn main() { let args = serde_json::json!({"path": "/tmp/x"}); let prompts = [Message::user("delete /tmp/x")]; let call = ToolCallRequest::new("call-1", "rm", &args) .with_run_prompts(&prompts); // or .with_messages(&history) assert!(matches!(MyPolicy.before_tool(&call).await, ToolDecision::Allow)); }